Skip to content

Commit

Permalink
fix: 3980 - refresh of the ordered nutrients (#3997)
Browse files Browse the repository at this point in the history
Impacted files:
* `ordered_nutrients_cache.dart`: now we cache the downloaded data in static - it used to be in database but then was never refreshed
* `pubspec.lock`: wtf
* `pubspec.yaml`: upgraded openfoodfacts to 2.5.1
  • Loading branch information
monsieurtanuki authored May 23, 2023
1 parent 96426b2 commit ec7113d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 29 deletions.
28 changes: 10 additions & 18 deletions packages/smooth_app/lib/pages/product/ordered_nutrients_cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> _cache = <String, String>{};

/// Returns an app-local/downloaded cache, or null if it failed.
static Future<OrderedNutrientsCache?> getCache(
final BuildContext context,
) async {
final LocalDatabase localDatabase = context.read<LocalDatabase>();
final OrderedNutrientsCache cache = OrderedNutrientsCache._(localDatabase);
final OrderedNutrientsCache cache = OrderedNutrientsCache._();
cache._orderedNutrients = await cache._get() ??
await LoadingDialog.run<OrderedNutrients>(
context: context,
Expand All @@ -39,14 +36,14 @@ class OrderedNutrientsCache {

/// Returns the ordered nutrients cached in the database.
Future<OrderedNutrients?> _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<String, dynamic>,
);
} catch (e) {
await _daoString.put(_getKey(), null);
_cache.remove(_getKey());
}
}
return null;
Expand All @@ -61,15 +58,10 @@ class OrderedNutrientsCache {
final OrderedNutrients result = OrderedNutrients.fromJson(
jsonDecode(string) as Map<String, dynamic>,
);
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<void> clear() async => _daoString.put(_getKey(), null);

/// Database key.
String _getKey() {
final OpenFoodFactsCountry country = ProductQuery.getCountry()!;
Expand Down
20 changes: 10 additions & 10 deletions packages/smooth_app/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion packages/smooth_app/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ dependencies:



openfoodfacts: 2.5.0
openfoodfacts: 2.5.1
# openfoodfacts:
# path: ../../../openfoodfacts-dart

Expand Down

0 comments on commit ec7113d

Please sign in to comment.