Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 5201 - change currency with country when relevant #5238

Merged
merged 4 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading