From d8042224f3a128d3b28d864d6243f2280f90fba0 Mon Sep 17 00:00:00 2001 From: monsieurtanuki Date: Sat, 26 Aug 2023 17:11:53 +0200 Subject: [PATCH] fix: 4575 - country is now always populated (#4591) * fix: 4575 - country is now always populated * Update packages/smooth_app/lib/query/product_query.dart * fix: 4575 - reformatted --- .../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 7709bf9fd5e2..8a1712804e65 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 0a0de40cb566..0026eb2d3b25 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 7f86cbb04ebb..836d2893ed17 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 4e58be376835..671864af84f4 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 e24db0421b3c..54b53cbc91b2 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 09608dfb9817..46b4d80efd7f 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 fc66e1c1c73d..683c9c9f476b 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 dbd42c707979..040421a9bc94 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 64f5781ed199..0bd5fc49e8e5 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 223d99fb7d50..35b6ee216c32 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 9a533d9a500a..67a1f1fa84b8 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 8d6a257b6801..6c42faea28f1 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 dae38d588228..28b4d41cb908 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 76376019ccb4..afe4477a059c 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 e5543fac587b..13774ea25cfe 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 e9d79dfe8166..dc85cefdfaa0 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 { + // not ideal, but we have many contributors monitoring France + const OpenFoodFactsCountry defaultCountry = OpenFoodFactsCountry.FRANCE; + 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. ///