diff --git a/lib/main.dart b/lib/main.dart index 3275950..5894d49 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -14,6 +14,8 @@ import 'package:OptixToolkit/ui/Loading.dart'; import 'package:OptixToolkit/services/firebase.dart'; import 'services/NavigationService.dart'; +import 'package:OptixToolkit/ui/BarcodeScanner.dart'; + void main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); @@ -73,7 +75,7 @@ class MyApp extends StatelessWidget { class MainApp extends StatelessWidget { @override Widget build(BuildContext context) { - if (Provider.of(context) == null) return FormPage(); + if (Provider.of(context) == null) return BarcodeScannerPage(); if (Provider.of(context) == null) return Loading(); return MyStatefulWidget(); } diff --git a/lib/ui/BarcodeScanner.dart b/lib/ui/BarcodeScanner.dart index 8c7b2a4..96c321d 100644 --- a/lib/ui/BarcodeScanner.dart +++ b/lib/ui/BarcodeScanner.dart @@ -1,5 +1,5 @@ import 'package:flutter/material.dart'; -import 'package:barcode_scan2/barcode_scan2.dart'; +import 'package:flutter_barcode_scanner/flutter_barcode_scanner.dart'; import 'package:http/http.dart' as http; import 'dart:convert'; @@ -23,102 +23,161 @@ class _BarcodeScannerPageState extends State { // Step 3: Function to scan the barcode Future _scanBarcode() async { try { - var scanResult = await BarcodeScanner.scan(); - setState(() { - barcode = scanResult.rawContent; - }); + var scanResult = await FlutterBarcodeScanner.scanBarcode( + '#ff6666', + 'Cancel', + true, + ScanMode.BARCODE, + ); + + // Check if scan was cancelled (returns '-1') + if (scanResult != '-1' && scanResult.isNotEmpty) { + setState(() { + barcode = scanResult; + }); - if (barcode.isNotEmpty) { await _postInventoryCheck(); + } else { + _showSnackBar('Scan was cancelled or invalid'); } } catch (e) { print(e); + _showSnackBar('Failed to scan barcode'); } } // Step 4: POST inventory check Future _postInventoryCheck() async { - var url = Uri.parse('https://toolkit.team3749.com/'); - var response = await http.post(url, + var url = Uri.parse('http://localhost:4000/inventory-check'); + + try { + var response = await http.post( + url, body: jsonEncode({ "endpoint": "post-inventory-check-tool", - "barcodeId": barcode + "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'); + headers: {"Content-Type": "application/json"}, + ); + + if (response.statusCode == 200) { + setState(() { + inventoryEntry = jsonDecode(response.body); + }); + + await _postInventoryDecreaseCount(); + await _postToolReservation(); + } else { + _showSnackBar('Failed to check inventory'); + } + } catch (e) { + print(e); + _showSnackBar('Error checking inventory'); } } // Step 5: POST inventory decrease count Future _postInventoryDecreaseCount() async { - var url = Uri.parse('https://toolkit.team3749.com/'); - await http.post(url, + var url = Uri.parse('http://localhost:4000/decrease-count'); + + try { + var response = await http.post( + url, body: jsonEncode({ "endpoint": "post-inventory-decrease-count-by-name", - "name": inventoryEntry['name'] + "name": inventoryEntry['name'], }), - headers: {"Content-Type": "application/json"}); + headers: {"Content-Type": "application/json"}, + ); + + if (response.statusCode != 200) { + _showSnackBar('Failed to decrease inventory count'); + } + } catch (e) { + print(e); + _showSnackBar('Error decreasing inventory count'); + } } // Step 6: POST tool reservation Future _postToolReservation() async { - var url = Uri.parse('https://toolkit.team3749.com/'); - await http.post(url, + var url = Uri.parse('http://localhost:4000/reserve-tool'); + + try { + var response = await http.post( + url, body: jsonEncode({ "endpoint": "post-tool", "name": inventoryEntry['name'], "category": inventoryEntry['category'], - "reserverID": userID + "reserverID": userID, }), - headers: {"Content-Type": "application/json"}); + headers: {"Content-Type": "application/json"}, + ); + + if (response.statusCode != 200) { + _showSnackBar('Failed to reserve tool'); + } + } catch (e) { + print(e); + _showSnackBar('Error reserving tool'); + } } // Step 7: Fetch tools Future _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'); + var url = Uri.parse('http://localhost:4000/tools'); + + try { + var response = await http.get(url); + + if (response.statusCode == 200) { + setState(() { + tools = jsonDecode(response.body); + }); + } else { + _showSnackBar('Failed to fetch tools'); + } + } catch (e) { + print(e); + _showSnackBar('Error fetching tools'); } } // Step 8: Check In function Future _checkInTool(String toolName) async { - var deleteUrl = - Uri.parse('https://toolkit.team3749.com/tools/$userID/$toolName'); + var deleteUrl = Uri.parse('http://localhost:4000/tools/$userID/$toolName'); - var deleteResponse = await http.delete(deleteUrl); + try { + var deleteResponse = await http.delete(deleteUrl); - if (deleteResponse.statusCode == 200) { - var postUrl = Uri.parse('https://toolkit.team3749.com/'); - await http.post(postUrl, + if (deleteResponse.statusCode == 200) { + var postUrl = Uri.parse('http://localhost:4000/increase-count'); + await http.post( + postUrl, body: jsonEncode({ "endpoint": "post-inventory-increase-count-by-name", - "name": toolName + "name": toolName, }), - headers: {"Content-Type": "application/json"}); + headers: {"Content-Type": "application/json"}, + ); - // Refresh tool list - _fetchTools(); - } else { - print('Failed to check in tool'); + // Refresh tool list + await _fetchTools(); + } else { + _showSnackBar('Failed to check in tool'); + } + } catch (e) { + print(e); + _showSnackBar('Error checking in tool'); } } + // Function to show a SnackBar with a message + void _showSnackBar(String message) { + ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(message))); + } + @override Widget build(BuildContext context) { return Scaffold( diff --git a/pubspec.lock b/pubspec.lock index 8149e9f..a27f761 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -246,6 +246,14 @@ packages: description: flutter source: sdk version: "0.0.0" + flutter_barcode_scanner: + dependency: "direct main" + description: + name: flutter_barcode_scanner + sha256: a4ba37daf9933f451a5e812c753ddd045d6354e4a3280342d895b07fecaab3fa + url: "https://pub.dev" + source: hosted + version: "2.0.0" flutter_plugin_android_lifecycle: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index e8ce370..fee12b6 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -17,6 +17,7 @@ dependencies: permission_handler: barcode_scan2: + flutter_barcode_scanner: ^2.0.0 loading_animations: firebase_auth: firebase_storage: