Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 3980 - refresh of the ordered nutrients #3997

Merged
merged 2 commits into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>{};
g123k marked this conversation as resolved.
Show resolved Hide resolved

/// 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