Skip to content

Commit

Permalink
feat: 5201 - change currency with country when relevant (#5238)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
monsieurtanuki authored May 11, 2024
1 parent 777241d commit 5f7966c
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 0 deletions.
18 changes: 18 additions & 0 deletions packages/smooth_app/lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand Down
55 changes: 55 additions & 0 deletions packages/smooth_app/lib/pages/onboarding/country_selector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<CountrySelector> createState() => _CountrySelectorState();
Expand Down Expand Up @@ -168,6 +170,13 @@ class _CountrySelectorState extends State<CountrySelector> {
userPreferences,
country.countryCode,
);
if (context.mounted) {
await _changeCurrencyIfRelevant(
country,
userPreferences,
context,
);
}
}
},
child: DecoratedBox(
Expand Down Expand Up @@ -301,4 +310,50 @@ class _CountrySelectorState extends State<CountrySelector> {
_countryController.dispose();
super.dispose();
}

Future<void> _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<bool>(
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);
}
}
}
1 change: 1 addition & 0 deletions packages/smooth_app/lib/pages/onboarding/welcome_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class WelcomePage extends StatelessWidget {
child: SizedBox(
width: double.infinity,
child: CountrySelector(
forceCurrencyChange: true,
padding: const EdgeInsets.symmetric(
horizontal: SMALL_SPACE,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
7 changes: 7 additions & 0 deletions packages/smooth_app/lib/query/product_query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 5f7966c

Please sign in to comment.