-
-
Notifications
You must be signed in to change notification settings - Fork 280
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 3941 - refactoring about up-to-date product data for StatefulWi…
…dgets (#4262)
- Loading branch information
1 parent
79aad85
commit 4f35708
Showing
12 changed files
with
254 additions
and
291 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:openfoodfacts/openfoodfacts.dart'; | ||
import 'package:smooth_app/background/background_task_manager.dart'; | ||
import 'package:smooth_app/database/local_database.dart'; | ||
|
||
/// Provides the most up-to-date local product data for a StatefulWidget. | ||
/// | ||
/// Typically we have | ||
/// * a product from the database (downloaded from the server) | ||
/// * potentially pending changes to apply on top while they're being uploaded | ||
/// | ||
/// With this mixin | ||
/// * we get the most up-to-date local product data | ||
/// * we re-launch the task manager if relevant | ||
/// * 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. | ||
void initUpToDate( | ||
final Product initialProduct, | ||
final LocalDatabase localDatabase, | ||
) { | ||
this.initialProduct = initialProduct; | ||
_localDatabase = localDatabase; | ||
localDatabase.upToDate.showInterest(barcode); | ||
} | ||
|
||
@protected | ||
late final Product initialProduct; | ||
|
||
late final LocalDatabase _localDatabase; | ||
|
||
late Product _product; | ||
|
||
@protected | ||
String get barcode => initialProduct.barcode!; | ||
|
||
@protected | ||
Product get upToDateProduct => _product; | ||
|
||
@override | ||
void dispose() { | ||
_localDatabase.upToDate.loseInterest(barcode); | ||
super.dispose(); | ||
} | ||
|
||
/// Refreshes [upToDateProduct] with the latest available local data. | ||
/// | ||
/// To be used in the `build` method, after a call to | ||
/// `context.watch<LocalDatabase>()`. | ||
void refreshUpToDate() { | ||
BackgroundTaskManager.getInstance(_localDatabase).run(); // no await | ||
_product = _localDatabase.upToDate.getLocalUpToDate(initialProduct); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.