From 32d6c9eb14498e38f05fae4c7b158110268adec0 Mon Sep 17 00:00:00 2001 From: Shawn Date: Thu, 8 Sep 2022 12:14:28 -0600 Subject: [PATCH] Add persistence to currentLocale settings, only auto-detect initial language --- lib/logic/app_logic.dart | 14 +++++++------- lib/logic/locale_logic.dart | 7 ++++--- lib/logic/settings_logic.dart | 9 ++++++--- lib/logic/timeline_logic.dart | 1 + lib/main.dart | 2 +- lib/ui/common/controls/locale_switcher.dart | 2 +- 6 files changed, 20 insertions(+), 15 deletions(-) diff --git a/lib/logic/app_logic.dart b/lib/logic/app_logic.dart index 1bc93ce3..16ee9e39 100644 --- a/lib/logic/app_logic.dart +++ b/lib/logic/app_logic.dart @@ -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); diff --git a/lib/logic/locale_logic.dart b/lib/logic/locale_logic.dart index 509dd884..92e3181f 100644 --- a/lib/logic/locale_logic.dart +++ b/lib/logic/locale_logic.dart @@ -12,7 +12,7 @@ class LocaleLogic { bool get isEnglish => strings.localeName == 'en'; Future 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 refreshIfChanged(Locale locale) async { - if (_strings?.localeName != locale.languageCode && AppLocalizations.supportedLocales.contains(locale)) { + Future loadIfChanged(Locale locale) async { + bool didChange = _strings?.localeName != locale.languageCode; + if (didChange && AppLocalizations.supportedLocales.contains(locale)) { _strings = await AppLocalizations.delegate.load(locale); } } diff --git a/lib/logic/settings_logic.dart b/lib/logic/settings_logic.dart index 23a19059..3a08c31a 100644 --- a/lib/logic/settings_logic.dart +++ b/lib/logic/settings_logic.dart @@ -8,7 +8,7 @@ class SettingsLogic with ThrottledSaveLoadMixin { late final hasCompletedOnboarding = ValueNotifier(false)..addListener(scheduleSave); late final hasDismissedSearchMessage = ValueNotifier(false)..addListener(scheduleSave); - late final currentLocale = ValueNotifier('en')..addListener(scheduleSave); + late final currentLocale = ValueNotifier(null)..addListener(scheduleSave); final bool useBlurs = defaultTargetPlatform != TargetPlatform.android; @@ -16,6 +16,7 @@ class SettingsLogic with ThrottledSaveLoadMixin { void copyFromJson(Map 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 setLocale(Locale value) async { + Future 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(); } diff --git a/lib/logic/timeline_logic.dart b/lib/logic/timeline_logic.dart index 8e1e6808..0b70be1e 100644 --- a/lib/logic/timeline_logic.dart +++ b/lib/logic/timeline_logic.dart @@ -6,6 +6,7 @@ class TimelineLogic { List events = []; void init() { + // Create an event for each wonder, and merge it with the list of GlobalEvents events = [ ...GlobalEventsData().globalEvents, ...wondersLogic.all.map( diff --git a/lib/main.dart b/lib/main.dart index 5b3ad375..1fd6199f 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -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, diff --git a/lib/ui/common/controls/locale_switcher.dart b/lib/ui/common/controls/locale_switcher.dart index 9150b9c2..1e206fa7 100644 --- a/lib/ui/common/controls/locale_switcher.dart +++ b/lib/ui/common/controls/locale_switcher.dart @@ -8,7 +8,7 @@ class LocaleSwitcher extends StatelessWidget with GetItMixin { final locale = watchX((SettingsLogic s) => s.currentLocale); Future handleSwapLocale() async { final newLocale = Locale(locale == 'en' ? 'zh' : 'en'); - await settingsLogic.setLocale(newLocale); + await settingsLogic.changeLocale(newLocale); } return AppBtn.from(