Skip to content

Commit

Permalink
barcode scanner version 1
Browse files Browse the repository at this point in the history
  • Loading branch information
PaarasPurohit committed Sep 8, 2024
1 parent b416068 commit 87fcb0e
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"dart.flutterSdkPath": "C:\\Users\\\\dev\\flutter",
"dart.flutterSdkPath": "C:\\Users\\Purohitz\\dev\\flutter",
"java.configuration.updateBuildConfiguration": "automatic"
}
11 changes: 5 additions & 6 deletions lib/services/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import 'package:flutter/material.dart';
// Project imports:
import 'package:OptixToolkit/constants.dart' as Constants;

import 'package:flutter/foundation.dart';

class FailedRequestException implements Exception {}

Expand Down Expand Up @@ -595,9 +594,9 @@ class Database {

for (var tool in list) {
if (category_map.containsKey(tool.category)) {
category_map[tool.category as String]!.insert(0, tool);
category_map[tool.category]!.insert(0, tool);
} else {
category_map[tool.category as String] = [tool];
category_map[tool.category] = [tool];
}
}

Expand Down Expand Up @@ -641,9 +640,9 @@ class Database {

for (var tool in list) {
if (category_map.containsKey(tool.category)) {
category_map[tool.category as String]!.insert(0, tool);
category_map[tool.category]!.insert(0, tool);
} else {
category_map[tool.category as String] = [tool];
category_map[tool.category] = [tool];
}
}

Expand Down Expand Up @@ -919,7 +918,7 @@ class MeetingCount {

void LogPrint(Object object) async {
int defaultPrintLength = 1020;
if (object == null || object.toString().length <= defaultPrintLength) {
if (object.toString().length <= defaultPrintLength) {
print(object);
} else {
String log = object.toString();
Expand Down
4 changes: 2 additions & 2 deletions lib/ui/BarcodeResultPage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class BarcodeResultPage extends StatelessWidget {
child: Text('Change',
style: TextStyle(color: Colors.white)),
style: ElevatedButton.styleFrom(
primary: Colors.blue, // Set the button color to blue
backgroundColor: Colors.blue, // Set the button color to blue
),
),
],
Expand All @@ -84,7 +84,7 @@ class BarcodeResultPage extends StatelessWidget {
child: Text('Change',
style: TextStyle(color: Colors.white)),
style: ElevatedButton.styleFrom(
primary: Colors.blue, // Set the button color to blue
backgroundColor: Colors.blue, // Set the button color to blue
),
),
],
Expand Down
154 changes: 154 additions & 0 deletions lib/ui/BarcodeScanner.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import 'package:flutter/material.dart';
import 'package:barcode_scan2/barcode_scan2.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';

class BarcodeScannerPage extends StatefulWidget {
@override
_BarcodeScannerPageState createState() => _BarcodeScannerPageState();
}

class _BarcodeScannerPageState extends State<BarcodeScannerPage> {
String barcode = '';
Map<String, dynamic> inventoryEntry = {};
List<dynamic> tools = [];
String userID = "yourUserID"; // Replace with actual user ID

@override
void initState() {
super.initState();
_fetchTools();
}

// Step 3: Function to scan the barcode
Future<void> _scanBarcode() async {
try {
var scanResult = await BarcodeScanner.scan();
setState(() {
barcode = scanResult.rawContent;
});

if (barcode.isNotEmpty) {
await _postInventoryCheck();
}
} catch (e) {
print(e);
}
}

// Step 4: POST inventory check
Future<void> _postInventoryCheck() async {
var url = Uri.parse('https://toolkit.team3749.com/');
var response = await http.post(url,
body: jsonEncode({
"endpoint": "post-inventory-check-tool",
"barcodeId": barcode
}),
headers: {"Content-Type": "application/json"});

if (response.statusCode == 200) {
setState(() {
inventoryEntry = jsonDecode(response.body);
});

await _postInventoryDecreaseCount();
await _postToolReservation();
} else {
print('Failed to check inventory');
}
}

// Step 5: POST inventory decrease count
Future<void> _postInventoryDecreaseCount() async {
var url = Uri.parse('https://toolkit.team3749.com/');
await http.post(url,
body: jsonEncode({
"endpoint": "post-inventory-decrease-count-by-name",
"name": inventoryEntry['name']
}),
headers: {"Content-Type": "application/json"});
}

// Step 6: POST tool reservation
Future<void> _postToolReservation() async {
var url = Uri.parse('https://toolkit.team3749.com/');
await http.post(url,
body: jsonEncode({
"endpoint": "post-tool",
"name": inventoryEntry['name'],
"category": inventoryEntry['category'],
"reserverID": userID
}),
headers: {"Content-Type": "application/json"});
}

// Step 7: Fetch tools
Future<void> _fetchTools() async {
var url = Uri.parse('https://toolkit.team3749.com/tools');
var response = await http.get(url);

if (response.statusCode == 200) {
setState(() {
tools = jsonDecode(response.body);
});
} else {
print('Failed to fetch tools');
}
}

// Step 8: Check In function
Future<void> _checkInTool(String toolName) async {
var deleteUrl =
Uri.parse('https://toolkit.team3749.com/tools/$userID/$toolName');

var deleteResponse = await http.delete(deleteUrl);

if (deleteResponse.statusCode == 200) {
var postUrl = Uri.parse('https://toolkit.team3749.com/');
await http.post(postUrl,
body: jsonEncode({
"endpoint": "post-inventory-increase-count-by-name",
"name": toolName
}),
headers: {"Content-Type": "application/json"});

// Refresh tool list
_fetchTools();
} else {
print('Failed to check in tool');
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Barcode Scanner'),
),
body: Column(
children: <Widget>[
ElevatedButton(
onPressed: _scanBarcode,
child: Text('Scan Barcode'),
),
SizedBox(height: 20),
Expanded(
child: ListView.builder(
itemCount: tools.length,
itemBuilder: (context, index) {
var tool = tools[index];
return ListTile(
title: Text(tool['name']),
trailing: ElevatedButton(
onPressed: () => _checkInTool(tool['name']),
child: Text('Check In'),
),
);
},
),
),
],
),
);
}
}
2 changes: 0 additions & 2 deletions lib/ui/ForgetPassword.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Flutter imports:
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:OptixToolkit/services/Good.dart';

Expand Down Expand Up @@ -29,7 +28,6 @@ class _ForgetPasswordState extends State<ForgetPassword> {
final Color gray = Color(0xff3A3D41);
final Color subtleGray = Color(0xffcccccc);
final Color divider = Color(0xff3a3d41);
bool _showPassword = false;

@override
void dispose() {
Expand Down
3 changes: 0 additions & 3 deletions lib/ui/HomePage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'package:firebase_auth/firebase_auth.dart' as firebase;
import 'package:google_fonts/google_fonts.dart';
import 'package:provider/provider.dart';
import 'package:OptixToolkit/services/database.dart';
import 'package:OptixToolkit/ui/Loading.dart';
import 'package:OptixToolkit/ui/tools/ToolLine.dart';

class homePage extends StatelessWidget {
Expand Down Expand Up @@ -39,10 +38,8 @@ class homePage2 extends StatefulWidget {
class _homePage2State extends State<homePage2> {
@override
Widget build(BuildContext context) {
var parts = Provider.of<List<Part>>(context);
var toolsMap = Provider.of<Map<String, List<Tool>>>(context);
var time = Provider.of<int>(context);
if (parts == null || toolsMap == null || time == null) return Loading();

List<Tool> tools = [];

Expand Down
10 changes: 2 additions & 8 deletions lib/ui/HoursPage.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:OptixToolkit/services/GoodPop.dart';
import 'package:OptixToolkit/ui/Loading.dart';
import 'package:firebase_auth/firebase_auth.dart' as firebase;
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
Expand Down Expand Up @@ -51,8 +50,7 @@ class _hoursPageState extends State<hoursPageLoaded> {
Provider.of<firebase.IdTokenResult>(context, listen: false), context);

if (curMeetingCountProv == null ||
curLastCheckInProv == null ||
curTime == null) {
curLastCheckInProv == null) {
return;
}

Expand All @@ -77,9 +75,6 @@ class _hoursPageState extends State<hoursPageLoaded> {
var timeProv = Provider.of<int>(context);
var lastCheckInProv = Provider.of<LastCheckInTime>(context);
var meetingCountProv = Provider.of<MeetingCount>(context);
if (timeProv == null ||
lastCheckInProv == null ||
meetingCountProv == null) return Loading();

time = timeProv;
lastCheckIn = lastCheckInProv.getValue();
Expand Down Expand Up @@ -117,8 +112,7 @@ class _hoursPageState extends State<hoursPageLoaded> {
};

final ButtonStyle buttonStyle = ElevatedButton.styleFrom(
textStyle: const TextStyle(fontSize: 30, fontWeight: FontWeight.bold),
primary: Colors.blue,
textStyle: const TextStyle(fontSize: 30, fontWeight: FontWeight.bold), backgroundColor: Colors.blue,
minimumSize: Size(MediaQuery.of(context).size.width * 0.9, 60.0),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12), // <-- Radius
Expand Down
1 change: 0 additions & 1 deletion lib/ui/UserCard.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Flutter imports:
import 'package:OptixToolkit/services/NavigationService.dart';
import 'package:firebase_auth/firebase_auth.dart' as firebase;
import 'package:flutter/material.dart';

Expand Down
1 change: 0 additions & 1 deletion lib/ui/parts/PartModal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import 'package:OptixToolkit/services/database.dart';
import 'package:google_fonts/google_fonts.dart';

// Project imports:
import 'package:OptixToolkit/services/database.dart';

class PartModal extends StatelessWidget {
final Part part;
Expand Down
3 changes: 0 additions & 3 deletions lib/ui/tools/ToolReserveItem.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,10 @@ class ToolReserveItem extends StatelessWidget {
switch (status) {
case "notInUse":
return "Tool is Broken";
break;
case "outOfService":
return "Tool is Working";
break;
default:
return "Tool is in Use";
break;
}
}

Expand Down
6 changes: 3 additions & 3 deletions lib/ui/tools/ToolsPage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class _toolState extends State<ToolWidget> with RouteAware {
} else {
print('Unknown Error: $e');
}
} on FormatException catch (e) {
} on FormatException {
print('User pressed back button before scanning');
} catch (e) {
print('Unknown Error: $e');
Expand All @@ -145,7 +145,7 @@ class _toolState extends State<ToolWidget> with RouteAware {
} else {
print('Unknown Error: $e');
}
} on FormatException catch (e) {
} on FormatException {
print('User pressed back button before scanning');
} catch (e) {
print('Unknown Error: $e');
Expand Down Expand Up @@ -349,7 +349,7 @@ class _toolState extends State<ToolWidget> with RouteAware {
} else {
print('Unknown Error: $e');
}
} on FormatException catch (e) {
} on FormatException {
print('User pressed back button before scanning');
} catch (e) {
print('Unknown Error: $e');
Expand Down
2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ packages:
source: hosted
version: "5.8.12"
firebase_core:
dependency: transitive
dependency: "direct main"
description:
name: firebase_core
sha256: "96607c0e829a581c2a483c658f04e8b159964c3bae2730f73297070bc85d40bb"
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ dependencies:
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons:
firebase_core: any
dev_dependencies:
flutter_test:
sdk: flutter
Expand Down

0 comments on commit 87fcb0e

Please sign in to comment.