Skip to content

refactor: migrate from iso_countries to l10n_countries #6541

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

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import 'package:auto_size_text/auto_size_text.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart' hide Listener;
import 'package:flutter/services.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:iso_countries/iso_countries.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:provider/provider.dart';
import 'package:smooth_app/data_models/preferences/user_preferences.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/helpers/provider_helper.dart';
import 'package:smooth_app/pages/prices/emoji_helper.dart';
import 'package:smooth_app/services/smooth_services.dart';
import 'package:smooth_app/pages/preferences/country_selector/openfoodfacts_country_emoji_extension.dart';
import 'package:smooth_app/pages/preferences/country_selector/openfoodfacts_country_name_extension.dart';
import 'package:smooth_app/query/product_query.dart';
import 'package:smooth_app/widgets/selector_screen/smooth_screen_list_choice.dart';
import 'package:smooth_app/widgets/selector_screen/smooth_screen_selector_provider.dart';
import 'package:smooth_app/widgets/smooth_text.dart';
Expand Down Expand Up @@ -50,13 +48,14 @@ class CountrySelector extends StatelessWidget {
child: Consumer<_CountrySelectorProvider>(
builder: (BuildContext context, _CountrySelectorProvider provider, _) {
return switch (provider.value) {
PreferencesSelectorLoadingState<Country> _ => SizedBox(
PreferencesSelectorLoadingState<OpenFoodFactsCountry> _ => SizedBox(
height: loadingHeight,
child: const Center(
child: CircularProgressIndicator.adaptive(),
),
),
PreferencesSelectorLoadedState<Country> _ => _CountrySelectorButton(
PreferencesSelectorLoadedState<OpenFoodFactsCountry> _ =>
_CountrySelectorButton(
icon: icon,
innerPadding: padding ?? EdgeInsets.zero,
textStyle: textStyle,
Expand Down Expand Up @@ -100,19 +99,24 @@ class _CountrySelectorButton extends StatelessWidget {
child: ConstrainedBox(
constraints: const BoxConstraints(minHeight: 40.0),
child: ConsumerValueNotifierFilter<_CountrySelectorProvider,
PreferencesSelectorState<Country>>(
buildWhen: (PreferencesSelectorState<Country>? previousValue,
PreferencesSelectorState<Country> currentValue) =>
previousValue != null &&
currentValue is! PreferencesSelectorEditingState &&
(currentValue as PreferencesSelectorLoadedState<Country>)
.selectedItem !=
(previousValue as PreferencesSelectorLoadedState<Country>)
.selectedItem,
builder: (_, PreferencesSelectorState<Country> value, __) {
final Country? country =
(value as PreferencesSelectorLoadedState<Country>)
.selectedItem;
PreferencesSelectorState<OpenFoodFactsCountry>>(
buildWhen:
(PreferencesSelectorState<OpenFoodFactsCountry>? previousValue,
PreferencesSelectorState<OpenFoodFactsCountry>
currentValue) =>
previousValue != null &&
currentValue is! PreferencesSelectorEditingState &&
(currentValue as PreferencesSelectorLoadedState<
OpenFoodFactsCountry>)
.selectedItem !=
(previousValue as PreferencesSelectorLoadedState<
OpenFoodFactsCountry>)
.selectedItem,
builder:
(_, PreferencesSelectorState<OpenFoodFactsCountry> value, __) {
final OpenFoodFactsCountry? country = (value
as PreferencesSelectorLoadedState<OpenFoodFactsCountry>)
.selectedItem;

return Padding(
padding: innerPadding,
Expand All @@ -122,8 +126,7 @@ class _CountrySelectorButton extends StatelessWidget {
SizedBox(
width: IconTheme.of(context).size! + LARGE_SPACE,
child: AutoSizeText(
EmojiHelper.getEmojiByCountryCode(
country.countryCode)!,
country.emoji,
textAlign: TextAlign.center,
style:
TextStyle(fontSize: IconTheme.of(context).size),
Expand Down Expand Up @@ -191,20 +194,18 @@ class _CountrySelectorButton extends StatelessWidget {
/// (eg: the user uses the Android back button).
if (newCountry == null) {
context.read<_CountrySelectorProvider>().dismissSelectedItem();
} else if (newCountry is Country) {
} else if (newCountry is OpenFoodFactsCountry) {
_changeCurrencyIfRelevant(context, newCountry);
}
}

// TODO(g123k): move this to a dedicated Provider
Future<void> _changeCurrencyIfRelevant(
final BuildContext context,
final Country country,
final OpenFoodFactsCountry country,
) async {
final UserPreferences userPreferences = context.read<UserPreferences>();
final OpenFoodFactsCountry? offCountry =
OpenFoodFactsCountry.fromOffTag(country.countryCode);
final String? possibleCurrencyCode = offCountry?.currency?.name;
final String? possibleCurrencyCode = country.currency?.name;

if (possibleCurrencyCode == null) {
return;
Expand Down Expand Up @@ -259,12 +260,12 @@ class _CountrySelectorScreen extends StatelessWidget {
Widget build(BuildContext context) {
final AppLocalizations appLocalizations = AppLocalizations.of(context);

return SmoothSelectorScreen<Country>(
return SmoothSelectorScreen<OpenFoodFactsCountry>(
provider: provider,
title: appLocalizations.country_selector_title,
itemBuilder: (
BuildContext context,
Country country,
OpenFoodFactsCountry country,
bool selected,
String filter,
) {
Expand All @@ -273,14 +274,14 @@ class _CountrySelectorScreen extends StatelessWidget {
Expanded(
flex: 1,
child: Text(
EmojiHelper.getEmojiByCountryCode(country.countryCode) ?? '',
country.emoji,
style: const TextStyle(fontSize: 25.0),
),
),
Expanded(
flex: 2,
child: Text(
country.countryCode.toUpperCase(),
country.offTag.toUpperCase(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same UK potential issue.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use iso2 instead.

textAlign: TextAlign.center,
maxLines: 1,
overflow: TextOverflow.ellipsis,
Expand All @@ -289,7 +290,9 @@ class _CountrySelectorScreen extends StatelessWidget {
Expanded(
flex: 7,
child: TextHighlighter(
text: country.name,
text:
country.getLocalizedName(ProductQuery.getLanguage().code) ??
'',
filter: filter,
textStyle: const TextStyle(
fontWeight: FontWeight.w600,
Expand All @@ -299,35 +302,35 @@ class _CountrySelectorScreen extends StatelessWidget {
],
);
},
itemsFilter: (List<Country> list, Country? selectedItem,
Country? selectedItemOverride, String filter) =>
itemsFilter: (List<OpenFoodFactsCountry> list,
OpenFoodFactsCountry? selectedItem,
OpenFoodFactsCountry? selectedItemOverride,
String filter) =>
_filterCountries(
list,
selectedItem,
selectedItemOverride,
filter,
),
list, selectedItem, selectedItemOverride, filter, context),
);
}

Iterable<Country> _filterCountries(
List<Country> countries,
Country? userCountry,
Country? selectedCountry,
String? filter,
) {
Iterable<OpenFoodFactsCountry> _filterCountries(
List<OpenFoodFactsCountry> countries,
OpenFoodFactsCountry? userCountry,
OpenFoodFactsCountry? selectedCountry,
String? filter,
BuildContext context) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
BuildContext context) {
) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove context as we don't need it.

if (filter == null || filter.isEmpty) {
return countries;
}

return countries.where(
(Country country) =>
(OpenFoodFactsCountry country) =>
country == userCountry ||
country == selectedCountry ||
country.name.toLowerCase().contains(
filter.toLowerCase(),
) ||
country.countryCode.toLowerCase().contains(
(country
.getLocalizedName(ProductQuery.getLanguage().code)
?.toLowerCase()
.contains(filter.toLowerCase()) ??
false) ||
country.offTag.toLowerCase().contains(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

offTag or iso2code? Or both?

filter.toLowerCase(),
),
);
Expand Down
Loading