Skip to content

Commit

Permalink
fix: 4575 - country is now always populated (#4591)
Browse files Browse the repository at this point in the history
* fix: 4575 - country is now always populated

* Update packages/smooth_app/lib/query/product_query.dart

* fix: 4575 - reformatted
  • Loading branch information
monsieurtanuki committed Aug 26, 2023
1 parent 5c2dc8e commit d804222
Show file tree
Hide file tree
Showing 16 changed files with 69 additions and 36 deletions.
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
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

0 comments on commit d804222

Please sign in to comment.