From 48f5cc2b6515bbad0eb87838c6e9712062ad1604 Mon Sep 17 00:00:00 2001 From: Moritz Oberhauser Date: Wed, 12 Jun 2024 10:38:08 +0200 Subject: [PATCH] refactor: Parallelize LoadingBuilders in Settings --- .../view/screens/more/settings/settings.dart | 230 +++++++++--------- 1 file changed, 114 insertions(+), 116 deletions(-) diff --git a/wrestling_scoreboard_client/lib/view/screens/more/settings/settings.dart b/wrestling_scoreboard_client/lib/view/screens/more/settings/settings.dart index 1ecc252f..c28f7de2 100644 --- a/wrestling_scoreboard_client/lib/view/screens/more/settings/settings.dart +++ b/wrestling_scoreboard_client/lib/view/screens/more/settings/settings.dart @@ -59,129 +59,127 @@ class CustomSettingsScreen extends ConsumerWidget { } } + Future<(Locale?, ThemeMode, String?)> loadGeneralSettings() async { + final results = await Future.wait([ + ref.watch(localeNotifierProvider), + ref.watch(themeModeNotifierProvider), + ref.watch(fontFamilyNotifierProvider) + ]); + + return (results[0] as Locale?, results[1] as ThemeMode, results[2] as String?); + } + return WindowStateScaffold( appBarTitle: Text(localizations.settings), body: ResponsiveColumn( children: [ - LoadingBuilder( - future: ref.watch(localeNotifierProvider), - builder: (context, locale) { - return LoadingBuilder( - future: ref.watch(themeModeNotifierProvider), - builder: (context, themeMode) { - return LoadingBuilder( - future: ref.watch(fontFamilyNotifierProvider), - builder: (context, fontFamily) { - return SettingsSection( - title: localizations.general, - action: TextButton( - onPressed: () async { - locale = null; - await ref.read(localeNotifierProvider.notifier).setState(locale); + LoadingBuilder<(Locale?, ThemeMode, String?)>( + future: loadGeneralSettings(), + builder: (context, generalSettings) { + var (locale, themeMode, fontFamily) = generalSettings; - themeMode = ThemeMode.system; - await ref.read(themeModeNotifierProvider.notifier).setState(themeMode); + return SettingsSection( + title: localizations.general, + action: TextButton( + onPressed: () async { + locale = null; + await ref.read(localeNotifierProvider.notifier).setState(locale); - const defaultFontFamily = 'Roboto'; - await ref.read(fontFamilyNotifierProvider.notifier).setState(defaultFontFamily); - }, - child: Text(localizations.reset), - ), - children: [ - ListTile( - leading: const Icon(Icons.translate), - title: Text(localizations.language + (isDisplayInternational() ? ' | Language' : '')), - subtitle: Text(getTranslationOfLocale(locale)), - onTap: () async { - final val = await showDialog( - context: context, - builder: (BuildContext context) { - final List> languageSettingValues = - Preferences.supportedLanguages.map((locale) { - return MapEntry(locale, getTranslationOfLocale(locale)); - }).toList(); - languageSettingValues.insert(0, MapEntry(null, getTranslationOfLocale())); - return RadioDialog(values: languageSettingValues, initialValue: locale); - }, - ); - await ref.read(localeNotifierProvider.notifier).setState(val); - }, - ), - ListTile( - leading: const Icon(Icons.brush), - title: Text(localizations.themeMode), - subtitle: Text(getTranslationOfThemeMode(themeMode)), - onTap: () async { - final val = await showDialog( - context: context, - builder: (BuildContext context) { - final List> themeModeValues = ThemeMode.values - .map((value) => - MapEntry(value, getTranslationOfThemeMode(value))) - .toList(); - return RadioDialog(values: themeModeValues, initialValue: themeMode); - }, - ); - if (val != null) { - await ref.read(themeModeNotifierProvider.notifier).setState(val); - } - }, - ), - ListTile( - leading: const Icon(Icons.abc), - trailing: IconButton( - tooltip: 'Google Fonts', - icon: const Icon(Icons.link), - onPressed: () => launchUrl(Uri.parse('https://fonts.google.com/')), - ), - title: Text(localizations.fontFamily), - subtitle: Text(fontFamily ?? localizations.systemSetting), - onTap: () async { - final currentTextTheme = Theme.of(context).textTheme; - final val = await showDialog( - context: context, - builder: (BuildContext context) { - final List> fontFamilies = GoogleFonts.asMap() - .keys - .map((String e) => MapEntry(e, e)) - .toList(); - fontFamilies.insert( - 0, MapEntry(null, localizations.systemSetting)); - return RadioDialog( - itemCount: fontFamilies.length, - builder: (index) { - final fontFamily = fontFamilies[index]; - return ( - fontFamily.key, - Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ - Text(fontFamily.value), - IconButton( - onPressed: () => showOkDialog( - context: context, - child: Text('ABCDEFGHIJKLMNOPQRSTUVWXYZ\nabcdefghijklmnopqrstuvwxyz', - style: fontFamily.key != null - ? GoogleFonts.getTextTheme(fontFamily.key!, currentTextTheme) - .headlineMedium - : null), - ), - icon: const Icon(Icons.visibility), - ), - ]) - ); - }, - initialValue: fontFamily); - }, - ); - await ref.read(fontFamilyNotifierProvider.notifier).setState(val); - }, - ), - ], + themeMode = ThemeMode.system; + await ref.read(themeModeNotifierProvider.notifier).setState(themeMode); + + const defaultFontFamily = 'Roboto'; + await ref.read(fontFamilyNotifierProvider.notifier).setState(defaultFontFamily); + }, + child: Text(localizations.reset), + ), + children: [ + ListTile( + leading: const Icon(Icons.translate), + title: Text(localizations.language + (isDisplayInternational() ? ' | Language' : '')), + subtitle: Text(getTranslationOfLocale(locale)), + onTap: () async { + final val = await showDialog( + context: context, + builder: (BuildContext context) { + final List> languageSettingValues = + Preferences.supportedLanguages.map((locale) { + return MapEntry(locale, getTranslationOfLocale(locale)); + }).toList(); + languageSettingValues.insert(0, MapEntry(null, getTranslationOfLocale())); + return RadioDialog(values: languageSettingValues, initialValue: locale); + }, ); - }); - }, - ); - }, - ), + await ref.read(localeNotifierProvider.notifier).setState(val); + }, + ), + ListTile( + leading: const Icon(Icons.brush), + title: Text(localizations.themeMode), + subtitle: Text(getTranslationOfThemeMode(themeMode)), + onTap: () async { + final val = await showDialog( + context: context, + builder: (BuildContext context) { + final List> themeModeValues = ThemeMode.values + .map((value) => MapEntry(value, getTranslationOfThemeMode(value))) + .toList(); + return RadioDialog(values: themeModeValues, initialValue: themeMode); + }, + ); + if (val != null) { + await ref.read(themeModeNotifierProvider.notifier).setState(val); + } + }, + ), + ListTile( + leading: const Icon(Icons.abc), + trailing: IconButton( + tooltip: 'Google Fonts', + icon: const Icon(Icons.link), + onPressed: () => launchUrl(Uri.parse('https://fonts.google.com/')), + ), + title: Text(localizations.fontFamily), + subtitle: Text(fontFamily ?? localizations.systemSetting), + onTap: () async { + final currentTextTheme = Theme.of(context).textTheme; + final val = await showDialog( + context: context, + builder: (BuildContext context) { + final List> fontFamilies = + GoogleFonts.asMap().keys.map((String e) => MapEntry(e, e)).toList(); + fontFamilies.insert(0, MapEntry(null, localizations.systemSetting)); + return RadioDialog( + itemCount: fontFamilies.length, + builder: (index) { + final fontFamily = fontFamilies[index]; + return ( + fontFamily.key, + Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ + Text(fontFamily.value), + IconButton( + onPressed: () => showOkDialog( + context: context, + child: Text('ABCDEFGHIJKLMNOPQRSTUVWXYZ\nabcdefghijklmnopqrstuvwxyz', + style: fontFamily.key != null + ? GoogleFonts.getTextTheme(fontFamily.key!, currentTextTheme) + .headlineMedium + : null), + ), + icon: const Icon(Icons.visibility), + ), + ]) + ); + }, + initialValue: fontFamily); + }, + ); + await ref.read(fontFamilyNotifierProvider.notifier).setState(val); + }, + ), + ], + ); + }), LoadingBuilder( future: ref.watch(bellSoundNotifierProvider), builder: (context, bellSoundPath) {