From 27db401a2500c5428613d7742dc084bc0ce58412 Mon Sep 17 00:00:00 2001 From: monsieurtanuki Date: Fri, 10 May 2024 14:55:09 +0200 Subject: [PATCH 1/3] feat: 5201 - change currency with country when relevant Impacted files: * `app_en.arb`: message for a "change currency too?" dialog * `country_selector.dart`: new method to change the currency at the same time as the country if relevant * `product_query.dart`: init currency if null * `pubspec.lock`: wtf * `pubspec.yaml`: upgraded `openfoodfacts` to `3.8.0` * `user_preferences_country_selector.dart`: asking to change the currency at the same time as the country only if confirmed or relevant * `welcome_page.dart`: explicitly asking to change the currency at the same time as the country --- packages/smooth_app/lib/l10n/app_en.arb | 14 +++++ .../pages/onboarding/country_selector.dart | 55 +++++++++++++++++++ .../lib/pages/onboarding/welcome_page.dart | 1 + .../user_preferences_country_selector.dart | 1 + .../smooth_app/lib/query/product_query.dart | 7 +++ packages/smooth_app/pubspec.lock | 4 +- packages/smooth_app/pubspec.yaml | 2 +- 7 files changed, 81 insertions(+), 3 deletions(-) diff --git a/packages/smooth_app/lib/l10n/app_en.arb b/packages/smooth_app/lib/l10n/app_en.arb index 113845369e8..526e2dd4bea 100644 --- a/packages/smooth_app/lib/l10n/app_en.arb +++ b/packages/smooth_app/lib/l10n/app_en.arb @@ -802,6 +802,20 @@ "@currency_chooser_label": { "description": "Label shown above a selector where the user can select their currency (in the preferences)" }, + "currency_auto_change_message": "You've just changed countries; do you also want to change the currency from {previousCurrency} to {possibleCurrency}?", + "@currency_auto_change_message": { + "description": "Message asking to confirm the change of currencies after the change of countries", + "placeholders": { + "previousCurrency": { + "type": "String", + "description": "Current currency" + }, + "possibleCurrency": { + "type": "String", + "description": "Possible currency" + } + } + }, "onboarding_country_chooser_label": "Please choose a country:", "@onboarding_country_chooser_label": { "description": "The label shown above a selector where the user can select their country (in the onboarding)" diff --git a/packages/smooth_app/lib/pages/onboarding/country_selector.dart b/packages/smooth_app/lib/pages/onboarding/country_selector.dart index 6d561081468..819ea51c80c 100644 --- a/packages/smooth_app/lib/pages/onboarding/country_selector.dart +++ b/packages/smooth_app/lib/pages/onboarding/country_selector.dart @@ -18,12 +18,14 @@ class CountrySelector extends StatefulWidget { this.padding, this.icon, this.inkWellBorderRadius, + required this.forceCurrencyChange, }); final TextStyle? textStyle; final EdgeInsetsGeometry? padding; final BorderRadius? inkWellBorderRadius; final Widget? icon; + final bool forceCurrencyChange; @override State createState() => _CountrySelectorState(); @@ -168,6 +170,13 @@ class _CountrySelectorState extends State { userPreferences, country.countryCode, ); + if (context.mounted) { + await _changeCurrencyIfRelevant( + country, + userPreferences, + context, + ); + } } }, child: DecoratedBox( @@ -301,4 +310,50 @@ class _CountrySelectorState extends State { _countryController.dispose(); super.dispose(); } + + Future _changeCurrencyIfRelevant( + final Country country, + final UserPreferences userPreferences, + final BuildContext context, + ) async { + final OpenFoodFactsCountry? offCountry = + OpenFoodFactsCountry.fromOffTag(country.countryCode); + final String? possibleCurrencyCode = offCountry?.currency?.name; + if (possibleCurrencyCode == null) { + return; + } + bool? changeCurrency; + final String? currentCurrencyCode = userPreferences.userCurrencyCode; + if (currentCurrencyCode == null) { + changeCurrency = true; + } else if (widget.forceCurrencyChange) { + changeCurrency = true; + } else if (currentCurrencyCode != possibleCurrencyCode) { + if (context.mounted) { + final AppLocalizations appLocalizations = AppLocalizations.of(context); + changeCurrency = await showDialog( + context: context, + builder: (final BuildContext context) => SmoothAlertDialog( + body: Text( + appLocalizations.currency_auto_change_message( + currentCurrencyCode, + possibleCurrencyCode, + ), + ), + negativeAction: SmoothActionButton( + onPressed: () => Navigator.of(context).pop(), + text: appLocalizations.no, + ), + positiveAction: SmoothActionButton( + onPressed: () => Navigator.of(context).pop(true), + text: appLocalizations.yes, + ), + ), + ); + } + } + if (changeCurrency == true) { + await userPreferences.setUserCurrencyCode(possibleCurrencyCode); + } + } } diff --git a/packages/smooth_app/lib/pages/onboarding/welcome_page.dart b/packages/smooth_app/lib/pages/onboarding/welcome_page.dart index 58535061740..205156b07c1 100644 --- a/packages/smooth_app/lib/pages/onboarding/welcome_page.dart +++ b/packages/smooth_app/lib/pages/onboarding/welcome_page.dart @@ -93,6 +93,7 @@ class WelcomePage extends StatelessWidget { child: SizedBox( width: double.infinity, child: CountrySelector( + forceCurrencyChange: true, padding: const EdgeInsets.symmetric( horizontal: SMALL_SPACE, ), diff --git a/packages/smooth_app/lib/pages/preferences/user_preferences_country_selector.dart b/packages/smooth_app/lib/pages/preferences/user_preferences_country_selector.dart index e58dfa3cc0e..6db20531a04 100644 --- a/packages/smooth_app/lib/pages/preferences/user_preferences_country_selector.dart +++ b/packages/smooth_app/lib/pages/preferences/user_preferences_country_selector.dart @@ -34,6 +34,7 @@ class UserPreferencesCountrySelector extends StatelessWidget { bottom: SMALL_SPACE, ), child: CountrySelector( + forceCurrencyChange: false, textStyle: themeData.textTheme.bodyMedium, icon: const Icon(Icons.edit), padding: const EdgeInsetsDirectional.only( diff --git a/packages/smooth_app/lib/query/product_query.dart b/packages/smooth_app/lib/query/product_query.dart index 1672f7dd538..49bc51770d4 100644 --- a/packages/smooth_app/lib/query/product_query.dart +++ b/packages/smooth_app/lib/query/product_query.dart @@ -67,6 +67,13 @@ abstract class ProductQuery { final OpenFoodFactsCountry country = OpenFoodFactsCountry.fromOffTag(isoCode) ?? defaultCountry; await _setCountry(userPreferences, country); + if (userPreferences.userCurrencyCode == null) { + // very very first time, or old app with new code + final Currency? possibleCurrency = country.currency; + if (possibleCurrency != null) { + await userPreferences.setUserCurrencyCode(possibleCurrency.name); + } + } } /// Sets the global country for API queries: explicit choice by the user. diff --git a/packages/smooth_app/pubspec.lock b/packages/smooth_app/pubspec.lock index 76a1d0e13f1..699c6526641 100644 --- a/packages/smooth_app/pubspec.lock +++ b/packages/smooth_app/pubspec.lock @@ -1132,10 +1132,10 @@ packages: dependency: "direct main" description: name: openfoodfacts - sha256: dd2e19905a00eba37f67b5c8a52612e93d0f6758a96986ee0afe3723da7e41d5 + sha256: "19c49aee1093dae611e79240e09ee46f3537b6083235307eec044fb3e5ecc750" url: "https://pub.dev" source: hosted - version: "3.7.0" + version: "3.8.0" openfoodfacts_flutter_lints: dependency: "direct dev" description: diff --git a/packages/smooth_app/pubspec.yaml b/packages/smooth_app/pubspec.yaml index 5fa56dfc7dc..3d32a5a670f 100644 --- a/packages/smooth_app/pubspec.yaml +++ b/packages/smooth_app/pubspec.yaml @@ -100,7 +100,7 @@ dependencies: path: ../scanner/zxing - openfoodfacts: 3.7.0 + openfoodfacts: 3.8.0 # openfoodfacts: # path: ../../../openfoodfacts-dart From df5b944309167fd94e3af49d05ff7db7564f4a39 Mon Sep 17 00:00:00 2001 From: monsieurtanuki Date: Fri, 10 May 2024 15:05:03 +0200 Subject: [PATCH 2/3] refactoring --- .../pages/onboarding/country_selector.dart | 38 +++++++++---------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/packages/smooth_app/lib/pages/onboarding/country_selector.dart b/packages/smooth_app/lib/pages/onboarding/country_selector.dart index 819ea51c80c..c5a6adef3ac 100644 --- a/packages/smooth_app/lib/pages/onboarding/country_selector.dart +++ b/packages/smooth_app/lib/pages/onboarding/country_selector.dart @@ -329,28 +329,26 @@ class _CountrySelectorState extends State { } else if (widget.forceCurrencyChange) { changeCurrency = true; } else if (currentCurrencyCode != possibleCurrencyCode) { - if (context.mounted) { - final AppLocalizations appLocalizations = AppLocalizations.of(context); - changeCurrency = await showDialog( - context: context, - builder: (final BuildContext context) => SmoothAlertDialog( - body: Text( - appLocalizations.currency_auto_change_message( - currentCurrencyCode, - possibleCurrencyCode, - ), - ), - negativeAction: SmoothActionButton( - onPressed: () => Navigator.of(context).pop(), - text: appLocalizations.no, - ), - positiveAction: SmoothActionButton( - onPressed: () => Navigator.of(context).pop(true), - text: appLocalizations.yes, + final AppLocalizations appLocalizations = AppLocalizations.of(context); + changeCurrency = await showDialog( + context: context, + builder: (final BuildContext context) => SmoothAlertDialog( + body: Text( + appLocalizations.currency_auto_change_message( + currentCurrencyCode, + possibleCurrencyCode, ), ), - ); - } + negativeAction: SmoothActionButton( + onPressed: () => Navigator.of(context).pop(), + text: appLocalizations.no, + ), + positiveAction: SmoothActionButton( + onPressed: () => Navigator.of(context).pop(true), + text: appLocalizations.yes, + ), + ), + ); } if (changeCurrency == true) { await userPreferences.setUserCurrencyCode(possibleCurrencyCode); From 85d296535c32af70e95774f898ddec2b62b890c8 Mon Sep 17 00:00:00 2001 From: monsieurtanuki Date: Fri, 10 May 2024 17:56:09 +0200 Subject: [PATCH 3/3] minor changes about labels --- packages/smooth_app/lib/l10n/app_en.arb | 8 ++++++-- .../smooth_app/lib/pages/onboarding/country_selector.dart | 6 ++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/smooth_app/lib/l10n/app_en.arb b/packages/smooth_app/lib/l10n/app_en.arb index 526e2dd4bea..aa2a8d8dca3 100644 --- a/packages/smooth_app/lib/l10n/app_en.arb +++ b/packages/smooth_app/lib/l10n/app_en.arb @@ -802,9 +802,13 @@ "@currency_chooser_label": { "description": "Label shown above a selector where the user can select their currency (in the preferences)" }, - "currency_auto_change_message": "You've just changed countries; do you also want to change the currency from {previousCurrency} to {possibleCurrency}?", + "country_change_message": "You have just changed countries.", + "@country_change_message": { + "description": "Message stating the change of countries" + }, + "currency_auto_change_message": "Do you want to change the currency from {previousCurrency} to {possibleCurrency}?", "@currency_auto_change_message": { - "description": "Message asking to confirm the change of currencies after the change of countries", + "description": "Message asking to confirm the change of currencies", "placeholders": { "previousCurrency": { "type": "String", diff --git a/packages/smooth_app/lib/pages/onboarding/country_selector.dart b/packages/smooth_app/lib/pages/onboarding/country_selector.dart index c5a6adef3ac..37d887cc6bd 100644 --- a/packages/smooth_app/lib/pages/onboarding/country_selector.dart +++ b/packages/smooth_app/lib/pages/onboarding/country_selector.dart @@ -334,10 +334,12 @@ class _CountrySelectorState extends State { context: context, builder: (final BuildContext context) => SmoothAlertDialog( body: Text( - appLocalizations.currency_auto_change_message( + '${appLocalizations.country_change_message}' + '\n' + '${appLocalizations.currency_auto_change_message( currentCurrencyCode, possibleCurrencyCode, - ), + )}', ), negativeAction: SmoothActionButton( onPressed: () => Navigator.of(context).pop(),