From 5f7966c7f880611d5114876ada463f189cd473b0 Mon Sep 17 00:00:00 2001 From: monsieurtanuki Date: Sat, 11 May 2024 14:38:13 +0200 Subject: [PATCH] feat: 5201 - change currency with country when relevant (#5238) * 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 * refactoring * minor changes about labels --- packages/smooth_app/lib/l10n/app_en.arb | 18 ++++++ .../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 +++ 5 files changed, 82 insertions(+) diff --git a/packages/smooth_app/lib/l10n/app_en.arb b/packages/smooth_app/lib/l10n/app_en.arb index 113845369e8..aa2a8d8dca3 100644 --- a/packages/smooth_app/lib/l10n/app_en.arb +++ b/packages/smooth_app/lib/l10n/app_en.arb @@ -802,6 +802,24 @@ "@currency_chooser_label": { "description": "Label shown above a selector where the user can select their currency (in the preferences)" }, + "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", + "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..37d887cc6bd 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) { + final AppLocalizations appLocalizations = AppLocalizations.of(context); + changeCurrency = await showDialog( + context: context, + builder: (final BuildContext context) => SmoothAlertDialog( + body: Text( + '${appLocalizations.country_change_message}' + '\n' + '${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.