Skip to content

Commit

Permalink
feat: #849 Add language selection to dev mode (#1729)
Browse files Browse the repository at this point in the history
* 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 <pierre@openfoodfacts.org>
  • Loading branch information
UgoKo-Rubeast and teolemon authored May 9, 2022
1 parent a9bab93 commit 903a2aa
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/smooth_app/lib/data_models/user_preferences.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -89,6 +90,20 @@ class UserPreferences extends ChangeNotifier {
: OnboardingPage.values[pageIndex];
}

Future<void> 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<void> setFlag(
Expand Down
4 changes: 4 additions & 0 deletions packages/smooth_app/lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions packages/smooth_app/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,11 @@ class _SmoothAppState extends State<SmoothApp> {
final ThemeProvider themeProvider = context.watch<ThemeProvider>();
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),
Expand Down
30 changes: 30 additions & 0 deletions packages/smooth_app/lib/pages/user_preferences_dev_mode.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -335,6 +336,35 @@ 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 {
await userPreferences.setAppLanguageCode(languageCode);
setState(() {});
},
items: AppLocalizations.supportedLocales.map((Locale locale) {
final String _locale = locale.toString();
return DropdownMenuItem<String>(
value: _locale,
child: Text(_locale),
);
}).toList(),
),
),
ListTile(
title: Text(
appLocalizations.dev_preferences_reset_app_language,
),
onTap: () async {
await userPreferences.setAppLanguageCode(null);
setState(() {});
},
),
];

ScaffoldFeatureController<SnackBar, SnackBarClosedReason>
Expand Down

0 comments on commit 903a2aa

Please sign in to comment.