Skip to content

Commit

Permalink
Prices: if the selected location has another currency, prompt the use…
Browse files Browse the repository at this point in the history
…r to change (#6239)
  • Loading branch information
g123k authored Jan 19, 2025
1 parent 0020264 commit 7d606ff
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 2 deletions.
31 changes: 31 additions & 0 deletions packages/smooth_app/lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -2235,6 +2235,37 @@
"prices_privacy_warning_title": "Privacy warning",
"prices_privacy_warning_message": "Prices will be public, along with the store they refer to.\nThat might allow people who know about your Open Food Facts pseudonym to:\n* infer in which area you live\n* know what you are buying\nIf you are uneasy with that, please change your pseudonym, or create a new Open Food Facts account and log into the app with it.",
"prices_unknown_product": "Unknown product",
"prices_currency_change_proposal_title": "Change the currency?",
"prices_currency_change_proposal_message": "Your current currency is **{currency}**. Would you like to change it to **{newCurrency}**?",
"@prices_currency_change_proposal_message": {
"description": "Message to ask the user if they want to change the currency",
"placeholders": {
"currency": {
"type": "String"
},
"newCurrency": {
"type": "String"
}
}
},
"prices_currency_change_proposal_action_approve": "Yes, use {newCurrency}",
"@prices_currency_change_proposal_action_approve": {
"description": "Button to approve the currency change",
"placeholders": {
"newCurrency": {
"type": "String"
}
}
},
"prices_currency_change_proposal_action_cancel": "No, keep {currency}",
"@prices_currency_change_proposal_action_cancel": {
"description": "Button to cancel the currency change",
"placeholders": {
"currency": {
"type": "String"
}
}
},
"@prices_unknown_product": {
"description": "Very small text, in the context of prices, to say that the product is unknown"
},
Expand Down
10 changes: 9 additions & 1 deletion packages/smooth_app/lib/pages/prices/price_location_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@ import 'package:smooth_app/pages/search/search_page.dart';

/// Card that displays the location for price adding.
class PriceLocationCard extends StatelessWidget {
const PriceLocationCard();
const PriceLocationCard({
required this.onLocationChanged,
});

final Function(OsmLocation? oldLocation, OsmLocation location)
onLocationChanged;

@override
Widget build(BuildContext context) {
final PriceModel model = context.watch<PriceModel>();
final AppLocalizations appLocalizations = AppLocalizations.of(context);
final OsmLocation? location = model.location;

return SmoothCardWithRoundedHeader(
title: appLocalizations.prices_location_subtitle,
leading: const Icon(Icons.shopping_cart),
Expand Down Expand Up @@ -73,6 +79,8 @@ class PriceLocationCard extends StatelessWidget {
final List<OsmLocation> newOsmLocations =
await daoOsmLocation.getAll();
model.locations = newOsmLocations;

onLocationChanged.call(location, model.location!);
},
),
);
Expand Down
70 changes: 69 additions & 1 deletion packages/smooth_app/lib/pages/prices/product_price_add_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:provider/provider.dart';
import 'package:smooth_app/data_models/preferences/user_preferences.dart';
import 'package:smooth_app/database/dao_osm_location.dart';
import 'package:smooth_app/database/local_database.dart';
import 'package:smooth_app/generic_lib/bottom_sheets/smooth_bottom_sheet.dart';
import 'package:smooth_app/generic_lib/design_constants.dart';
import 'package:smooth_app/generic_lib/dialogs/smooth_alert_dialog.dart';
import 'package:smooth_app/generic_lib/widgets/smooth_back_button.dart';
Expand All @@ -27,6 +28,7 @@ import 'package:smooth_app/themes/theme_provider.dart';
import 'package:smooth_app/widgets/smooth_app_bar.dart';
import 'package:smooth_app/widgets/smooth_expandable_floating_action_button.dart';
import 'package:smooth_app/widgets/smooth_scaffold.dart';
import 'package:smooth_app/widgets/smooth_text.dart';
import 'package:smooth_app/widgets/will_pop_scope.dart';

/// Single page that displays all the elements of price adding.
Expand Down Expand Up @@ -134,7 +136,17 @@ class _ProductPriceAddPageState extends State<ProductPriceAddPage>
const SizedBox(height: LARGE_SPACE),
const PriceDateCard(),
const SizedBox(height: LARGE_SPACE),
const PriceLocationCard(),
PriceLocationCard(
onLocationChanged: (
OsmLocation? oldLocation,
OsmLocation location,
) =>
_updateCurrency(
oldLocation,
location,
model,
),
),
const SizedBox(height: LARGE_SPACE),
const PriceCurrencyCard(),
const SizedBox(height: LARGE_SPACE),
Expand Down Expand Up @@ -176,6 +188,62 @@ class _ProductPriceAddPageState extends State<ProductPriceAddPage>
);
}

Future<void> _updateCurrency(
OsmLocation? oldLocation,
OsmLocation location,
PriceModel model,
) async {
if (location.countryCode != null) {
final Currency? newCurrency =
OpenFoodFactsCountry.fromOffTag(location.countryCode)?.currency;

if (newCurrency != null && model.currency != newCurrency) {
final AppLocalizations appLocalizations = AppLocalizations.of(context);
final SmoothColorsThemeExtension extension =
context.extension<SmoothColorsThemeExtension>();

final Currency? currency =
await showSmoothListOfChoicesModalSheet<Currency?>(
context: context,
title: appLocalizations.prices_currency_change_proposal_title,
header: ColoredBox(
color: extension.primaryLight,
child: Padding(
padding: const EdgeInsetsDirectional.symmetric(
horizontal: LARGE_SPACE,
vertical: MEDIUM_SPACE,
),
child: TextWithBoldParts(
text: appLocalizations.prices_currency_change_proposal_message(
model.currency.name, newCurrency.name),
textStyle: Theme.of(context).textTheme.bodyLarge?.copyWith(
fontWeight: FontWeight.w500,
),
),
),
),
labels: <String>[
appLocalizations.prices_currency_change_proposal_action_approve(
newCurrency.name,
),
appLocalizations.prices_currency_change_proposal_action_cancel(
model.currency.name,
),
],
prefixIcons: <Widget>[
Icon(Icons.check_circle_rounded, color: extension.success),
Icon(Icons.cancel_rounded, color: extension.error),
],
values: <Currency?>[newCurrency, null],
);

if (currency != null) {
model.currency = currency;
}
}
}
}

Future<bool?> _doesAcceptWarning({required final bool justInfo}) async {
final AppLocalizations appLocalizations = AppLocalizations.of(context);
return showDialog<bool>(
Expand Down

0 comments on commit 7d606ff

Please sign in to comment.