From bc44b99d43a67b7587effce7b69e0fa256a5d6a4 Mon Sep 17 00:00:00 2001 From: Oliver Date: Mon, 23 Dec 2024 09:57:13 +1100 Subject: [PATCH] Label printing fix (#587) * Handle blank URL provided for file download * Improved printing checks * Auto-select the correct printer --- assets/release_notes.md | 1 + lib/api.dart | 7 ++++++- lib/inventree/model.dart | 2 +- lib/labels.dart | 22 +++++++++++++++------- lib/widget/progress.dart | 4 +++- 5 files changed, 26 insertions(+), 10 deletions(-) diff --git a/assets/release_notes.md b/assets/release_notes.md index 91c6b49a..3b1237cc 100644 --- a/assets/release_notes.md +++ b/assets/release_notes.md @@ -1,6 +1,7 @@ ### 0.17.2 - December 2024 --- +- Fixed error message when printing a label to a remote machine - Prevent notification sounds from pause media playback - Updated translations diff --git a/lib/api.dart b/lib/api.dart index d686e1b1..24e8c1fd 100644 --- a/lib/api.dart +++ b/lib/api.dart @@ -817,6 +817,11 @@ class InvenTreeAPI { */ Future downloadFile(String url, {bool openOnDownload = true}) async { + if (url.isEmpty) { + // No URL provided for download + return; + } + // Find the local downlods directory final Directory dir = await getTemporaryDirectory(); @@ -1538,7 +1543,7 @@ class InvenTreeAPI { return setting.value; } - final response = await InvenTreeGlobalSetting().getModel(key); + final response = await InvenTreeUserSetting().getModel(key); if (response is InvenTreeUserSetting) { response.lastReload = DateTime.now(); diff --git a/lib/inventree/model.dart b/lib/inventree/model.dart index 2078f86d..fc90a2ba 100644 --- a/lib/inventree/model.dart +++ b/lib/inventree/model.dart @@ -904,7 +904,7 @@ class InvenTreeUserSetting extends InvenTreeGlobalSetting { @override InvenTreeGlobalSetting createFromJson(Map json) { - return InvenTreeGlobalSetting.fromJson(json); + return InvenTreeUserSetting.fromJson(json); } @override diff --git a/lib/labels.dart b/lib/labels.dart index 1e15bd4a..819df713 100644 --- a/lib/labels.dart +++ b/lib/labels.dart @@ -63,7 +63,11 @@ Future selectAndPrintLabel( }); } - if (plugin_options.length == 1) { + String selectedPlugin = await InvenTreeAPI().getUserSetting("LABEL_DEFAULT_PRINTER"); + + if (selectedPlugin.isNotEmpty) { + initial_plugin = selectedPlugin; + } else if (plugin_options.length == 1) { initial_plugin = plugin_options.first["value"]; } @@ -111,27 +115,29 @@ Future selectAndPrintLabel( "items": [instanceId] } ).then((APIResponse response) { - hideLoadingOverlay(); if (response.isValid() && response.statusCode >= 200 && response.statusCode <= 201) { var data = response.asMap(); if (data.containsKey("output")) { - var label_file = (data["output"] ?? "") as String; + String? label_file = (data["output"]) as String?; + + if (label_file != null && label_file.isNotEmpty) { + // Attempt to open generated file + InvenTreeAPI().downloadFile(label_file); + } - // Attempt to open generated file - InvenTreeAPI().downloadFile(label_file); result = true; } } }); - } else { + + } else { // Legacy label printing API // Uses a GET request to a specially formed URL which depends on the parameters String url = "/label/${labelType}/${labelId}/print/?${labelQuery}&plugin=${pluginKey}"; await InvenTreeAPI().get(url).then((APIResponse response) { - hideLoadingOverlay(); if (response.isValid() && response.statusCode == 200) { var data = response.asMap(); if (data.containsKey("file")) { @@ -145,6 +151,8 @@ Future selectAndPrintLabel( }); } + hideLoadingOverlay(); + if (result) { showSnackIcon( L10().printLabelSuccess, diff --git a/lib/widget/progress.dart b/lib/widget/progress.dart index cc00c85b..3deb9387 100644 --- a/lib/widget/progress.dart +++ b/lib/widget/progress.dart @@ -66,5 +66,7 @@ void showLoadingOverlay() { void hideLoadingOverlay() { - Loader.hide(); + if (Loader.isShown) { + Loader.hide(); + } }