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: 4575 - country is now always populated #4591

Merged
merged 3 commits into from
Aug 26, 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
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packages/smooth_app/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ Future<bool> _init1() async {

AnalyticsHelper.linkPreferences(_userPreferences);

await ProductQuery.setCountry(_userPreferences);
await ProductQuery.initCountry(_userPreferences);
_themeProvider = ThemeProvider(_userPreferences);
_colorProvider = ColorProvider(_userPreferences);
_textContrastProvider = TextContrastProvider(_userPreferences);
Expand Down
16 changes: 7 additions & 9 deletions packages/smooth_app/lib/pages/navigator/external_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,16 @@ class _ExternalPageState extends State<ExternalPage> {

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}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ class _CountrySelectorState extends State<CountrySelector> {
if (country != null) {
await ProductQuery.setCountry(
userPreferences,
isoCode: country.countryCode,
country.countryCode,
);
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ class _ProductPageState extends State<ProductPage>
// 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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class OrderedNutrientsCache {
/// Downloads the ordered nutrients and caches them in the database.
Future<OrderedNutrients> _download() async {
final String string = await OpenFoodAPIClient.getOrderedNutrientsJsonString(
country: ProductQuery.getCountry()!,
country: ProductQuery.getCountry(),
language: ProductQuery.getLanguage(),
);
final OrderedNutrients result = OrderedNutrients.fromJson(
Expand All @@ -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}'
Expand Down
59 changes: 47 additions & 12 deletions packages/smooth_app/lib/query/product_query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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<void> 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<bool> 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<void> setCountry(
final UserPreferences userPreferences, {
String? isoCode,
}) async {
isoCode ??= userPreferences.userCountryCode ??
PlatformDispatcher.instance.locale.countryCode?.toLowerCase();
_country = CountryHelper.fromJson(isoCode);
static Future<void> _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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did already get answers in Ukrainian ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

answers in Ukrainian, but regarding the UK for Eco-Score adjustments ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://fr.wikipedia.org/wiki/ISO_3166-1:

  • Ukraine is 'ua'
  • United Kingdom is 'gb'

For some reason, United Kingdom is 'uk' in openfoodfacts.

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.
///
Expand Down