From d84f76d482c599e79b0b4721e62b90f2b5242119 Mon Sep 17 00:00:00 2001 From: Oliver Date: Mon, 23 Dec 2024 10:36:36 +1100 Subject: [PATCH] Improve error management for label printing (#588) - Handle empty label - Handle empty printer --- lib/api_form.dart | 11 +++++++++++ lib/l10n/app_en.arb | 6 ++++++ lib/labels.dart | 22 ++++++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/lib/api_form.dart b/lib/api_form.dart index 7b8cb426..a906d908 100644 --- a/lib/api_form.dart +++ b/lib/api_form.dart @@ -982,6 +982,7 @@ Future launchApiForm( Map modelData = const {}, String method = "PATCH", Function(Map)? onSuccess, + bool Function(Map)? validate, Function? onCancel, IconData icon = TablerIcons.device_floppy }) async { @@ -1071,6 +1072,7 @@ Future launchApiForm( formFields, method, onSuccess: onSuccess, + validate: validate, fileField: fileField, icon: icon, )) @@ -1088,6 +1090,7 @@ class APIFormWidget extends StatefulWidget { { Key? key, this.onSuccess, + this.validate, this.fileField = "", this.icon = TablerIcons.device_floppy, } @@ -1111,6 +1114,8 @@ class APIFormWidget extends StatefulWidget { final Function(Map)? onSuccess; + final bool Function(Map)? validate; + @override _APIFormWidgetState createState() => _APIFormWidgetState(); @@ -1401,6 +1406,12 @@ class _APIFormWidgetState extends State { } } } + + final bool isValid = widget.validate?.call(data) ?? true; + + if (!isValid) { + return; + } // Run custom onSuccess function var successFunc = widget.onSuccess; diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index ef3b9d96..8ea01ad6 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -623,6 +623,12 @@ "labelTemplate": "Label Template", "@labelTemplate": {}, + "labelSelectTemplate": "Select Label Template", + "@labelSelectTemplate": {}, + + "labelSelectPrinter": "Select Label Printer", + "@labelSelectPrinter": {}, + "language": "Language", "@language": {}, diff --git a/lib/labels.dart b/lib/labels.dart index 819df713..ca145e91 100644 --- a/lib/labels.dart +++ b/lib/labels.dart @@ -94,6 +94,28 @@ Future selectAndPrintLabel( "", fields, icon: TablerIcons.printer, + validate: (Map data) { + final template = data["label"]; + final plugin = data["plugin"]; + + if (template == null) { + showSnackIcon( + L10().labelSelectTemplate, + success: false, + ); + return false; + } + + if (plugin == null) { + showSnackIcon( + L10().labelSelectPrinter, + success: false, + ); + return false; + } + + return true; + }, onSuccess: (Map data) async { int labelId = (data["label"] ?? -1) as int; var pluginKey = data["plugin"];