Skip to content

Commit

Permalink
Improve error management for label printing (#588)
Browse files Browse the repository at this point in the history
- Handle empty label
- Handle empty printer
  • Loading branch information
SchrodingersGat authored Dec 22, 2024
1 parent bc44b99 commit d84f76d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/api_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,7 @@ Future<void> launchApiForm(
Map<String, dynamic> modelData = const {},
String method = "PATCH",
Function(Map<String, dynamic>)? onSuccess,
bool Function(Map<String, dynamic>)? validate,
Function? onCancel,
IconData icon = TablerIcons.device_floppy
}) async {
Expand Down Expand Up @@ -1071,6 +1072,7 @@ Future<void> launchApiForm(
formFields,
method,
onSuccess: onSuccess,
validate: validate,
fileField: fileField,
icon: icon,
))
Expand All @@ -1088,6 +1090,7 @@ class APIFormWidget extends StatefulWidget {
{
Key? key,
this.onSuccess,
this.validate,
this.fileField = "",
this.icon = TablerIcons.device_floppy,
}
Expand All @@ -1111,6 +1114,8 @@ class APIFormWidget extends StatefulWidget {

final Function(Map<String, dynamic>)? onSuccess;

final bool Function(Map<String, dynamic>)? validate;

@override
_APIFormWidgetState createState() => _APIFormWidgetState();

Expand Down Expand Up @@ -1401,6 +1406,12 @@ class _APIFormWidgetState extends State<APIFormWidget> {
}
}
}

final bool isValid = widget.validate?.call(data) ?? true;

if (!isValid) {
return;
}

// Run custom onSuccess function
var successFunc = widget.onSuccess;
Expand Down
6 changes: 6 additions & 0 deletions lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,12 @@
"labelTemplate": "Label Template",
"@labelTemplate": {},

"labelSelectTemplate": "Select Label Template",
"@labelSelectTemplate": {},

"labelSelectPrinter": "Select Label Printer",
"@labelSelectPrinter": {},

"language": "Language",
"@language": {},

Expand Down
22 changes: 22 additions & 0 deletions lib/labels.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,28 @@ Future<void> selectAndPrintLabel(
"",
fields,
icon: TablerIcons.printer,
validate: (Map<String, dynamic> 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<String, dynamic> data) async {
int labelId = (data["label"] ?? -1) as int;
var pluginKey = data["plugin"];
Expand Down

0 comments on commit d84f76d

Please sign in to comment.