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(); await FlutterDisplayMode.setHighRefreshRate();
} }
// Settings
await settingsLogic.load();
// Localizations // Localizations
await localeLogic.load(); await localeLogic.load();
// Data load // Wonders Data
wondersLogic.init(); wondersLogic.init();
// Timeline // Events
timelineLogic.init(); timelineLogic.init();
// Settings
await settingsLogic.load();
// Collectibles // Collectibles
await collectiblesLogic.load(); await collectiblesLogic.load();
// flag bootStrap as complete // Flag bootStrap as complete
isBootstrapComplete = true; 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; bool showIntro = settingsLogic.hasCompletedOnboarding.value == false;
if (showIntro) { if (showIntro) {
appRouter.go(ScreenPaths.intro); appRouter.go(ScreenPaths.intro);

View File

@ -12,7 +12,7 @@ class LocaleLogic {
bool get isEnglish => strings.localeName == 'en'; bool get isEnglish => strings.localeName == 'en';
Future<void> load() async { Future<void> load() async {
final localeCode = await findSystemLocale(); final localeCode = settingsLogic.currentLocale.value ?? await findSystemLocale();
Locale locale = Locale(localeCode.split('_')[0]); Locale locale = Locale(localeCode.split('_')[0]);
if (kDebugMode) { if (kDebugMode) {
// Uncomment for testing in chinese // Uncomment for testing in chinese
@ -25,8 +25,9 @@ class LocaleLogic {
_strings = await AppLocalizations.delegate.load(locale); _strings = await AppLocalizations.delegate.load(locale);
} }
Future<void> refreshIfChanged(Locale locale) async { Future<void> loadIfChanged(Locale locale) async {
if (_strings?.localeName != locale.languageCode && AppLocalizations.supportedLocales.contains(locale)) { bool didChange = _strings?.localeName != locale.languageCode;
if (didChange && AppLocalizations.supportedLocales.contains(locale)) {
_strings = await AppLocalizations.delegate.load(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 hasCompletedOnboarding = ValueNotifier<bool>(false)..addListener(scheduleSave);
late final hasDismissedSearchMessage = 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; final bool useBlurs = defaultTargetPlatform != TargetPlatform.android;
@ -16,6 +16,7 @@ class SettingsLogic with ThrottledSaveLoadMixin {
void copyFromJson(Map<String, dynamic> value) { void copyFromJson(Map<String, dynamic> value) {
hasCompletedOnboarding.value = value['hasCompletedOnboarding'] ?? false; hasCompletedOnboarding.value = value['hasCompletedOnboarding'] ?? false;
hasDismissedSearchMessage.value = value['hasDismissedSearchMessage'] ?? false; hasDismissedSearchMessage.value = value['hasDismissedSearchMessage'] ?? false;
currentLocale.value = value['currentLocale'];
} }
@override @override
@ -23,12 +24,14 @@ class SettingsLogic with ThrottledSaveLoadMixin {
return { return {
'hasCompletedOnboarding': hasCompletedOnboarding.value, 'hasCompletedOnboarding': hasCompletedOnboarding.value,
'hasDismissedSearchMessage': hasDismissedSearchMessage.value, 'hasDismissedSearchMessage': hasDismissedSearchMessage.value,
'currentLocale': currentLocale.value,
}; };
} }
Future<void> setLocale(Locale value) async { Future<void> changeLocale(Locale value) async {
currentLocale.value = value.languageCode; 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(); wondersLogic.init();
timelineLogic.init(); timelineLogic.init();
} }

View File

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

View File

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

View File

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