diff --git a/packages/smooth_app/lib/pages/product/ordered_nutrients_cache.dart b/packages/smooth_app/lib/pages/product/ordered_nutrients_cache.dart index 21ceeacb8c9..e5543fac587 100644 --- a/packages/smooth_app/lib/pages/product/ordered_nutrients_cache.dart +++ b/packages/smooth_app/lib/pages/product/ordered_nutrients_cache.dart @@ -2,28 +2,25 @@ import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:openfoodfacts/openfoodfacts.dart'; -import 'package:provider/provider.dart'; -import 'package:smooth_app/database/dao_string.dart'; -import 'package:smooth_app/database/local_database.dart'; import 'package:smooth_app/generic_lib/loading_dialog.dart'; import 'package:smooth_app/query/product_query.dart'; /// Helper class about getting and caching the back-end ordered nutrients. class OrderedNutrientsCache { - OrderedNutrientsCache._(final LocalDatabase localDatabase) - : _daoString = DaoString(localDatabase); - - final DaoString _daoString; + OrderedNutrientsCache._(); OrderedNutrients? _orderedNutrients; OrderedNutrients get orderedNutrients => _orderedNutrients!; - /// Returns a database/downloaded cache, or null if it failed. + // We store the cached data in a static instead of a database, so that data + // can be refreshed (by each app full restart). + static final Map _cache = {}; + + /// Returns an app-local/downloaded cache, or null if it failed. static Future getCache( final BuildContext context, ) async { - final LocalDatabase localDatabase = context.read(); - final OrderedNutrientsCache cache = OrderedNutrientsCache._(localDatabase); + final OrderedNutrientsCache cache = OrderedNutrientsCache._(); cache._orderedNutrients = await cache._get() ?? await LoadingDialog.run( context: context, @@ -39,14 +36,14 @@ class OrderedNutrientsCache { /// Returns the ordered nutrients cached in the database. Future _get() async { - final String? string = await _daoString.get(_getKey()); + final String? string = _cache[_getKey()]; if (string != null) { try { return OrderedNutrients.fromJson( jsonDecode(string) as Map, ); } catch (e) { - await _daoString.put(_getKey(), null); + _cache.remove(_getKey()); } } return null; @@ -61,15 +58,10 @@ class OrderedNutrientsCache { final OrderedNutrients result = OrderedNutrients.fromJson( jsonDecode(string) as Map, ); - await _daoString.put(_getKey(), string); + _cache[_getKey()] = string; return result; } - /// Clears the cache. - /// - /// Typical use case: when it's time to refresh the cached data. - Future clear() async => _daoString.put(_getKey(), null); - /// Database key. String _getKey() { final OpenFoodFactsCountry country = ProductQuery.getCountry()!; diff --git a/packages/smooth_app/pubspec.lock b/packages/smooth_app/pubspec.lock index 4b2b5191264..f36d22bf6f8 100644 --- a/packages/smooth_app/pubspec.lock +++ b/packages/smooth_app/pubspec.lock @@ -209,34 +209,34 @@ packages: dependency: transitive description: name: camera_android - sha256: "45313e6bd5e74ac369d5bef928ab81594b5494762ac5cb8f4f255c01daa77622" + sha256: f83e406d34f5faa80bf0f5c3beee4b4c11da94a94e9621c1bb8e312988621b4b url: "https://pub.dev" source: hosted - version: "0.10.8" + version: "0.10.8+2" camera_avfoundation: dependency: transitive description: name: camera_avfoundation - sha256: "7ac8b950672716722af235eed7a7c37896853669800b7da706bb0a9fd41d3737" + sha256: "1a416e452b30955b392f4efbf23291d3f2ba3660a85e1628859eb62d2a2bab26" url: "https://pub.dev" source: hosted - version: "0.9.13+1" + version: "0.9.13+2" camera_platform_interface: dependency: transitive description: name: camera_platform_interface - sha256: "525017018d116c5db8c4c43ec2d9b1663216b369c9f75149158280168a7ce472" + sha256: "60fa0bb62a4f3bf3a7c413e31e4cd01b69c779ccc8e4668904a24581b86c316b" url: "https://pub.dev" source: hosted - version: "2.5.0" + version: "2.5.1" camera_web: dependency: transitive description: name: camera_web - sha256: d77965f32479ee6d8f48205dcf10f845d7210595c6c00faa51eab265d1cae993 + sha256: bcbd775fb3a9d51cc3ece899d54ad66f6306410556bac5759f78e13f9228841f url: "https://pub.dev" source: hosted - version: "0.3.1+3" + version: "0.3.1+4" carousel_slider: dependency: "direct main" description: @@ -890,10 +890,10 @@ packages: dependency: "direct main" description: name: openfoodfacts - sha256: "04b2e2691042ac1ff55ba90871858e789daae249c5c5a477f3e14930a70e6d12" + sha256: "8c989cbe437bf09cd06e480af9500fa821a76ddc0b1f82cdb9084f9e906acea2" url: "https://pub.dev" source: hosted - version: "2.5.0" + version: "2.5.1" openfoodfacts_flutter_lints: dependency: "direct dev" description: diff --git a/packages/smooth_app/pubspec.yaml b/packages/smooth_app/pubspec.yaml index 69ea9d0107d..df8d1d7a198 100644 --- a/packages/smooth_app/pubspec.yaml +++ b/packages/smooth_app/pubspec.yaml @@ -94,7 +94,7 @@ dependencies: - openfoodfacts: 2.5.0 + openfoodfacts: 2.5.1 # openfoodfacts: # path: ../../../openfoodfacts-dart