Add persistence to currentLocale settings, only auto-detect initial language

This commit is contained in:
Shawn 2022-09-08 12:14:28 -06:00
parent 636153e6de
commit 32d6c9eb14
6 changed files with 20 additions and 15 deletions

View File

@ -27,25 +27,25 @@ class AppLogic {
await FlutterDisplayMode.setHighRefreshRate();
}
// Settings
await settingsLogic.load();
// Localizations
await localeLogic.load();
// Data load
// Wonders Data
wondersLogic.init();
// Timeline
// Events
timelineLogic.init();
// Settings
await settingsLogic.load();
// Collectibles
await collectiblesLogic.load();
// flag bootStrap as complete
// Flag bootStrap as complete
isBootstrapComplete = true;
// load initial view (replace empty initial view which is covered by a native splash screen)
// Load initial view (replace empty initial view which is covered by a native splash screen)
bool showIntro = settingsLogic.hasCompletedOnboarding.value == false;
if (showIntro) {
appRouter.go(ScreenPaths.intro);

View File

@ -12,7 +12,7 @@ class LocaleLogic {
bool get isEnglish => strings.localeName == 'en';
Future<void> load() async {
final localeCode = await findSystemLocale();
final localeCode = settingsLogic.currentLocale.value ?? await findSystemLocale();
Locale locale = Locale(localeCode.split('_')[0]);
if (kDebugMode) {
// Uncomment for testing in chinese
@ -25,8 +25,9 @@ class LocaleLogic {
_strings = await AppLocalizations.delegate.load(locale);
}
Future<void> refreshIfChanged(Locale locale) async {
if (_strings?.localeName != locale.languageCode && AppLocalizations.supportedLocales.contains(locale)) {
Future<void> loadIfChanged(Locale locale) async {
bool didChange = _strings?.localeName != locale.languageCode;
if (didChange && AppLocalizations.supportedLocales.contains(locale)) {
_strings = await AppLocalizations.delegate.load(locale);
}
}

View File

@ -8,7 +8,7 @@ class SettingsLogic with ThrottledSaveLoadMixin {
late final hasCompletedOnboarding = ValueNotifier<bool>(false)..addListener(scheduleSave);
late final hasDismissedSearchMessage = ValueNotifier<bool>(false)..addListener(scheduleSave);
late final currentLocale = ValueNotifier<String>('en')..addListener(scheduleSave);
late final currentLocale = ValueNotifier<String?>(null)..addListener(scheduleSave);
final bool useBlurs = defaultTargetPlatform != TargetPlatform.android;
@ -16,6 +16,7 @@ class SettingsLogic with ThrottledSaveLoadMixin {
void copyFromJson(Map<String, dynamic> value) {
hasCompletedOnboarding.value = value['hasCompletedOnboarding'] ?? false;
hasDismissedSearchMessage.value = value['hasDismissedSearchMessage'] ?? false;
currentLocale.value = value['currentLocale'];
}
@override
@ -23,12 +24,14 @@ class SettingsLogic with ThrottledSaveLoadMixin {
return {
'hasCompletedOnboarding': hasCompletedOnboarding.value,
'hasDismissedSearchMessage': hasDismissedSearchMessage.value,
'currentLocale': currentLocale.value,
};
}
Future<void> setLocale(Locale value) async {
Future<void> changeLocale(Locale value) async {
currentLocale.value = value.languageCode;
await localeLogic.refreshIfChanged(value);
await localeLogic.loadIfChanged(value);
// Re-init controllers that have some cached data that is localized
wondersLogic.init();
timelineLogic.init();
}

View File

@ -6,6 +6,7 @@ class TimelineLogic {
List<TimelineEvent> events = [];
void init() {
// Create an event for each wonder, and merge it with the list of GlobalEvents
events = [
...GlobalEventsData().globalEvents,
...wondersLogic.all.map(

View File

@ -32,7 +32,7 @@ class WondersApp extends StatelessWidget with GetItMixin {
Widget build(BuildContext context) {
final locale = watchX((SettingsLogic s) => s.currentLocale);
return MaterialApp.router(
locale: Locale(locale),
locale: locale == null ? null : Locale(locale),
debugShowCheckedModeBanner: false,
routerDelegate: appRouter.routerDelegate,
routeInformationProvider: appRouter.routeInformationProvider,

View File

@ -8,7 +8,7 @@ class LocaleSwitcher extends StatelessWidget with GetItMixin {
final locale = watchX((SettingsLogic s) => s.currentLocale);
Future<void> handleSwapLocale() async {
final newLocale = Locale(locale == 'en' ? 'zh' : 'en');
await settingsLogic.setLocale(newLocale);
await settingsLogic.changeLocale(newLocale);
}
return AppBtn.from(