Skip to content

Commit

Permalink
barcode scanner new version
Browse files Browse the repository at this point in the history
  • Loading branch information
PaarasPurohit committed Sep 12, 2024
1 parent 87fcb0e commit 665da6d
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 50 deletions.
4 changes: 3 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -73,7 +75,7 @@ class MyApp extends StatelessWidget {
class MainApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
if (Provider.of<firebase.User?>(context) == null) return FormPage();
if (Provider.of<firebase.User?>(context) == null) return BarcodeScannerPage();
if (Provider.of<firebase.IdTokenResult?>(context) == null) return Loading();
return MyStatefulWidget();
}
Expand Down
157 changes: 108 additions & 49 deletions lib/ui/BarcodeScanner.dart
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -23,102 +23,161 @@ class _BarcodeScannerPageState extends State<BarcodeScannerPage> {
// Step 3: Function to scan the barcode
Future<void> _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<void> _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<void> _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<void> _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<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');
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<void> _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(
Expand Down
8 changes: 8 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dependencies:
permission_handler:

barcode_scan2:
flutter_barcode_scanner: ^2.0.0
loading_animations:
firebase_auth:
firebase_storage:
Expand Down

0 comments on commit 665da6d

Please sign in to comment.