Skip to content

Commit

Permalink
fix: 4601 - more robust management of product pending changes (#4602)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
monsieurtanuki committed Sep 2, 2023
1 parent c71e0b9 commit 0a64d60
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 13 deletions.
15 changes: 9 additions & 6 deletions packages/smooth_app/lib/data_models/up_to_date_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@ import 'package:smooth_app/database/local_database.dart';
/// * we track the barcodes currently "opened" by the app
@optionalTypeArgs
mixin UpToDateMixin<T extends StatefulWidget> on State<T> {
/// 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;
Expand All @@ -50,6 +50,9 @@ mixin UpToDateMixin<T extends StatefulWidget> on State<T> {
/// `context.watch<LocalDatabase>()`.
void refreshUpToDate() {
BackgroundTaskManager.getInstance(_localDatabase).run(); // no await
_product = _localDatabase.upToDate.getLocalUpToDate(initialProduct);
_refreshUpToDate();
}

void _refreshUpToDate() =>
_product = _localDatabase.upToDate.getLocalUpToDate(_initialProduct);
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ class _AddNewProductPageState extends State<AddNewProductPage>
@override
void initState() {
super.initState();
final LocalDatabase localDatabase = context.read<LocalDatabase>();
initUpToDate(widget.product, localDatabase);
_editors = <ProductFieldEditor>[
_packagingEditor,
_ingredientsEditor,
Expand Down Expand Up @@ -142,8 +144,6 @@ class _AddNewProductPageState extends State<AddNewProductPage>
_otherCount > 0 || _helper.isOneMainImagePopulated(upToDateProduct),
),
];
final LocalDatabase localDatabase = context.read<LocalDatabase>();
initUpToDate(widget.product, localDatabase);
_daoProductList = DaoProductList(localDatabase);
AnalyticsHelper.trackEvent(
widget.events[EditProductAction.openPage]!,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ class _EditNewPackagingsState extends State<EditNewPackagings>
_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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class _NutritionPageLoadedState extends State<NutritionPageLoaded>
initUpToDate(widget.product, context.read<LocalDatabase>());
_nutritionContainer = NutritionContainer(
orderedNutrients: widget.orderedNutrients,
product: initialProduct,
product: upToDateProduct,
);

_decimalNumberFormat =
Expand Down
2 changes: 1 addition & 1 deletion packages/smooth_app/lib/pages/product/summary_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class _SummaryCardState extends State<SummaryCard> with UpToDateMixin {
super.initState();
initUpToDate(widget._product, context.read<LocalDatabase>());
_questionsLayout = getUserQuestionsLayout(context.read<UserPreferences>());
if (ProductIncompleteCard.isProductIncomplete(initialProduct)) {
if (ProductIncompleteCard.isProductIncomplete(upToDateProduct)) {
AnalyticsHelper.trackEvent(
AnalyticsEvent.showFastTrackProductEditCard,
barcode: barcode,
Expand Down

0 comments on commit 0a64d60

Please sign in to comment.