From 0a64d60ef553641b972b69d3f074d2ab058abb1b Mon Sep 17 00:00:00 2001 From: monsieurtanuki Date: Sat, 2 Sep 2023 16:10:09 +0200 Subject: [PATCH] fix: 4601 - more robust management of product pending changes (#4602) Impacted files: * `add_new_product_page.dart`: moved the call to `initUpToDate` on top * `edit_new_packagings.dart`: uses `upToDateProduct` and not `initialProduct` anymore * `nutrition_page_loaded.dart`: uses `upToDateProduct` and not `initialProduct` anymore * `summary_card.dart`: uses `upToDateProduct` and not `initialProduct` anymore * `up_to_date_mixin.dart`: computes the `upToDateProduct` asap; makes `initialProduct` private --- .../lib/data_models/up_to_date_mixin.dart | 15 +++++++++------ .../lib/pages/product/add_new_product_page.dart | 4 ++-- .../lib/pages/product/edit_new_packagings.dart | 6 +++--- .../lib/pages/product/nutrition_page_loaded.dart | 2 +- .../lib/pages/product/summary_card.dart | 2 +- 5 files changed, 16 insertions(+), 13 deletions(-) diff --git a/packages/smooth_app/lib/data_models/up_to_date_mixin.dart b/packages/smooth_app/lib/data_models/up_to_date_mixin.dart index 1e9941c17f3..3ffb766e4ef 100644 --- a/packages/smooth_app/lib/data_models/up_to_date_mixin.dart +++ b/packages/smooth_app/lib/data_models/up_to_date_mixin.dart @@ -15,25 +15,25 @@ import 'package:smooth_app/database/local_database.dart'; /// * we track the barcodes currently "opened" by the app @optionalTypeArgs mixin UpToDateMixin on State { - /// To be used in the `initState` method. + /// To be used in the `initState` method, just after `super.initState();`. void initUpToDate( final Product initialProduct, final LocalDatabase localDatabase, ) { - this.initialProduct = initialProduct; + _initialProduct = initialProduct; _localDatabase = localDatabase; + _refreshUpToDate(); localDatabase.upToDate.showInterest(barcode); } - @protected - late final Product initialProduct; + late final Product _initialProduct; late final LocalDatabase _localDatabase; late Product _product; @protected - String get barcode => initialProduct.barcode!; + String get barcode => _initialProduct.barcode!; @protected Product get upToDateProduct => _product; @@ -50,6 +50,9 @@ mixin UpToDateMixin on State { /// `context.watch()`. void refreshUpToDate() { BackgroundTaskManager.getInstance(_localDatabase).run(); // no await - _product = _localDatabase.upToDate.getLocalUpToDate(initialProduct); + _refreshUpToDate(); } + + void _refreshUpToDate() => + _product = _localDatabase.upToDate.getLocalUpToDate(_initialProduct); } diff --git a/packages/smooth_app/lib/pages/product/add_new_product_page.dart b/packages/smooth_app/lib/pages/product/add_new_product_page.dart index fe1454ed62f..88abbd7bd47 100644 --- a/packages/smooth_app/lib/pages/product/add_new_product_page.dart +++ b/packages/smooth_app/lib/pages/product/add_new_product_page.dart @@ -110,6 +110,8 @@ class _AddNewProductPageState extends State @override void initState() { super.initState(); + final LocalDatabase localDatabase = context.read(); + initUpToDate(widget.product, localDatabase); _editors = [ _packagingEditor, _ingredientsEditor, @@ -142,8 +144,6 @@ class _AddNewProductPageState extends State _otherCount > 0 || _helper.isOneMainImagePopulated(upToDateProduct), ), ]; - final LocalDatabase localDatabase = context.read(); - initUpToDate(widget.product, localDatabase); _daoProductList = DaoProductList(localDatabase); AnalyticsHelper.trackEvent( widget.events[EditProductAction.openPage]!, diff --git a/packages/smooth_app/lib/pages/product/edit_new_packagings.dart b/packages/smooth_app/lib/pages/product/edit_new_packagings.dart index 57c0a86e011..742499d82cf 100644 --- a/packages/smooth_app/lib/pages/product/edit_new_packagings.dart +++ b/packages/smooth_app/lib/pages/product/edit_new_packagings.dart @@ -75,10 +75,10 @@ class _EditNewPackagingsState extends State _unitNumberFormat = SimpleInputNumberField.getNumberFormat( decimal: false, ); - if (initialProduct.packagings != null) { - initialProduct.packagings!.forEach(_addPackagingToControllers); + if (upToDateProduct.packagings != null) { + upToDateProduct.packagings!.forEach(_addPackagingToControllers); } - _packagingsComplete = initialProduct.packagingsComplete; + _packagingsComplete = upToDateProduct.packagingsComplete; } @override diff --git a/packages/smooth_app/lib/pages/product/nutrition_page_loaded.dart b/packages/smooth_app/lib/pages/product/nutrition_page_loaded.dart index eb522c2961b..eec00fe6e19 100644 --- a/packages/smooth_app/lib/pages/product/nutrition_page_loaded.dart +++ b/packages/smooth_app/lib/pages/product/nutrition_page_loaded.dart @@ -98,7 +98,7 @@ class _NutritionPageLoadedState extends State initUpToDate(widget.product, context.read()); _nutritionContainer = NutritionContainer( orderedNutrients: widget.orderedNutrients, - product: initialProduct, + product: upToDateProduct, ); _decimalNumberFormat = diff --git a/packages/smooth_app/lib/pages/product/summary_card.dart b/packages/smooth_app/lib/pages/product/summary_card.dart index 7df9eb02455..c909e265e39 100644 --- a/packages/smooth_app/lib/pages/product/summary_card.dart +++ b/packages/smooth_app/lib/pages/product/summary_card.dart @@ -92,7 +92,7 @@ class _SummaryCardState extends State with UpToDateMixin { super.initState(); initUpToDate(widget._product, context.read()); _questionsLayout = getUserQuestionsLayout(context.read()); - if (ProductIncompleteCard.isProductIncomplete(initialProduct)) { + if (ProductIncompleteCard.isProductIncomplete(upToDateProduct)) { AnalyticsHelper.trackEvent( AnalyticsEvent.showFastTrackProductEditCard, barcode: barcode,