diff --git a/reference/ToDo/src/ToDo/Presentation/SettingsViewModel.cs b/reference/ToDo/src/ToDo/Presentation/SettingsViewModel.cs index 8e33a46d2..5e7a0e667 100644 --- a/reference/ToDo/src/ToDo/Presentation/SettingsViewModel.cs +++ b/reference/ToDo/src/ToDo/Presentation/SettingsViewModel.cs @@ -10,8 +10,7 @@ public partial class SettingsViewModel private readonly INavigator _sourceNavigator; private readonly INavigator _navigator; private readonly IThemeService _themeService; - - private bool _isDark; + private readonly IDispatcher _dispatcher; public ILocalizationService LocalizationSettings { get; } @@ -36,13 +35,10 @@ public SettingsViewModel( _userSvc = userSvc; LocalizationSettings = localizationSettings; _themeService = themeService; + _dispatcher = dispatcher; AppThemes = new string[] { localizer["SettingsFlyout_ThemeLight"], localizer["SettingsFlyout_ThemeDark"] }; - _ = dispatcher.TryEnqueue(() => { - _isDark = _themeService.IsDark; - }); - Cultures = localizationConfiguration.Value!.Cultures!.Select(c => new DisplayCulture(localizer[$"SettingsFlyout_LanguageLabel_{c}"], c)).ToArray(); SelectedCulture = State.Value(this, () => Cultures.FirstOrDefault(c => c.Culture == LocalizationSettings.CurrentCulture.ToString()) ?? Cultures.First()); @@ -56,9 +52,13 @@ public SettingsViewModel( [Value] public IState SelectedCulture { get; } - + [Value] - public IState SelectedAppTheme => State.Value(this, () => AppThemes[_isDark ? 1 : 0]); + public IState SelectedAppTheme => State.Async(this, async ct => + { + var isDark = await _dispatcher.ExecuteAsync(async _ => _themeService.IsDark, ct); + return AppThemes[isDark ? 1 : 0]; + }); public async ValueTask SignOut(CancellationToken ct) { @@ -70,13 +70,16 @@ public async ValueTask SignOut(CancellationToken ct) await _sourceNavigator.NavigateViewModelAsync(this); } } - - public async ValueTask ChangeAppTheme(CancellationToken ct) + + public async ValueTask ChangeAppTheme(string selectedTheme, CancellationToken ct) { - var currentTheme = _themeService.Theme; - var newTheme = currentTheme == AppTheme.Dark ? AppTheme.Light : AppTheme.Dark; - await _themeService.SetThemeAsync(newTheme); - WeakReferenceMessenger.Default.Send(new ThemeChangedMessage(newTheme)); + var newTheme = selectedTheme == AppThemes[1] ? AppTheme.Dark : AppTheme.Light; + var currentTheme = _themeService.IsDark ? AppThemes[1] : AppThemes[0]; + if (selectedTheme != currentTheme) + { + await _themeService.SetThemeAsync(newTheme); + WeakReferenceMessenger.Default.Send(new ThemeChangedMessage(newTheme)); + } } private async ValueTask ChangeLanguage(DisplayCulture? culture, CancellationToken ct) diff --git a/reference/ToDo/src/ToDo/Views/Dialogs/SettingsFlyout.xaml b/reference/ToDo/src/ToDo/Views/Dialogs/SettingsFlyout.xaml index f22728925..6cb9d9cf0 100644 --- a/reference/ToDo/src/ToDo/Views/Dialogs/SettingsFlyout.xaml +++ b/reference/ToDo/src/ToDo/Views/Dialogs/SettingsFlyout.xaml @@ -103,7 +103,7 @@ diff --git a/reference/ToDo/src/ToDo/Views/Dialogs/SettingsFlyout.xaml.cs b/reference/ToDo/src/ToDo/Views/Dialogs/SettingsFlyout.xaml.cs index 99beccfb7..cc9e41a44 100644 --- a/reference/ToDo/src/ToDo/Views/Dialogs/SettingsFlyout.xaml.cs +++ b/reference/ToDo/src/ToDo/Views/Dialogs/SettingsFlyout.xaml.cs @@ -6,9 +6,7 @@ internal record ThemeChangedMessage(AppTheme Theme) public sealed partial class SettingsFlyout : Flyout, IRecipient { -#if ANDROID private bool _isThemeInitialized = false; -#endif public SettingsFlyout() { @@ -20,14 +18,13 @@ private void ThemeChipGroup_ItemChecked(object sender, ChipItemEventArgs e) { if (FlyoutRoot.DataContext is BindableSettingsViewModel viewModel) { -#if ANDROID if (_isThemeInitialized) { - viewModel.ChangeAppTheme.Execute(null); + viewModel.ChangeAppTheme.Execute(e.Item); } _isThemeInitialized = true; -#else - viewModel.ChangeAppTheme.Execute(null); +#if WINDOWS + viewModel.ChangeAppTheme.Execute(e.Item); #endif } }