Skip to content

Commit

Permalink
feat: Language filter (openfoodfacts#2539)
Browse files Browse the repository at this point in the history
* feat: language filter

* feat: dev mode language search

* Oop, Open food facts Language used

* language picker seperated.

* localize choose app language

* fix: language filter changes requested

* fix: further optimization

Co-authored-by: Pierre Slamich <pierre@openfoodfacts.org>
  • Loading branch information
abughalib and teolemon authored Jul 22, 2022
1 parent 6b8da74 commit d856b35
Show file tree
Hide file tree
Showing 4 changed files with 348 additions and 21 deletions.
113 changes: 113 additions & 0 deletions packages/smooth_app/lib/generic_lib/widgets/language_selector.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart'
show AppLocalizations;
import 'package:openfoodfacts/utils/LanguageHelper.dart';
import 'package:smooth_app/data_models/user_preferences.dart';
import 'package:smooth_app/generic_lib/dialogs/smooth_alert_dialog.dart';
import 'package:smooth_app/pages/preferences/user_preferences_languages_list.dart';

class LanguageSelectorSettings extends StatelessWidget {
const LanguageSelectorSettings({
required this.userPreferences,
required this.appLocalizations,
});

final UserPreferences userPreferences;
final AppLocalizations appLocalizations;

static final Languages _languages = Languages();

@override
Widget build(BuildContext context) {
// The languages that are supported by flutter widget
final String currentLanguageCode = userPreferences.appLanguageCode ??
Localizations.localeOf(context).toString();
final String nameInLanguage =
_languages.getLanguageNameInEnglishFromOpenFoodFactsLanguage(
LanguageHelper.fromJson(currentLanguageCode));
return ListTile(
leading: const Icon(Icons.language),
title: Text(
appLocalizations.choose_app_language,
),
subtitle: Text(
'$nameInLanguage ($currentLanguageCode)',
softWrap: false,
overflow: TextOverflow.fade,
),
onTap: () async {
final List<OpenFoodFactsLanguage> leftovers =
_languages.getSupportedLanguagesNameInEnglish();
leftovers.sort((OpenFoodFactsLanguage a, OpenFoodFactsLanguage b) =>
_languages
.getLanguageNameInEnglishFromOpenFoodFactsLanguage(a)
.compareTo(_languages
.getLanguageNameInEnglishFromOpenFoodFactsLanguage(b)));
List<OpenFoodFactsLanguage> filteredList = leftovers;
await showDialog<void>(
context: context,
builder: (BuildContext context) {
return StatefulBuilder(
builder: (BuildContext context,
void Function(VoidCallback fn) setState) {
return SmoothAlertDialog(
body: Column(
children: <Widget>[
TextField(
decoration: InputDecoration(
prefixIcon: const Icon(Icons.search),
enabledBorder: const UnderlineInputBorder(),
labelText: appLocalizations.search,
),
onChanged: (String query) {
setState(
() {
filteredList = leftovers
.where((OpenFoodFactsLanguage item) =>
_languages
.getLanguageNameInEnglishFromOpenFoodFactsLanguage(
item)
.toLowerCase()
.contains(query.toLowerCase()) ||
item.code.contains(query))
.toList();
},
);
},
),
...List<ListTile>.generate(
filteredList.length,
(int index) {
final OpenFoodFactsLanguage openFoodFactsLanguage =
filteredList[index];
final String nameInLanguage = _languages
.getLanguageNameInLanguageFromOpenFoodFactsLanguage(
openFoodFactsLanguage);
return ListTile(
title: Text(
'$nameInLanguage (${_languages.getLanguageNameInEnglishFromOpenFoodFactsLanguage(openFoodFactsLanguage)})',
softWrap: false,
overflow: TextOverflow.fade,
),
onTap: () {
userPreferences.setAppLanguageCode(
openFoodFactsLanguage.code);
Navigator.of(context).pop();
},
);
},
),
],
),
positiveAction: SmoothActionButton(
onPressed: () => Navigator.pop(context),
text: appLocalizations.cancel,
),
);
},
);
});
},
);
}
}
6 changes: 5 additions & 1 deletion packages/smooth_app/lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -1543,7 +1543,7 @@
"description": "Button label for taking a photo"
},
"choose_from_gallery": "Choose from gallery",
"@choose from gallery": {
"@choose_from_gallery": {
"description": "Button label for choosing a photo from gallery"
},
"expand_nutrition_facts": "Expand nutrition facts table",
Expand All @@ -1559,5 +1559,9 @@
"no_internet_connection": "No internet connection",
"@no_internet_connection": {
"description": "Message when there is no internet connection"
},
"choose_app_language": "Choose App Language",
"@choose_app_language": {
"description": "Choose Application Language"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:smooth_app/data_models/user_preferences.dart';
import 'package:smooth_app/database/dao_product_list.dart';
import 'package:smooth_app/database/local_database.dart';
import 'package:smooth_app/generic_lib/dialogs/smooth_alert_dialog.dart';
import 'package:smooth_app/generic_lib/widgets/language_selector.dart';
import 'package:smooth_app/helpers/data_importer/product_list_import_export.dart';
import 'package:smooth_app/helpers/data_importer/smooth_app_data_importer.dart';
import 'package:smooth_app/pages/onboarding/onboarding_flow_navigator.dart';
Expand Down Expand Up @@ -60,9 +61,6 @@ class UserPreferencesDevMode extends AbstractUserPreferences {

static const LocalizationsDelegate<MaterialLocalizations> delegate =
GlobalMaterialLocalizations.delegate;
final List<Locale> _supportedLanguageCodes = AppLocalizations.supportedLocales
.where((Locale locale) => delegate.isSupported(locale))
.toList();

@override
PreferencePageType? getPreferencePageType() => PreferencePageType.DEV_MODE;
Expand Down Expand Up @@ -335,23 +333,9 @@ class UserPreferencesDevMode extends AbstractUserPreferences {
setState(() {});
},
),
ListTile(
leading: const Icon(Icons.language),
title: DropdownButton<String>(
value: userPreferences.appLanguageCode ??
Localizations.localeOf(context).toString(),
elevation: 16,
isExpanded: true,
onChanged: (String? languageCode) async =>
userPreferences.setAppLanguageCode(languageCode),
items: _supportedLanguageCodes.map((Locale locale) {
final String localeString = locale.toString();
return DropdownMenuItem<String>(
value: localeString,
child: Text(localeString),
);
}).toList(),
),
LanguageSelectorSettings(
userPreferences: userPreferences,
appLocalizations: appLocalizations,
),
ListTile(
// Do not translate
Expand Down
Loading

0 comments on commit d856b35

Please sign in to comment.