Skip to content

Commit

Permalink
Start integration with backend
Browse files Browse the repository at this point in the history
  • Loading branch information
rjawesome committed Dec 17, 2023
1 parent b1a3997 commit d0f2334
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 7 deletions.
57 changes: 57 additions & 0 deletions lib/services/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,39 @@ class Database {

return users;
}

static Future<Inventory?> 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<String, String> deliveryMap = {
Expand Down Expand Up @@ -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<String, dynamic> 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;

Expand Down
23 changes: 19 additions & 4 deletions lib/ui/tools/ToolModal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand All @@ -53,7 +53,7 @@ class ToolModal extends StatelessWidget {
text: TextSpan(
children: <TextSpan>[
TextSpan(
text: 'Status: ',
text: 'Count: ${inventory.count}',
style: GoogleFonts.rubik(
fontWeight: FontWeight.bold,
color: Colors.white,
Expand All @@ -63,6 +63,21 @@ class ToolModal extends StatelessWidget {
],
),
),
if (inventory.description != null)
RichText(
text: TextSpan(
children: <TextSpan>[
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(
Expand Down
11 changes: 8 additions & 3 deletions lib/ui/tools/ToolsPage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,12 @@ class _toolState extends State<ToolWidget> 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');
Expand All @@ -330,12 +335,12 @@ class _toolState extends State<ToolWidget> 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);
},
);
}
Expand Down

0 comments on commit d0f2334

Please sign in to comment.