From 903a2aab6fc5b4b0699a61a4afb21950ec331ab5 Mon Sep 17 00:00:00 2001 From: UgoKo-Rubeast <102586132+UgoKo-Rubeast@users.noreply.github.com> Date: Tue, 10 May 2022 00:14:42 +0900 Subject: [PATCH] feat: #849 Add language selection to dev mode (#1729) * feature/849-add-language-selection * feature/849-add-language-selection add language selection to dev mode * feature/849-add-language-selection add context.select to main.dart for watching locale * feature/849-add-language-selection flutter format * feature/849-add-language-selection add app language reset to dev mode * feature/849-add-language-selection change app language reset from platform.localeName to null Co-authored-by: Pierre Slamich --- .../lib/data_models/user_preferences.dart | 15 ++++++++++ packages/smooth_app/lib/l10n/app_en.arb | 4 +++ packages/smooth_app/lib/main.dart | 3 ++ .../lib/pages/user_preferences_dev_mode.dart | 30 +++++++++++++++++++ 4 files changed, 52 insertions(+) diff --git a/packages/smooth_app/lib/data_models/user_preferences.dart b/packages/smooth_app/lib/data_models/user_preferences.dart index 80a9ed564a2..20d9fbabd70 100644 --- a/packages/smooth_app/lib/data_models/user_preferences.dart +++ b/packages/smooth_app/lib/data_models/user_preferences.dart @@ -3,6 +3,7 @@ import 'package:openfoodfacts/personalized_search/preference_importance.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:smooth_app/data_models/product_preferences.dart'; import 'package:smooth_app/pages/onboarding/onboarding_flow_navigator.dart'; +import 'package:smooth_app/pages/user_preferences_dev_mode.dart'; class UserPreferences extends ChangeNotifier { UserPreferences._shared(final SharedPreferences sharedPreferences) @@ -89,6 +90,20 @@ class UserPreferences extends ChangeNotifier { : OnboardingPage.values[pageIndex]; } + Future setAppLanguageCode(String? languageCode) async { + if (languageCode == null) { + await _sharedPreferences + .remove(UserPreferencesDevMode.userPreferencesAppLanguageCode); + } else { + await setDevModeString( + UserPreferencesDevMode.userPreferencesAppLanguageCode, languageCode); + } + notifyListeners(); + } + + String? get appLanguageCode => + getDevModeString(UserPreferencesDevMode.userPreferencesAppLanguageCode); + String _getFlagTag(final String key) => _TAG_PREFIX_FLAG + key; Future setFlag( diff --git a/packages/smooth_app/lib/l10n/app_en.arb b/packages/smooth_app/lib/l10n/app_en.arb index 0f067fe9d79..3a696f4b3b7 100644 --- a/packages/smooth_app/lib/l10n/app_en.arb +++ b/packages/smooth_app/lib/l10n/app_en.arb @@ -988,6 +988,10 @@ "@dev_preferences_export_history_title": { "description": "User dev preferences - Export history - Title" }, + "dev_preferences_reset_app_language": "Reset App Language", + "@dev_preferences_reset_app_language": { + "description": "User dev preferences - Reset App Language - Title" + }, "dev_preferences_export_history_progress_error": "exception", "@dev_preferences_export_history_progress_error": { "description": "User dev preferences - Export history - Item - Error" diff --git a/packages/smooth_app/lib/main.dart b/packages/smooth_app/lib/main.dart index 5a2ad00394d..82f180252c2 100644 --- a/packages/smooth_app/lib/main.dart +++ b/packages/smooth_app/lib/main.dart @@ -180,8 +180,11 @@ class _SmoothAppState extends State { final ThemeProvider themeProvider = context.watch(); final Widget appWidget = OnboardingFlowNavigator(_userPreferences) .getPageWidget(context, _userPreferences.lastVisitedOnboardingPage); + final String? languageCode = + context.select((UserPreferences _up) => _up.appLanguageCode); return MaterialApp( + locale: languageCode != null ? Locale(languageCode) : null, localizationsDelegates: AppLocalizations.localizationsDelegates, supportedLocales: AppLocalizations.supportedLocales, debugShowCheckedModeBanner: !(kReleaseMode || _screenshots), diff --git a/packages/smooth_app/lib/pages/user_preferences_dev_mode.dart b/packages/smooth_app/lib/pages/user_preferences_dev_mode.dart index 391d54c0d86..0a5e34c64e2 100644 --- a/packages/smooth_app/lib/pages/user_preferences_dev_mode.dart +++ b/packages/smooth_app/lib/pages/user_preferences_dev_mode.dart @@ -46,6 +46,7 @@ class UserPreferencesDevMode extends AbstractUserPreferences { '__additionalButtonOnProductPage'; static const String userPreferencesFlagEditIngredients = '__editIngredients'; static const String userPreferencesEnumScanMode = '__scanMode'; + static const String userPreferencesAppLanguageCode = '__appLanguage'; static const String userPreferencesCameraPostFrameDuration = '__cameraPostFrameDuration'; @@ -335,6 +336,35 @@ class UserPreferencesDevMode extends AbstractUserPreferences { setState(() {}); }, ), + ListTile( + leading: const Icon(Icons.language), + title: DropdownButton( + value: userPreferences.appLanguageCode ?? + Localizations.localeOf(context).toString(), + elevation: 16, + isExpanded: true, + onChanged: (String? languageCode) async { + await userPreferences.setAppLanguageCode(languageCode); + setState(() {}); + }, + items: AppLocalizations.supportedLocales.map((Locale locale) { + final String _locale = locale.toString(); + return DropdownMenuItem( + value: _locale, + child: Text(_locale), + ); + }).toList(), + ), + ), + ListTile( + title: Text( + appLocalizations.dev_preferences_reset_app_language, + ), + onTap: () async { + await userPreferences.setAppLanguageCode(null); + setState(() {}); + }, + ), ]; ScaffoldFeatureController