From 4f8794e6528c6d623892f9bc48037e7ff4720cbe Mon Sep 17 00:00:00 2001 From: Oliver Date: Sun, 3 Oct 2021 12:04:01 +1100 Subject: [PATCH] Refactor barcode scann function - Provide a custom callback to be executed when the barcode is scanned --- lib/api_form.dart | 7 +++ lib/barcode.dart | 110 ++++------------------------------- lib/helpers.dart | 24 ++++++++ lib/widget/stock_detail.dart | 31 ++++++++-- 4 files changed, 67 insertions(+), 105 deletions(-) diff --git a/lib/api_form.dart b/lib/api_form.dart index 1a7237f6..799f1864 100644 --- a/lib/api_form.dart +++ b/lib/api_form.dart @@ -335,6 +335,13 @@ class APIFormField { var handler = UniqueBarcodeHandler((String hash) { controller.text = hash; data["value"] = hash; + + successTone(); + + showSnackIcon( + L10().barcodeAssigned, + success: true + ); }); Navigator.push( diff --git a/lib/barcode.dart b/lib/barcode.dart index 76bb4fe2..aeeba495 100644 --- a/lib/barcode.dart +++ b/lib/barcode.dart @@ -1,10 +1,8 @@ import "dart:io"; -import "package:inventree/app_settings.dart"; import "package:inventree/inventree/sentry.dart"; import "package:inventree/widget/dialogs.dart"; import "package:inventree/widget/snacks.dart"; -import "package:audioplayers/audioplayers.dart"; import "package:flutter/cupertino.dart"; import "package:flutter/material.dart"; import "package:font_awesome_flutter/font_awesome_flutter.dart"; @@ -15,7 +13,7 @@ import "package:qr_code_scanner/qr_code_scanner.dart"; import "package:inventree/inventree/stock.dart"; import "package:inventree/inventree/part.dart"; import "package:inventree/l10.dart"; - +import "package:inventree/helpers.dart"; import "package:inventree/api.dart"; import "package:inventree/widget/location_display.dart"; @@ -38,26 +36,6 @@ class BarcodeHandler { QRViewController? _controller; - Future successTone() async { - - final bool en = await InvenTreeSettingsManager().getValue("barcodeSounds", true) as bool; - - if (en) { - final player = AudioCache(); - player.play("sounds/barcode_scan.mp3"); - } - } - - Future failureTone() async { - - final bool en = await InvenTreeSettingsManager().getValue("barcodeSounds", true) as bool; - - if (en) { - final player = AudioCache(); - player.play("sounds/barcode_error.mp3"); - } - } - Future onBarcodeMatched(BuildContext context, Map data) async { // Called when the server "matches" a barcode // Override this function @@ -260,75 +238,6 @@ class BarcodeScanHandler extends BarcodeHandler { } } - -class StockItemBarcodeAssignmentHandler extends BarcodeHandler { - /* - * Barcode handler for assigning a new barcode to a stock item - */ - - StockItemBarcodeAssignmentHandler(this.item); - - final InvenTreeStockItem item; - - @override - String getOverlayText(BuildContext context) => L10().barcodeScanAssign; - - @override - Future onBarcodeMatched(BuildContext context, Map data) async { - - failureTone(); - - // If the barcode is known, we can"t assign it to the stock item! - showSnackIcon( - L10().barcodeInUse, - icon: FontAwesomeIcons.qrcode, - success: false - ); - } - - @override - Future onBarcodeUnknown(BuildContext context, Map data) async { - // If the barcode is unknown, we *can* assign it to the stock item! - - if (!data.containsKey("hash")) { - showServerError( - L10().missingData, - L10().barcodeMissingHash, - ); - } else { - String hash = (data["hash"] ?? "") as String; - - if (hash.isNotEmpty) { - item.update( - values: { - "uid": hash, - } - ).then((result) { - if (result) { - successTone(); - - Navigator.of(context).pop(); - - showSnackIcon( - L10().barcodeAssigned, - success: true, - icon: FontAwesomeIcons.qrcode - ); - } else { - failureTone(); - - showSnackIcon( - L10().barcodeNotAssigned, - success: false, - icon: FontAwesomeIcons.qrcode - ); - } - }); - } - } - } -} - class StockItemScanIntoLocationHandler extends BarcodeHandler { /* * Barcode handler for scanning a provided StockItem into a scanned StockLocation @@ -469,13 +378,21 @@ class UniqueBarcodeHandler extends BarcodeHandler { * Barcode handler for finding a "unique" barcode (one that does not match an item in the database) */ - UniqueBarcodeHandler(this.callback); + UniqueBarcodeHandler(this.callback, {this.overlayText = ""}); // Callback function when a "unique" barcode hash is found final Function(String) callback; + final String overlayText; + @override - String getOverlayText(BuildContext context) => L10().barcodeScanAssign; + String getOverlayText(BuildContext context) { + if (overlayText.isEmpty) { + return L10().barcodeScanAssign; + } else { + return overlayText; + } + } @override Future onBarcodeMatched(BuildContext context, Map data) async { @@ -516,11 +433,6 @@ class UniqueBarcodeHandler extends BarcodeHandler { // Close the barcode scanner Navigator.of(context).pop(); - showSnackIcon( - L10().barcodeAssigned, - success: true - ); - callback(hash); } } diff --git a/lib/helpers.dart b/lib/helpers.dart index 942c98ec..8b3e1764 100644 --- a/lib/helpers.dart +++ b/lib/helpers.dart @@ -6,8 +6,32 @@ * Simplify a numerical value into a string, * supressing trailing zeroes */ + +import "package:audioplayers/audioplayers.dart"; +import "package:inventree/app_settings.dart"; + String simpleNumberString(double number) { // Ref: https://stackoverflow.com/questions/55152175/how-to-remove-trailing-zeros-using-dart return number.toStringAsFixed(number.truncateToDouble() == number ? 0 : 1); +} + +Future successTone() async { + + final bool en = await InvenTreeSettingsManager().getValue("barcodeSounds", true) as bool; + + if (en) { + final player = AudioCache(); + player.play("sounds/barcode_scan.mp3"); + } +} + +Future failureTone() async { + + final bool en = await InvenTreeSettingsManager().getValue("barcodeSounds", true) as bool; + + if (en) { + final player = AudioCache(); + player.play("sounds/barcode_error.mp3"); + } } \ No newline at end of file diff --git a/lib/widget/stock_detail.dart b/lib/widget/stock_detail.dart index 72e6e5e4..46e49cfb 100644 --- a/lib/widget/stock_detail.dart +++ b/lib/widget/stock_detail.dart @@ -16,7 +16,7 @@ import "package:flutter/cupertino.dart"; import "package:flutter/material.dart"; import "package:inventree/l10.dart"; - +import "package:inventree/helpers.dart"; import "package:inventree/api.dart"; import "package:dropdown_search/dropdown_search.dart"; @@ -676,12 +676,31 @@ class _StockItemDisplayState extends RefreshableState { leading: FaIcon(FontAwesomeIcons.barcode, color: COLOR_CLICK), trailing: FaIcon(FontAwesomeIcons.qrcode), onTap: () { - Navigator.push( - context, - MaterialPageRoute(builder: (context) => InvenTreeQRView(StockItemBarcodeAssignmentHandler(item))) - ).then((context) { - refresh(); + + var handler = UniqueBarcodeHandler((String hash) { + item.update( + values: { + "uid": hash, + } + ).then((result) { + if (result) { + successTone(); + + showSnackIcon( + L10().barcodeAssigned, + success: true, + icon: FontAwesomeIcons.qrcode + ); + + refresh(); + } + }); }); + + Navigator.push( + context, + MaterialPageRoute(builder: (context) => InvenTreeQRView(handler)) + ); } ) );