From 3a6ea3a745ee84cc392a696bc3a2829dcd0ac773 Mon Sep 17 00:00:00 2001 From: Kunal22shah Date: Fri, 19 Jul 2024 14:09:07 -0400 Subject: [PATCH 1/3] fix: wrong value for default theme --- .../ToDo/src/ToDo/Presentation/SettingsViewModel.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/reference/ToDo/src/ToDo/Presentation/SettingsViewModel.cs b/reference/ToDo/src/ToDo/Presentation/SettingsViewModel.cs index 8e33a46d2..e3e618844 100644 --- a/reference/ToDo/src/ToDo/Presentation/SettingsViewModel.cs +++ b/reference/ToDo/src/ToDo/Presentation/SettingsViewModel.cs @@ -38,10 +38,12 @@ public SettingsViewModel( _themeService = themeService; AppThemes = new string[] { localizer["SettingsFlyout_ThemeLight"], localizer["SettingsFlyout_ThemeDark"] }; - - _ = dispatcher.TryEnqueue(() => { - _isDark = _themeService.IsDark; - }); + +#if WINDOWS + _ = dispatcher.TryEnqueue(() => { _isDark = _themeService.IsDark; }); +#else + _isDark = _themeService.IsDark; +#endif 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()); From 43e9e26520635db66a93cb674b4c47001914d8b2 Mon Sep 17 00:00:00 2001 From: Kunal22shah Date: Mon, 22 Jul 2024 14:16:16 -0400 Subject: [PATCH 2/3] fix: threading issue --- .../ToDo/Presentation/SettingsViewModel.cs | 22 +++++++++---------- .../ToDo/Views/Dialogs/SettingsFlyout.xaml | 2 +- .../ToDo/Views/Dialogs/SettingsFlyout.xaml.cs | 4 ++-- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/reference/ToDo/src/ToDo/Presentation/SettingsViewModel.cs b/reference/ToDo/src/ToDo/Presentation/SettingsViewModel.cs index e3e618844..41e9dd11b 100644 --- a/reference/ToDo/src/ToDo/Presentation/SettingsViewModel.cs +++ b/reference/ToDo/src/ToDo/Presentation/SettingsViewModel.cs @@ -10,8 +10,8 @@ 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,14 +36,11 @@ public SettingsViewModel( _userSvc = userSvc; LocalizationSettings = localizationSettings; _themeService = themeService; + _dispatcher = dispatcher; AppThemes = new string[] { localizer["SettingsFlyout_ThemeLight"], localizer["SettingsFlyout_ThemeDark"] }; -#if WINDOWS _ = dispatcher.TryEnqueue(() => { _isDark = _themeService.IsDark; }); -#else - _isDark = _themeService.IsDark; -#endif 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()); @@ -58,9 +55,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 _ => await Task.FromResult(_themeService.IsDark), ct); + return AppThemes[isDark ? 1 : 0]; + }); public async ValueTask SignOut(CancellationToken ct) { @@ -72,11 +73,10 @@ 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; + var newTheme = selectedTheme == AppThemes[1] ? AppTheme.Dark : AppTheme.Light; await _themeService.SetThemeAsync(newTheme); WeakReferenceMessenger.Default.Send(new ThemeChangedMessage(newTheme)); } 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..0b765ebde 100644 --- a/reference/ToDo/src/ToDo/Views/Dialogs/SettingsFlyout.xaml.cs +++ b/reference/ToDo/src/ToDo/Views/Dialogs/SettingsFlyout.xaml.cs @@ -23,11 +23,11 @@ private void ThemeChipGroup_ItemChecked(object sender, ChipItemEventArgs e) #if ANDROID if (_isThemeInitialized) { - viewModel.ChangeAppTheme.Execute(null); + viewModel.ChangeAppTheme.Execute(e.Item); } _isThemeInitialized = true; #else - viewModel.ChangeAppTheme.Execute(null); + viewModel.ChangeAppTheme.Execute(e.Item); #endif } } From fb60dd01d001b54c4adde86a28b8e615d0b4248a Mon Sep 17 00:00:00 2001 From: Kunal22shah Date: Tue, 23 Jul 2024 16:27:17 -0400 Subject: [PATCH 3/3] fix: wasm flickering issue --- .../ToDo/src/ToDo/Presentation/SettingsViewModel.cs | 13 +++++++------ .../src/ToDo/Views/Dialogs/SettingsFlyout.xaml.cs | 5 +---- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/reference/ToDo/src/ToDo/Presentation/SettingsViewModel.cs b/reference/ToDo/src/ToDo/Presentation/SettingsViewModel.cs index 41e9dd11b..5e7a0e667 100644 --- a/reference/ToDo/src/ToDo/Presentation/SettingsViewModel.cs +++ b/reference/ToDo/src/ToDo/Presentation/SettingsViewModel.cs @@ -10,7 +10,6 @@ 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; } @@ -39,8 +38,6 @@ public SettingsViewModel( _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()); @@ -59,7 +56,7 @@ public SettingsViewModel( [Value] public IState SelectedAppTheme => State.Async(this, async ct => { - var isDark = await _dispatcher.ExecuteAsync(async _ => await Task.FromResult(_themeService.IsDark), ct); + var isDark = await _dispatcher.ExecuteAsync(async _ => _themeService.IsDark, ct); return AppThemes[isDark ? 1 : 0]; }); @@ -77,8 +74,12 @@ public async ValueTask SignOut(CancellationToken ct) public async ValueTask ChangeAppTheme(string selectedTheme, CancellationToken ct) { var newTheme = selectedTheme == AppThemes[1] ? AppTheme.Dark : AppTheme.Light; - await _themeService.SetThemeAsync(newTheme); - WeakReferenceMessenger.Default.Send(new ThemeChangedMessage(newTheme)); + 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.cs b/reference/ToDo/src/ToDo/Views/Dialogs/SettingsFlyout.xaml.cs index 0b765ebde..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,13 +18,12 @@ private void ThemeChipGroup_ItemChecked(object sender, ChipItemEventArgs e) { if (FlyoutRoot.DataContext is BindableSettingsViewModel viewModel) { -#if ANDROID if (_isThemeInitialized) { viewModel.ChangeAppTheme.Execute(e.Item); } _isThemeInitialized = true; -#else +#if WINDOWS viewModel.ChangeAppTheme.Execute(e.Item); #endif }