From c57c26e3c3d334de4a3ca28d3ee9a8bd085e4b49 Mon Sep 17 00:00:00 2001 From: monsieurtanuki Date: Wed, 23 Aug 2023 15:53:56 +0200 Subject: [PATCH 1/3] fix: 4575 - country is now always populated --- .../lib/background/background_task_crop.dart | 2 +- .../background/background_task_details.dart | 2 +- .../background_task_download_products.dart | 2 +- .../background_task_full_refresh.dart | 2 +- .../background_task_hunger_games.dart | 2 +- .../lib/background/background_task_image.dart | 2 +- .../background/background_task_offline.dart | 2 +- .../background_task_refresh_later.dart | 2 +- .../background_task_top_barcodes.dart | 2 +- .../background/background_task_unselect.dart | 2 +- packages/smooth_app/lib/main.dart | 2 +- .../lib/pages/navigator/external_page.dart | 16 +++-- .../pages/onboarding/country_selector.dart | 2 +- .../lib/pages/product/new_product_page.dart | 2 +- .../product/ordered_nutrients_cache.dart | 4 +- .../smooth_app/lib/query/product_query.dart | 59 +++++++++++++++---- 16 files changed, 69 insertions(+), 36 deletions(-) diff --git a/packages/smooth_app/lib/background/background_task_crop.dart b/packages/smooth_app/lib/background/background_task_crop.dart index 7709bf9fd5e..8a1712804e6 100644 --- a/packages/smooth_app/lib/background/background_task_crop.dart +++ b/packages/smooth_app/lib/background/background_task_crop.dart @@ -120,7 +120,7 @@ class BackgroundTaskCrop extends BackgroundTaskUpload { cropY2: cropY2, languageCode: language.code, user: jsonEncode(ProductQuery.getUser().toJson()), - country: ProductQuery.getCountry()!.offTag, + country: ProductQuery.getCountry().offTag, stamp: BackgroundTaskUpload.getStamp( barcode, imageField.offTag, diff --git a/packages/smooth_app/lib/background/background_task_details.dart b/packages/smooth_app/lib/background/background_task_details.dart index 0a0de40cb56..0026eb2d3b2 100644 --- a/packages/smooth_app/lib/background/background_task_details.dart +++ b/packages/smooth_app/lib/background/background_task_details.dart @@ -111,7 +111,7 @@ class BackgroundTaskDetails extends BackgroundTaskBarcode { languageCode: ProductQuery.getLanguage().code, inputMap: jsonEncode(minimalistProduct.toJson()), user: jsonEncode(ProductQuery.getUser().toJson()), - country: ProductQuery.getCountry()!.offTag, + country: ProductQuery.getCountry().offTag, stamp: getStamp(minimalistProduct.barcode!, stamp.tag), ); diff --git a/packages/smooth_app/lib/background/background_task_download_products.dart b/packages/smooth_app/lib/background/background_task_download_products.dart index 7f86cbb04eb..836d2893ed1 100644 --- a/packages/smooth_app/lib/background/background_task_download_products.dart +++ b/packages/smooth_app/lib/background/background_task_download_products.dart @@ -88,7 +88,7 @@ class BackgroundTaskDownloadProducts extends BackgroundTaskProgressing { uniqueId: uniqueId, languageCode: ProductQuery.getLanguage().offTag, user: jsonEncode(ProductQuery.getUser().toJson()), - country: ProductQuery.getCountry()!.offTag, + country: ProductQuery.getCountry().offTag, stamp: ';offlineProducts;$work', work: work, pageSize: pageSize, diff --git a/packages/smooth_app/lib/background/background_task_full_refresh.dart b/packages/smooth_app/lib/background/background_task_full_refresh.dart index 4e58be37683..671864af84f 100644 --- a/packages/smooth_app/lib/background/background_task_full_refresh.dart +++ b/packages/smooth_app/lib/background/background_task_full_refresh.dart @@ -63,7 +63,7 @@ class BackgroundTaskFullRefresh extends BackgroundTaskPaged { uniqueId: uniqueId, languageCode: ProductQuery.getLanguage().offTag, user: jsonEncode(ProductQuery.getUser().toJson()), - country: ProductQuery.getCountry()!.offTag, + country: ProductQuery.getCountry().offTag, stamp: ';fullRefresh', pageSize: pageSize, ); diff --git a/packages/smooth_app/lib/background/background_task_hunger_games.dart b/packages/smooth_app/lib/background/background_task_hunger_games.dart index e24db0421b3..54b53cbc91b 100644 --- a/packages/smooth_app/lib/background/background_task_hunger_games.dart +++ b/packages/smooth_app/lib/background/background_task_hunger_games.dart @@ -85,7 +85,7 @@ class BackgroundTaskHungerGames extends BackgroundTaskBarcode { barcode: barcode, languageCode: ProductQuery.getLanguage().offTag, user: jsonEncode(ProductQuery.getUser().toJson()), - country: ProductQuery.getCountry()!.offTag, + country: ProductQuery.getCountry().offTag, stamp: _getStamp(barcode, insightId), insightId: insightId, insightAnnotation: insightAnnotation, diff --git a/packages/smooth_app/lib/background/background_task_image.dart b/packages/smooth_app/lib/background/background_task_image.dart index 09608dfb981..46b4d80efd7 100644 --- a/packages/smooth_app/lib/background/background_task_image.dart +++ b/packages/smooth_app/lib/background/background_task_image.dart @@ -134,7 +134,7 @@ class BackgroundTaskImage extends BackgroundTaskUpload { cropY2: cropY2, languageCode: language.code, user: jsonEncode(ProductQuery.getUser().toJson()), - country: ProductQuery.getCountry()!.offTag, + country: ProductQuery.getCountry().offTag, stamp: BackgroundTaskUpload.getStamp( barcode, imageField.offTag, diff --git a/packages/smooth_app/lib/background/background_task_offline.dart b/packages/smooth_app/lib/background/background_task_offline.dart index fc66e1c1c73..683c9c9f476 100644 --- a/packages/smooth_app/lib/background/background_task_offline.dart +++ b/packages/smooth_app/lib/background/background_task_offline.dart @@ -69,7 +69,7 @@ class BackgroundTaskOffline extends BackgroundTaskProgressing { uniqueId: uniqueId, languageCode: ProductQuery.getLanguage().offTag, user: jsonEncode(ProductQuery.getUser().toJson()), - country: ProductQuery.getCountry()!.offTag, + country: ProductQuery.getCountry().offTag, stamp: ';offline', work: work, pageSize: pageSize, diff --git a/packages/smooth_app/lib/background/background_task_refresh_later.dart b/packages/smooth_app/lib/background/background_task_refresh_later.dart index dbd42c70797..040421a9bc9 100644 --- a/packages/smooth_app/lib/background/background_task_refresh_later.dart +++ b/packages/smooth_app/lib/background/background_task_refresh_later.dart @@ -81,7 +81,7 @@ class BackgroundTaskRefreshLater extends BackgroundTaskBarcode { barcode: barcode, languageCode: ProductQuery.getLanguage().code, user: jsonEncode(ProductQuery.getUser().toJson()), - country: ProductQuery.getCountry()!.offTag, + country: ProductQuery.getCountry().offTag, timestamp: LocalDatabase.nowInMillis(), stamp: _getStamp(barcode), ); diff --git a/packages/smooth_app/lib/background/background_task_top_barcodes.dart b/packages/smooth_app/lib/background/background_task_top_barcodes.dart index 64f5781ed19..0bd5fc49e8e 100644 --- a/packages/smooth_app/lib/background/background_task_top_barcodes.dart +++ b/packages/smooth_app/lib/background/background_task_top_barcodes.dart @@ -83,7 +83,7 @@ class BackgroundTaskTopBarcodes extends BackgroundTaskProgressing { uniqueId: uniqueId, languageCode: ProductQuery.getLanguage().offTag, user: jsonEncode(ProductQuery.getUser().toJson()), - country: ProductQuery.getCountry()!.offTag, + country: ProductQuery.getCountry().offTag, stamp: ';offlineBarcodes;$work', work: work, pageSize: pageSize, diff --git a/packages/smooth_app/lib/background/background_task_unselect.dart b/packages/smooth_app/lib/background/background_task_unselect.dart index 223d99fb7d5..35b6ee216c3 100644 --- a/packages/smooth_app/lib/background/background_task_unselect.dart +++ b/packages/smooth_app/lib/background/background_task_unselect.dart @@ -86,7 +86,7 @@ class BackgroundTaskUnselect extends BackgroundTaskBarcode { imageField: imageField.offTag, languageCode: language.code, user: jsonEncode(ProductQuery.getUser().toJson()), - country: ProductQuery.getCountry()!.offTag, + country: ProductQuery.getCountry().offTag, // same stamp as image upload stamp: BackgroundTaskUpload.getStamp( barcode, diff --git a/packages/smooth_app/lib/main.dart b/packages/smooth_app/lib/main.dart index 9a533d9a500..67a1f1fa84b 100644 --- a/packages/smooth_app/lib/main.dart +++ b/packages/smooth_app/lib/main.dart @@ -141,7 +141,7 @@ Future _init1() async { AnalyticsHelper.linkPreferences(_userPreferences); - await ProductQuery.setCountry(_userPreferences); + await ProductQuery.initCountry(_userPreferences); _themeProvider = ThemeProvider(_userPreferences); _colorProvider = ColorProvider(_userPreferences); _textContrastProvider = TextContrastProvider(_userPreferences); diff --git a/packages/smooth_app/lib/pages/navigator/external_page.dart b/packages/smooth_app/lib/pages/navigator/external_page.dart index 8d6a257b680..6c42faea28f 100644 --- a/packages/smooth_app/lib/pages/navigator/external_page.dart +++ b/packages/smooth_app/lib/pages/navigator/external_page.dart @@ -34,18 +34,16 @@ class _ExternalPageState extends State { WidgetsBinding.instance.addPostFrameCallback((_) async { // First let's try with https://{country}.openfoodfacts.org - final OpenFoodFactsCountry? country = ProductQuery.getCountry(); + final OpenFoodFactsCountry country = ProductQuery.getCountry(); String? url; - if (country != null) { - url = path.join( - 'https://${country.offTag}.openfoodfacts.org', - widget.path, - ); + url = path.join( + 'https://${country.offTag}.openfoodfacts.org', + widget.path, + ); - if (await _testUrl(url)) { - url = null; - } + if (await _testUrl(url)) { + url = null; } // If that's not OK, let's try with world.openfoodfacts.org?lc={language} diff --git a/packages/smooth_app/lib/pages/onboarding/country_selector.dart b/packages/smooth_app/lib/pages/onboarding/country_selector.dart index dae38d58822..28b4d41cb90 100644 --- a/packages/smooth_app/lib/pages/onboarding/country_selector.dart +++ b/packages/smooth_app/lib/pages/onboarding/country_selector.dart @@ -164,7 +164,7 @@ class _CountrySelectorState extends State { if (country != null) { await ProductQuery.setCountry( userPreferences, - isoCode: country.countryCode, + country.countryCode, ); } }, diff --git a/packages/smooth_app/lib/pages/product/new_product_page.dart b/packages/smooth_app/lib/pages/product/new_product_page.dart index 76376019ccb..afe4477a059 100644 --- a/packages/smooth_app/lib/pages/product/new_product_page.dart +++ b/packages/smooth_app/lib/pages/product/new_product_page.dart @@ -280,7 +280,7 @@ class _ProductPageState extends State // We need to provide a sharePositionOrigin to make the plugin work on ipad final RenderBox? box = context.findRenderObject() as RenderBox?; final String url = 'https://' - '${ProductQuery.getCountry()!.offTag}.openfoodfacts.org' + '${ProductQuery.getCountry().offTag}.openfoodfacts.org' '/product/$barcode'; Share.share( appLocalizations.share_product_text(url), 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 e5543fac587..13774ea25cf 100644 --- a/packages/smooth_app/lib/pages/product/ordered_nutrients_cache.dart +++ b/packages/smooth_app/lib/pages/product/ordered_nutrients_cache.dart @@ -52,7 +52,7 @@ class OrderedNutrientsCache { /// Downloads the ordered nutrients and caches them in the database. Future _download() async { final String string = await OpenFoodAPIClient.getOrderedNutrientsJsonString( - country: ProductQuery.getCountry()!, + country: ProductQuery.getCountry(), language: ProductQuery.getLanguage(), ); final OrderedNutrients result = OrderedNutrients.fromJson( @@ -64,7 +64,7 @@ class OrderedNutrientsCache { /// Database key. String _getKey() { - final OpenFoodFactsCountry country = ProductQuery.getCountry()!; + final OpenFoodFactsCountry country = ProductQuery.getCountry(); final OpenFoodFactsLanguage language = ProductQuery.getLanguage(); return 'nutrients.pl' '/${country.offTag}' diff --git a/packages/smooth_app/lib/query/product_query.dart b/packages/smooth_app/lib/query/product_query.dart index e9d79dfe816..d03a48ab34f 100644 --- a/packages/smooth_app/lib/query/product_query.dart +++ b/packages/smooth_app/lib/query/product_query.dart @@ -13,7 +13,7 @@ import 'package:uuid/uuid.dart'; abstract class ProductQuery { static const ProductQueryVersion productQueryVersion = ProductQueryVersion.v3; - static OpenFoodFactsCountry? _country; + static late OpenFoodFactsCountry _country; static String replaceSubdomain(final String url) => UriHelper.replaceSubdomain( @@ -53,29 +53,64 @@ abstract class ProductQuery { } /// Returns the global country for API queries. - static OpenFoodFactsCountry? getCountry() => _country; + static OpenFoodFactsCountry getCountry() => _country; + + /// Sets the global country for API queries: implicit choice at init time. + static Future initCountry( + final UserPreferences userPreferences, + ) async { + const OpenFoodFactsCountry defaultCountry = + OpenFoodFactsCountry.SWITZERLAND; // why not? + final String? isoCode = userPreferences.userCountryCode ?? + PlatformDispatcher.instance.locale.countryCode?.toLowerCase(); + final OpenFoodFactsCountry country = + _countryFromJsonFix(isoCode) ?? defaultCountry; + await _setCountry(userPreferences, country); + } + + /// Sets the global country for API queries: explicit choice by the user. + /// + /// Returns true if the [isoCode] was correctly detected. + static Future setCountry( + final UserPreferences userPreferences, + final String isoCode, + ) async { + final OpenFoodFactsCountry? country = _countryFromJsonFix(isoCode); + if (country == null) { + return false; + } + await _setCountry(userPreferences, country); + return true; + } /// Sets the global country for API queries. - static Future setCountry( - final UserPreferences userPreferences, { - String? isoCode, - }) async { - isoCode ??= userPreferences.userCountryCode ?? - PlatformDispatcher.instance.locale.countryCode?.toLowerCase(); - _country = CountryHelper.fromJson(isoCode); + static Future _setCountry( + final UserPreferences userPreferences, + final OpenFoodFactsCountry country, + ) async { + _country = country; // we need this to run "world" queries OpenFoodAPIConfiguration.globalCountry = null; - isoCode = _country?.offTag; - if (isoCode != null && isoCode != userPreferences.userCountryCode) { + final String isoCode = country.offTag; + if (isoCode != userPreferences.userCountryCode) { await userPreferences.setUserCountryCode(isoCode); } } + // TODO(monsieurtanuki): move to off-dart + static OpenFoodFactsCountry? _countryFromJsonFix(final String? isoCode) { + // special case as we use 'uk' in off-dart + if (isoCode == 'gb') { + return OpenFoodFactsCountry.UNITED_KINGDOM; + } + return CountryHelper.fromJson(isoCode); + } + /// Returns the global locale string (e.g. 'pt_BR') static String getLocaleString() => '${getLanguage().code}' '_' - '${getCountry()!.offTag.toUpperCase()}'; + '${getCountry().offTag.toUpperCase()}'; /// Sets a comment for the user agent. /// From f4903876863331652bbaf0b67cd79c5a754fc7a7 Mon Sep 17 00:00:00 2001 From: monsieurtanuki Date: Sat, 26 Aug 2023 16:38:59 +0200 Subject: [PATCH 2/3] Update packages/smooth_app/lib/query/product_query.dart --- packages/smooth_app/lib/query/product_query.dart | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/smooth_app/lib/query/product_query.dart b/packages/smooth_app/lib/query/product_query.dart index d03a48ab34f..511cdf3869e 100644 --- a/packages/smooth_app/lib/query/product_query.dart +++ b/packages/smooth_app/lib/query/product_query.dart @@ -59,8 +59,9 @@ abstract class ProductQuery { static Future initCountry( final UserPreferences userPreferences, ) async { + // not ideal, but we have many contributors monitoring France const OpenFoodFactsCountry defaultCountry = - OpenFoodFactsCountry.SWITZERLAND; // why not? + OpenFoodFactsCountry.FRANCE; final String? isoCode = userPreferences.userCountryCode ?? PlatformDispatcher.instance.locale.countryCode?.toLowerCase(); final OpenFoodFactsCountry country = From a28d322b91ea55e3edb4359915fb9a3de76400e6 Mon Sep 17 00:00:00 2001 From: monsieurtanuki Date: Sat, 26 Aug 2023 16:44:53 +0200 Subject: [PATCH 3/3] fix: 4575 - reformatted --- packages/smooth_app/lib/query/product_query.dart | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/smooth_app/lib/query/product_query.dart b/packages/smooth_app/lib/query/product_query.dart index 511cdf3869e..dc85cefdfaa 100644 --- a/packages/smooth_app/lib/query/product_query.dart +++ b/packages/smooth_app/lib/query/product_query.dart @@ -60,8 +60,7 @@ abstract class ProductQuery { final UserPreferences userPreferences, ) async { // not ideal, but we have many contributors monitoring France - const OpenFoodFactsCountry defaultCountry = - OpenFoodFactsCountry.FRANCE; + const OpenFoodFactsCountry defaultCountry = OpenFoodFactsCountry.FRANCE; final String? isoCode = userPreferences.userCountryCode ?? PlatformDispatcher.instance.locale.countryCode?.toLowerCase(); final OpenFoodFactsCountry country =