Skip to content

Commit

Permalink
feat: openfoodfacts#782 - now using global country and language
Browse files Browse the repository at this point in the history
Impacted files:
* `barcode_product_query.dart`: now using global country and language
* `continuous_scan_model.dart`: now using implicitly global country and language
* `country_selector.dart`: now setting global country
* `image_upload_card.dart`: now using global language
* `keywords_product_query.dart`: now using global country and language
* `knowledge_panels_query.dart`: now using global country and language
* `main.dart`: now setting global country and language at init time
* `new_product_page.dart`: now using implicitly global country and language
* `product_dialog_helper.dart`: now using implicitly global country and language
* `product_query.dart`: added getter and setter for global country and language
* `scan_page.dart`: now using implicitly global country and language
* `search_page.dart`: now using implicitly global country and language
* `user_preferences.dart`: minor refactoring
  • Loading branch information
monsieurtanuki committed Dec 29, 2021
1 parent 6e20e05 commit 73bcc6e
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
import 'package:image_cropper/image_cropper.dart';
import 'package:image_picker/image_picker.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:smooth_app/database/product_query.dart';
import 'package:smooth_app/pages/product/product_image_page.dart';

class ImageUploadCard extends StatefulWidget {
Expand Down Expand Up @@ -54,8 +55,7 @@ class _ImageUploadCardState extends State<ImageUploadCard> {
});

final SendImage image = SendImage(
lang: LanguageHelper.fromJson(
Localizations.localeOf(context).languageCode),
lang: ProductQuery.getLanguage(),
barcode: widget.product
.barcode!, //Probably throws an error, but this is not a big problem when we got a product without a barcode
imageField: widget.imageField,
Expand Down
11 changes: 1 addition & 10 deletions packages/smooth_app/lib/data_models/continuous_scan_model.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:openfoodfacts/model/Product.dart';
import 'package:openfoodfacts/utils/CountryHelper.dart';
import 'package:openfoodfacts/utils/LanguageHelper.dart';
import 'package:qr_code_scanner/qr_code_scanner.dart';
import 'package:smooth_app/data_models/fetched_product.dart';
import 'package:smooth_app/data_models/product_list.dart';
Expand All @@ -21,10 +19,7 @@ enum ScannedProductState {
}

class ContinuousScanModel with ChangeNotifier {
ContinuousScanModel({
required this.country,
required this.language,
});
ContinuousScanModel();

final Map<String, ScannedProductState> _states =
<String, ScannedProductState>{};
Expand All @@ -36,8 +31,6 @@ class ContinuousScanModel with ChangeNotifier {
String? _barcodeTrustCheck; // TODO(monsieurtanuki): could probably be removed
late DaoProduct _daoProduct;
late DaoProductList _daoProductList;
final OpenFoodFactsLanguage? language;
final OpenFoodFactsCountry? country;

QRViewController? _qrViewController;

Expand Down Expand Up @@ -167,8 +160,6 @@ class ContinuousScanModel with ChangeNotifier {
) async =>
BarcodeProductQuery(
barcode: barcode,
language: language,
country: country,
daoProduct: _daoProduct,
).getFetchedProduct();

Expand Down
7 changes: 2 additions & 5 deletions packages/smooth_app/lib/data_models/user_preferences.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import 'package:flutter/material.dart';
import 'package:openfoodfacts/personalized_search/preference_importance.dart';
import 'package:openfoodfacts/utils/CountryHelper.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:smooth_app/data_models/product_preferences.dart';
import 'package:smooth_app/database/product_query.dart';
import 'package:smooth_app/pages/onboarding/onboarding_flow_navigator.dart';

class UserPreferences extends ChangeNotifier {
Expand Down Expand Up @@ -66,9 +64,8 @@ class UserPreferences extends ChangeNotifier {
Future<void> setUserCountry(final String countryCode) async =>
_sharedPreferences.setString(_TAG_USER_COUNTRY_CODE, countryCode);

OpenFoodFactsCountry? get userCountry => ProductQuery.getCountry(
_sharedPreferences.getString(_TAG_USER_COUNTRY_CODE),
);
String? get userCountryCode =>
_sharedPreferences.getString(_TAG_USER_COUNTRY_CODE);

Future<void> setLastVisitedOnboardingPage(final OnboardingPage page) async =>
_sharedPreferences.setInt(_TAG_LAST_VISITED_ONBOARDING_PAGE, page.index);
Expand Down
9 changes: 2 additions & 7 deletions packages/smooth_app/lib/database/barcode_product_query.dart
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
import 'dart:async';

import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:openfoodfacts/utils/CountryHelper.dart';
import 'package:smooth_app/data_models/fetched_product.dart';
import 'package:smooth_app/database/dao_product.dart';
import 'package:smooth_app/database/product_query.dart';

class BarcodeProductQuery {
BarcodeProductQuery({
required this.barcode,
required this.language,
required this.country,
required this.daoProduct,
});

final String barcode;
final OpenFoodFactsLanguage? language;
final OpenFoodFactsCountry? country;
final DaoProduct daoProduct;

Future<FetchedProduct> getFetchedProduct() async {
final ProductQueryConfiguration configuration = ProductQueryConfiguration(
barcode,
fields: ProductQuery.fields,
language: language,
country: country,
language: ProductQuery.getLanguage(),
country: ProductQuery.getCountry(),
);

final ProductResult result;
Expand Down
16 changes: 3 additions & 13 deletions packages/smooth_app/lib/database/keywords_product_query.dart
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import 'dart:async';
import 'package:openfoodfacts/model/parameter/SearchTerms.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:openfoodfacts/utils/CountryHelper.dart';
import 'package:smooth_app/data_models/product_list.dart';
import 'package:smooth_app/database/product_query.dart';

class KeywordsProductQuery implements ProductQuery {
KeywordsProductQuery({
required this.keywords,
required this.language,
required this.country,
required this.size,
});

final String keywords;
final OpenFoodFactsLanguage? language;
final OpenFoodFactsCountry? country;
final int size;

@override
Expand All @@ -28,19 +23,14 @@ class KeywordsProductQuery implements ProductQuery {
PageSize(size: size),
SearchTerms(terms: <String>[keywords]),
],
language: language,
country: country,
language: ProductQuery.getLanguage(),
country: ProductQuery.getCountry(),
),
);

@override
ProductList getProductList() => ProductList.keywordSearch(keywords);

@override
String toString() => 'KeywordsProductQuery('
'"$keywords"'
', $language'
', $country'
', $size'
')';
String toString() => 'KeywordsProductQuery("$keywords", $size)';
}
10 changes: 3 additions & 7 deletions packages/smooth_app/lib/database/knowledge_panels_query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,21 @@ import 'dart:async';

import 'package:openfoodfacts/model/KnowledgePanels.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:openfoodfacts/utils/CountryHelper.dart';
import 'package:openfoodfacts/utils/QueryType.dart';
import 'package:smooth_app/database/product_query.dart';

class KnowledgePanelsQuery {
KnowledgePanelsQuery({
required this.barcode,
required this.country,
required this.language,
});

final String barcode;
final OpenFoodFactsCountry? country;
final OpenFoodFactsLanguage? language;

Future<KnowledgePanels> getKnowledgePanels() async {
final ProductQueryConfiguration configuration = ProductQueryConfiguration(
barcode,
language: language,
country: country,
language: ProductQuery.getLanguage(),
country: ProductQuery.getCountry(),
);
return OpenFoodAPIClient.getKnowledgePanels(configuration, QueryType.PROD);
}
Expand Down
33 changes: 25 additions & 8 deletions packages/smooth_app/lib/database/product_query.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
import 'package:flutter/material.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:openfoodfacts/utils/CountryHelper.dart';
import 'package:openfoodfacts/utils/OpenFoodAPIConfiguration.dart';
import 'package:smooth_app/data_models/product_list.dart';

abstract class ProductQuery {
static String _getCurrentLanguageCode(final BuildContext context) =>
Localizations.localeOf(context).languageCode;
/// Returns the global language for API queries.
static OpenFoodFactsLanguage? getLanguage() {
final List<OpenFoodFactsLanguage> languages =
OpenFoodAPIConfiguration.globalLanguages ?? <OpenFoodFactsLanguage>[];
if (languages.isEmpty) {
return null;
}
return languages[0];
}

static OpenFoodFactsLanguage? getCurrentLanguage(
final BuildContext context) =>
LanguageHelper.fromJson(_getCurrentLanguageCode(context));
/// Sets the global language for API queries.
static void setLanguage(final String languageCode) {
final OpenFoodFactsLanguage language =
LanguageHelper.fromJson(languageCode);
OpenFoodAPIConfiguration.globalLanguages = <OpenFoodFactsLanguage>[
language,
];
}

static OpenFoodFactsCountry? getCountry(final String? isoCode) =>
CountryHelper.fromJson(isoCode);
/// Returns the global country for API queries?
static OpenFoodFactsCountry? getCountry() =>
OpenFoodAPIConfiguration.globalCountry;

/// Sets the global country for API queries.
static void setCountry(final String? isoCode) =>
OpenFoodAPIConfiguration.globalCountry = CountryHelper.fromJson(isoCode);

static const User SMOOTH_USER = User(
userId: 'project-smoothie',
Expand Down
3 changes: 3 additions & 0 deletions packages/smooth_app/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:sentry_flutter/sentry_flutter.dart';
import 'package:smooth_app/data_models/product_preferences.dart';
import 'package:smooth_app/data_models/user_preferences.dart';
import 'package:smooth_app/database/local_database.dart';
import 'package:smooth_app/database/product_query.dart';
import 'package:smooth_app/pages/onboarding/onboarding_flow_navigator.dart';
import 'package:smooth_app/themes/smooth_theme.dart';
import 'package:smooth_app/themes/theme_provider.dart';
Expand Down Expand Up @@ -81,6 +82,7 @@ class _SmoothAppState extends State<SmoothApp> {
await _productPreferences
.loadReferenceFromAssets(DefaultAssetBundle.of(context));
await _userPreferences.init(_productPreferences);
ProductQuery.setCountry(_userPreferences.userCountryCode);
_localDatabase = await LocalDatabase.getLocalDatabase();
_themeProvider = ThemeProvider(_userPreferences);
}
Expand Down Expand Up @@ -173,6 +175,7 @@ class SmoothAppGetLanguage extends StatelessWidget {
context.watch<ProductPreferences>();
final Locale myLocale = Localizations.localeOf(context);
final String languageCode = myLocale.languageCode;
ProductQuery.setLanguage(languageCode);
_refresh(
productPreferences,
DefaultAssetBundle.of(context),
Expand Down
12 changes: 9 additions & 3 deletions packages/smooth_app/lib/pages/onboarding/country_selector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:iso_countries/iso_countries.dart';
import 'package:openfoodfacts/utils/CountryHelper.dart';
import 'package:smooth_app/data_models/user_preferences.dart';
import 'package:smooth_app/database/product_query.dart';
import 'package:smooth_ui_library/util/ui_helpers.dart';

/// A selector for selecting user's country.
Expand Down Expand Up @@ -31,7 +32,12 @@ class _CountrySelectorState extends State<CountrySelector> {
_userPreferences = await UserPreferences.getUserPreferences();
_countryList = _sanitizeCountriesList(localizedCountries);
_chosenValue = _countryList[0];
_userPreferences.setUserCountry(_chosenValue.countryCode);
_setUserCountry(_chosenValue.countryCode);
}

Future<void> _setUserCountry(final String countryCode) async {
await _userPreferences.setUserCountry(_chosenValue.countryCode);
ProductQuery.setCountry(_userPreferences.userCountryCode);
}

@override
Expand Down Expand Up @@ -68,10 +74,10 @@ class _CountrySelectorState extends State<CountrySelector> {
);
}).toList(),
onChanged: (Country? value) {
setState(() {
setState(() async {
if (value != null) {
_chosenValue = value;
_userPreferences.setUserCountry(_chosenValue.countryCode);
await _setUserCountry(_chosenValue.countryCode);
}
});
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:openfoodfacts/utils/CountryHelper.dart';
import 'package:smooth_app/data_models/fetched_product.dart';
import 'package:smooth_app/database/barcode_product_query.dart';
import 'package:smooth_app/database/dao_product.dart';
Expand All @@ -16,16 +15,12 @@ class ProductDialogHelper {
required this.context,
required this.localDatabase,
required this.refresh,
required this.country,
required this.language,
});

final String barcode;
final BuildContext context;
final LocalDatabase localDatabase;
final bool refresh;
final OpenFoodFactsCountry? country;
final OpenFoodFactsLanguage? language;
bool _popEd = false;

Future<FetchedProduct> openBestChoice() async {
Expand All @@ -42,8 +37,6 @@ class ProductDialogHelper {
builder: (BuildContext context) {
BarcodeProductQuery(
barcode: barcode,
language: language,
country: country,
daoProduct: DaoProduct(localDatabase),
).getFetchedProduct().then<void>(
(final FetchedProduct value) => _popSearchingDialog(value),
Expand Down
12 changes: 0 additions & 12 deletions packages/smooth_app/lib/pages/product/new_product_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@ import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:openfoodfacts/model/KnowledgePanels.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:openfoodfacts/utils/CountryHelper.dart';
import 'package:provider/provider.dart';
import 'package:smooth_app/cards/data_cards/image_upload_card.dart';
import 'package:smooth_app/cards/product_cards/knowledge_panels/knowledge_panels_builder.dart';
import 'package:smooth_app/data_models/fetched_product.dart';
import 'package:smooth_app/data_models/product_list.dart';
import 'package:smooth_app/data_models/product_preferences.dart';
import 'package:smooth_app/data_models/user_preferences.dart';
import 'package:smooth_app/database/dao_product_list.dart';
import 'package:smooth_app/database/knowledge_panels_query.dart';
import 'package:smooth_app/database/local_database.dart';
import 'package:smooth_app/database/product_query.dart';
import 'package:smooth_app/helpers/launch_url_helper.dart';
import 'package:smooth_app/helpers/product_cards_helper.dart';
import 'package:smooth_app/pages/product/common/product_dialog_helper.dart';
Expand All @@ -36,8 +33,6 @@ enum ProductPageMenuItem { WEB, REFRESH }
class _ProductPageState extends State<ProductPage> {
late Product _product;
late ProductPreferences _productPreferences;
late OpenFoodFactsCountry? _country;
late OpenFoodFactsLanguage? _language;

@override
void initState() {
Expand All @@ -51,9 +46,6 @@ class _ProductPageState extends State<ProductPage> {
// All watchers defined here:
_productPreferences = context.watch<ProductPreferences>();
final ThemeProvider themeProvider = context.watch<ThemeProvider>();
final UserPreferences userPreferences = context.watch<UserPreferences>();
_country = userPreferences.userCountry;
_language = ProductQuery.getCurrentLanguage(context);
final AppLocalizations appLocalizations = AppLocalizations.of(context)!;
final ThemeData themeData = Theme.of(context);
final ColorScheme colorScheme = themeData.colorScheme;
Expand Down Expand Up @@ -107,8 +99,6 @@ class _ProductPageState extends State<ProductPage> {
context: context,
localDatabase: localDatabase,
refresh: true,
language: _language,
country: _country,
);
final FetchedProduct fetchedProduct =
await productDialogHelper.openUniqueProductSearch();
Expand Down Expand Up @@ -209,8 +199,6 @@ class _ProductPageState extends State<ProductPage> {
// TODO(jasmeet): Avoid additional requests on rebuilds.
final Future<KnowledgePanels> knowledgePanels = KnowledgePanelsQuery(
barcode: _product.barcode!,
country: _country,
language: _language,
).getKnowledgePanels();
return FutureBuilder<KnowledgePanels>(
future: knowledgePanels,
Expand Down
Loading

0 comments on commit 73bcc6e

Please sign in to comment.