2
0
mirror of https://github.com/inventree/inventree-app.git synced 2025-07-12 00:24:12 +00:00

Toot toot, it's the refactor tractor

- Introduces the concept of a "BarcodeHandler"
- Re-uses most of the code
- Custom handlers for barcode success / failure
This commit is contained in:
Oliver Walters
2021-01-21 20:17:05 +11:00
parent e39cd4c009
commit c15d3a6524
4 changed files with 247 additions and 104 deletions

View File

@ -3,10 +3,6 @@ import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart'; import 'package:font_awesome_flutter/font_awesome_flutter.dart';
//import 'package:qr_utils/qr_utils.dart';
//import 'package:flutter_barcode_scanner/flutter_barcode_scanner.dart';
//import 'package:barcode_scan/barcode_scan.dart';
import 'package:qr_code_scanner/qr_code_scanner.dart'; import 'package:qr_code_scanner/qr_code_scanner.dart';
import 'package:InvenTree/inventree/stock.dart'; import 'package:InvenTree/inventree/stock.dart';
@ -22,179 +18,288 @@ import 'package:InvenTree/widget/stock_detail.dart';
import 'dart:convert'; import 'dart:convert';
class InvenTreeQRView extends StatefulWidget { class BarcodeHandler {
/**
* Class which "handles" a barcode, by communicating with the InvenTree server,
* and handling match / unknown / error cases.
*
* Override functionality of this class to perform custom actions,
* based on the response returned from the InvenTree server
*/
InvenTreeQRView({Key key}) : super(key: key); BarcodeHandler();
@override QRViewController _controller;
State<StatefulWidget> createState() => _QRViewState(); BuildContext _context;
}
class _QRViewState extends State<InvenTreeQRView> { Future<void> onBarcodeMatched(Map<String, dynamic> data) {
// Called when the server "matches" a barcode
// Override this function
}
QRViewController _controller; Future<void> onBarcodeUnknown(Map<String, dynamic> data) {
// Called when the server does not know about a barcode
// Override this function
}
BuildContext context; Future<void> onBarcodeUnhandled(Map<String, dynamic> data) {
// Called when the server returns an unhandled response
showErrorDialog(
_context,
"Response Data",
data.toString(),
error: "Unknown Response",
onDismissed: () {
_controller.resumeCamera();
}
);
}
_QRViewState() : super(); Future<void> processBarcode(BuildContext context, QRViewController _controller, String barcode) {
this._context = context;
this._controller = _controller;
final GlobalKey qrKey = GlobalKey(debugLabel: 'QR'); print("Scanned barcode data: ${barcode}");
showProgressDialog(context, "Scanning", "Sending barcode data to server");
// Callback when the server repsonds with a match for the barcode // Send barcode request to server
Future<void> onBarcodeMatched(Map<String, dynamic> response) { InvenTreeAPI().post(
"barcode/",
body: {
"barcode": barcode
}
).then((var response) {
hideProgressDialog(context);
if (response.statusCode != 200) {
showErrorDialog(
context,
"Status Code: ${response.statusCode}",
"${response.body
.toString()
.split('\n')
.first}",
onDismissed: () {
_controller.resumeCamera();
},
error: "Server Error",
icon: FontAwesomeIcons.server,
);
return;
}
// Decode the response
final Map<String, dynamic> data = json.decode(response.body);
if (data.containsKey('error')) {
onBarcodeUnknown(data);
} else if (data.containsKey('success')) {
onBarcodeMatched(data);
} else {
onBarcodeUnhandled(data);
}
}).timeout(
Duration(seconds: 5)
).catchError((error) {
hideProgressDialog(context);
showErrorDialog(
context,
"Error",
error.toString(),
onDismissed: () {
_controller.resumeCamera();
}
);
return;
});
}
}
class BarcodeScanHandler extends BarcodeHandler {
/**
* Class for general barcode scanning.
* Scan *any* barcode without context, and then redirect app to correct view
*/
@override
Future<void> onBarcodeUnknown(Map<String, dynamic> data) {
showErrorDialog(
_context,
data['error'] ?? '',
data['plugin'] ?? 'No barcode plugin information',
error: "Barcode Error",
icon: FontAwesomeIcons.barcode,
onDismissed: () {
_controller.resumeCamera();
}
);
}
@override
Future<void> onBarcodeMatched(Map<String, dynamic> data) {
int pk; int pk;
print("Handle barcode:"); print("Handle barcode:");
print(response); print(data);
// A stocklocation has been passed? // A stocklocation has been passed?
if (response.containsKey('stocklocation')) { if (data.containsKey('stocklocation')) {
pk = response['stocklocation']['pk'] as int ?? null; pk = data['stocklocation']['pk'] as int ?? null;
if (pk != null) { if (pk != null) {
InvenTreeStockLocation().get(context, pk).then((var loc) { InvenTreeStockLocation().get(_context, pk).then((var loc) {
if (loc is InvenTreeStockLocation) { if (loc is InvenTreeStockLocation) {
Navigator.of(context).pop(); Navigator.of(_context).pop();
Navigator.push(context, MaterialPageRoute(builder: (context) => LocationDisplayWidget(loc))); Navigator.push(_context, MaterialPageRoute(builder: (context) => LocationDisplayWidget(loc)));
} }
}); });
} else { } else {
// TODO - Show an error here! // TODO - Show an error here!
} }
} else if (response.containsKey('stockitem')) { } else if (data.containsKey('stockitem')) {
pk = response['stockitem']['pk'] as int ?? null; pk = data['stockitem']['pk'] as int ?? null;
if (pk != null) { if (pk != null) {
InvenTreeStockItem().get(context, pk).then((var item) { InvenTreeStockItem().get(_context, pk).then((var item) {
Navigator.of(context).pop(); Navigator.of(_context).pop();
Navigator.push(context, MaterialPageRoute(builder: (context) => StockDetailWidget(item))); Navigator.push(_context, MaterialPageRoute(builder: (context) => StockDetailWidget(item)));
}); });
} else { } else {
// TODO - Show an error here! // TODO - Show an error here!
} }
} else if (response.containsKey('part')) { } else if (data.containsKey('part')) {
pk = response['part']['pk'] as int ?? null; pk = data['part']['pk'] as int ?? null;
if (pk != null) { if (pk != null) {
InvenTreePart().get(context, pk).then((var part) { InvenTreePart().get(_context, pk).then((var part) {
Navigator.of(context).pop(); Navigator.of(_context).pop();
Navigator.push(context, MaterialPageRoute(builder: (context) => PartDetailWidget(part))); Navigator.push(_context, MaterialPageRoute(builder: (context) => PartDetailWidget(part)));
}); });
} else { } else {
// TODO - Show an error here! // TODO - Show an error here!
} }
} else { } else {
showDialog( showDialog(
context: context, context: _context,
child: SimpleDialog( child: SimpleDialog(
title: Text("Unknown response"), title: Text("Unknown response"),
children: <Widget>[ children: <Widget>[
ListTile( ListTile(
title: Text("Response data"), title: Text("Response data"),
subtitle: Text(response.toString()), subtitle: Text(data.toString()),
) )
], ],
) )
); );
} }
} }
}
class StockItemBarcodeAssignmentHandler extends BarcodeHandler {
/**
* Barcode handler for assigning a new barcode to a stock item
*/
final InvenTreeStockItem item;
StockItemBarcodeAssignmentHandler(this.item);
// Callback when the server responds with no match for the barcode @override
Future<void> onBarcodeUnknown(Map<String, dynamic> response) { Future<void> onBarcodeMatched(Map<String, dynamic> data) {
// If the barcode is known, we can't asisgn it to the stock item!
showErrorDialog( showErrorDialog(
context, _context,
response['error'] ?? '', "Barcode in Use",
response['plugin'] ?? 'No barcode plugin information', "Barcode is already known",
error: "Barcode Error", onDismissed: () {
icon: FontAwesomeIcons.barcode, _controller.resumeCamera();
onDismissed: () { }
_controller.resumeCamera();
}
); );
} }
// Callback when the server responds with an unhandled response @override
Future<void> onBarcodeUnhandled(Map<String, dynamic> response) { Future<void> onBarcodeUnknown(Map<String, dynamic> data) {
showErrorDialog( // If the barcode is unknown, we *can* assign it to the stock item!
context,
"Response Data", if (!data.containsKey("hash")) {
response.toString(), showErrorDialog(
error: "Unknown Response", _context,
"Missing Data",
"Missing hash data from server",
onDismissed: () { onDismissed: () {
_controller.resumeCamera(); _controller.resumeCamera();
} }
); );
} else {
// Send the 'hash' code as the UID for the stock item
item.update(
_context,
values: {
"uid": data['hash'],
}
).then((result) {
if (result) {
showInfoDialog(
_context,
"Barcode Set",
"Barcode assigned to stock item",
onDismissed: () {
_controller.dispose();
Navigator.of(_context).pop();
}
);
} else {
showErrorDialog(
_context,
"Server Error",
"Could not assign barcode",
onDismissed: () {
_controller.resumeCamera();
}
);
}
});
}
} }
}
Future<void> processBarcode(String barcode) async {
if (barcode == null || barcode.isEmpty) {
return;
}
print("Scanned: ${barcode}"); class InvenTreeQRView extends StatefulWidget {
showProgressDialog(context, "Querying server", "Sending barcode data to server");
InvenTreeAPI().post("barcode/", body: {"barcode": barcode}).then((var response) { final BarcodeHandler _handler;
hideProgressDialog(context);
print("Response:"); InvenTreeQRView(this._handler, {Key key}) : super(key: key);
print(response.body);
if (response.statusCode != 200) { @override
State<StatefulWidget> createState() => _QRViewState(_handler);
}
showErrorDialog(
context,
"Status Code: ${response.statusCode}",
"${response.body.toString().split('\n').first}",
onDismissed: () {
_controller.resumeCamera();
},
error: "Server Error",
icon: FontAwesomeIcons.server,
);
return; class _QRViewState extends State<InvenTreeQRView> {
}
// Decode the response QRViewController _controller;
final Map<String, dynamic> body = json.decode(response.body);
// "Error" contained in response final BarcodeHandler _handler;
if (body.containsKey('error')) {
onBarcodeUnknown(body);
} else if (body.containsKey('success')) {
onBarcodeMatched(body);
} else {
onBarcodeUnhandled(body);
}
}).timeout( BuildContext context;
Duration(seconds: 5)
).catchError((error) {
hideProgressDialog(context);
showErrorDialog(
context,
"Error",
error.toString(),
onDismissed: () {
_controller.resumeCamera();
}
);
return;
});
} _QRViewState(this._handler) : super();
final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');
void _onViewCreated(QRViewController controller) { void _onViewCreated(QRViewController controller) {
_controller = controller; _controller = controller;
controller.scannedDataStream.listen((scandata) { controller.scannedDataStream.listen((scandata) {
_controller?.pauseCamera(); _controller?.pauseCamera();
processBarcode(scandata); _handler.processBarcode(context, _controller, scandata);
}); });
} }
@ -236,7 +341,7 @@ class _QRViewState extends State<InvenTreeQRView> {
Future<void> scanQrCode(BuildContext context) async { Future<void> scanQrCode(BuildContext context) async {
Navigator.push(context, MaterialPageRoute(builder: (context) => InvenTreeQRView())); Navigator.push(context, MaterialPageRoute(builder: (context) => InvenTreeQRView(BarcodeScanHandler())));
return; return;
} }

View File

@ -168,6 +168,8 @@ class InvenTreeStockItem extends InvenTreeModel {
return false; return false;
} }
String get uid => jsondata['uid'] ?? '';
int get partId => jsondata['part'] ?? -1; int get partId => jsondata['part'] ?? -1;
int get trackingItemCount => jsondata['tracking_items'] as int ?? 0; int get trackingItemCount => jsondata['tracking_items'] as int ?? 0;

View File

@ -9,6 +9,28 @@ void showMessage(BuildContext context, String message) {
)); ));
} }
Future<void> showInfoDialog(BuildContext context, String title, String description, {IconData icon = FontAwesomeIcons.info, String info = "Info", Function onDismissed}) async {
showDialog(
context: context,
child: SimpleDialog(
title: ListTile(
title: Text(info),
leading: FaIcon(icon),
),
children: <Widget>[
ListTile(
title: Text(title),
subtitle: Text(description)
)
]
)
).then((value) {
if (onDismissed != null) {
onDismissed();
}
});
}
Future<void> showErrorDialog(BuildContext context, String title, String description, {IconData icon = FontAwesomeIcons.exclamationCircle, String error = "Error", Function onDismissed}) async { Future<void> showErrorDialog(BuildContext context, String title, String description, {IconData icon = FontAwesomeIcons.exclamationCircle, String error = "Error", Function onDismissed}) async {
showDialog( showDialog(
context: context, context: context,

View File

@ -1,5 +1,6 @@
import 'package:InvenTree/barcode.dart';
import 'package:InvenTree/inventree/stock.dart'; import 'package:InvenTree/inventree/stock.dart';
import 'package:InvenTree/inventree/part.dart'; import 'package:InvenTree/inventree/part.dart';
import 'package:InvenTree/widget/dialogs.dart'; import 'package:InvenTree/widget/dialogs.dart';
@ -374,9 +375,22 @@ class _StockItemDisplayState extends RefreshableState<StockDetailWidget> {
trailing: Text("${item.quantity}"), trailing: Text("${item.quantity}"),
) )
); );
} }
tiles.add(
ListTile(
title: Text("Add Barcode"),
leading: FaIcon(FontAwesomeIcons.qrcode),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => InvenTreeQRView(StockItemBarcodeAssignmentHandler(item)))
);
//Navigator.push(context, MaterialPageRoute(builder: (context) => AssignBarcodeToStockItemView(item)));
},
)
);
// Location information // Location information
if ((item.locationId > 0) && (item.locationName != null) && (item.locationName.isNotEmpty)) { if ((item.locationId > 0) && (item.locationName != null) && (item.locationName.isNotEmpty)) {
tiles.add( tiles.add(