From a0fb228ebb62c92f0428b5a55650c66ab0e465f1 Mon Sep 17 00:00:00 2001 From: Edouard Marquez Date: Mon, 20 Jan 2025 19:20:33 +0100 Subject: [PATCH] When leaving an edit page with a `SimpleInputWidget`, ensure it will be saved and restored correctly (#6250) --- .../pages/product/add_basic_details_page.dart | 5 +++++ .../lib/pages/product/simple_input_page.dart | 8 ++++++- .../product/simple_input_page_helpers.dart | 21 +++++++++++++++++-- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/packages/smooth_app/lib/pages/product/add_basic_details_page.dart b/packages/smooth_app/lib/pages/product/add_basic_details_page.dart index 653a1a299e0f..06f3c8296f77 100644 --- a/packages/smooth_app/lib/pages/product/add_basic_details_page.dart +++ b/packages/smooth_app/lib/pages/product/add_basic_details_page.dart @@ -251,6 +251,7 @@ class _AddBasicDetailsPageState extends State { final bool? pleaseSave = await MayExitPageHelper().openSaveBeforeLeavingDialog(context); if (pleaseSave == null) { + _brandsHelper.restoreItemsBeforeLastAddition(); return false; } if (pleaseSave == false) { @@ -300,6 +301,10 @@ class _AddBasicDetailsPageState extends State { hasChanged = true; } + _brandsHelper.addItemsFromController( + _brandsController, + clearController: false, + ); if (_brandsHelper.getChangedProduct(result)) { hasChanged = true; } diff --git a/packages/smooth_app/lib/pages/product/simple_input_page.dart b/packages/smooth_app/lib/pages/product/simple_input_page.dart index 154d1ecf4ccc..114bcf6b0f71 100644 --- a/packages/smooth_app/lib/pages/product/simple_input_page.dart +++ b/packages/smooth_app/lib/pages/product/simple_input_page.dart @@ -139,7 +139,10 @@ class _SimpleInputPageState extends State { bool added = false; for (int i = 0; i < widget.helpers.length; i++) { final AbstractSimpleInputPageHelper helper = widget.helpers[i]; - if (helper.addItemsFromController(_controllers[i])) { + if (helper.addItemsFromController( + _controllers[i], + clearController: false, + )) { added = true; } final Product changedProduct = Product(barcode: widget.product.barcode); @@ -158,6 +161,9 @@ class _SimpleInputPageState extends State { final bool? pleaseSave = await MayExitPageHelper().openSaveBeforeLeavingDialog(context); if (pleaseSave == null) { + for (int i = 0; i < widget.helpers.length; i++) { + widget.helpers[i].restoreItemsBeforeLastAddition(); + } return false; } if (pleaseSave == false) { diff --git a/packages/smooth_app/lib/pages/product/simple_input_page_helpers.dart b/packages/smooth_app/lib/pages/product/simple_input_page_helpers.dart index 78b6240e585e..6dde04ff19f0 100644 --- a/packages/smooth_app/lib/pages/product/simple_input_page_helpers.dart +++ b/packages/smooth_app/lib/pages/product/simple_input_page_helpers.dart @@ -194,10 +194,17 @@ abstract class AbstractSimpleInputPageHelper extends ChangeNotifier { @protected OpenFoodFactsLanguage getLanguage() => ProductQuery.getLanguage(); + List? _itemsBeforeLastAddition; + /// Adds all the non-already existing items from the controller. /// /// The item separator is the comma. - bool addItemsFromController(final TextEditingController controller) { + bool addItemsFromController( + final TextEditingController controller, { + bool clearController = true, + }) { + _itemsBeforeLastAddition = List.from(_terms); + final List input = controller.text.split(','); bool result = false; for (final String item in input) { @@ -205,12 +212,22 @@ abstract class AbstractSimpleInputPageHelper extends ChangeNotifier { result = true; } } - if (result) { + if (result && clearController) { controller.text = ''; } return result; } + void restoreItemsBeforeLastAddition() { + if (_itemsBeforeLastAddition == null) { + return; + } + _terms = List.from(_itemsBeforeLastAddition!); + _changed = true; + _itemsBeforeLastAddition = null; + notifyListeners(); + } + /// Mainly used when reordering the list. void replaceItems(final List items) { _terms = List.of(items);