diff --git a/lib/services/database.dart b/lib/services/database.dart index bc068d6..49a127b 100644 --- a/lib/services/database.dart +++ b/lib/services/database.dart @@ -679,6 +679,39 @@ class Database { return users; } + + static Future getInventory(firebase.IdTokenResult idToken, + String barcodeId, BuildContext context) async { + var client = http.Client(); + + Map data = { + 'endpoint': 'get-inventory', + 'auth': idToken.token, + 'barcodeId': barcodeId, + }; + + var body = json.encode(data); + + var result = await client.post(Uri.parse(Constants.SERVER_URL), + headers: {"Content-Type": "application/json"}, body: body); + + if (result.statusCode != 200) { + Alert.showAlert(context, jsonDecode(result.body)['err']); + return null; + } + + if (jsonDecode(result.body)['inventory'] == null) { + Alert.showAlert(context, "Inventoried tool does not exist!"); + return null; + } + + final parsed = + jsonDecode(result.body)['inventory']; + + var inventory = Inventory.fromJson(parsed); + + return inventory; + } } final Map deliveryMap = { @@ -796,6 +829,30 @@ class User { } } +class Inventory { + final String name; + final String? description; + final int count; + final String barcodeId; + Inventory( + {required this.name, + required this.count, + required this.barcodeId, + required this.description}); + + factory Inventory.fromJson(Map json) { + return Inventory( + name: json['name'] as String, + count: json['count'] as int, + barcodeId: json['barcodeId'] as String, + description: json['description'] as String?); + } + + String toString() { + return name; + } +} + class LastCheckInTime { late int _value; diff --git a/lib/ui/tools/ToolModal.dart b/lib/ui/tools/ToolModal.dart index 45b5980..f8c201d 100644 --- a/lib/ui/tools/ToolModal.dart +++ b/lib/ui/tools/ToolModal.dart @@ -8,8 +8,8 @@ import 'package:OptixToolkit/services/database.dart'; import 'package:google_fonts/google_fonts.dart'; class ToolModal extends StatelessWidget { - final String toolName; - const ToolModal({Key? key, required this.toolName}) : super(key: key); + final Inventory inventory; + const ToolModal({Key? key, required this.inventory}) : super(key: key); @override Widget build(BuildContext context) { @@ -38,7 +38,7 @@ class ToolModal extends StatelessWidget { return AlertDialog( title: Text( - 'Tool: $toolName', + 'Tool: ${inventory.name}', style: GoogleFonts.rubik( fontSize: 25.0, fontWeight: FontWeight.bold, @@ -53,7 +53,7 @@ class ToolModal extends StatelessWidget { text: TextSpan( children: [ TextSpan( - text: 'Status: ', + text: 'Count: ${inventory.count}', style: GoogleFonts.rubik( fontWeight: FontWeight.bold, color: Colors.white, @@ -63,6 +63,21 @@ class ToolModal extends StatelessWidget { ], ), ), + if (inventory.description != null) + RichText( + text: TextSpan( + children: [ + TextSpan( + text: 'Description: ${inventory.description}', + style: GoogleFonts.rubik( + fontWeight: FontWeight.bold, + color: Colors.white, + fontSize: 15.0, + ), + ), + ], + ), + ), const SizedBox(height: 20), ElevatedButton( style: ElevatedButton.styleFrom( diff --git a/lib/ui/tools/ToolsPage.dart b/lib/ui/tools/ToolsPage.dart index 549b687..4547152 100644 --- a/lib/ui/tools/ToolsPage.dart +++ b/lib/ui/tools/ToolsPage.dart @@ -315,7 +315,12 @@ class _toolState extends State with RouteAware { Future _scanBarcode(BuildContext context) async { try { String barcodeValue = (await BarcodeScanner.scan()).rawContent; - _showBarcodeModal(context, barcodeValue); + + Inventory? inv = await Database.getInventory(idToken, barcodeValue, context); + + if (inv != null) { + _showBarcodeModal(context, inv); + } } on PlatformException catch (e) { if (e.code == BarcodeScanner.cameraAccessDenied) { print('Camera permission not granted'); @@ -330,12 +335,12 @@ class _toolState extends State with RouteAware { } // Function to show a modal - void _showBarcodeModal(BuildContext context, String barcodeValue) { + void _showBarcodeModal(BuildContext context, Inventory inv) { showDialog( context: context, //tells flutter the context, or where we are in the app builder: (BuildContext context) { // return object of type Dialog - return ToolModal(toolName: barcodeValue); + return ToolModal(inventory: inv); }, ); }