Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ToDo] fix: Wrong value for default theme & Opening Settings Flyout Changes App #783

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions reference/ToDo/src/ToDo/Presentation/SettingsViewModel.cs
Kunal22shah marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand All @@ -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());

Expand All @@ -56,9 +52,13 @@ public SettingsViewModel(

[Value]
public IState<DisplayCulture> SelectedCulture { get; }

[Value]
public IState<string> SelectedAppTheme => State.Value(this, () => AppThemes[_isDark ? 1 : 0]);
public IState<string> 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)
{
Expand All @@ -70,13 +70,16 @@ public async ValueTask SignOut(CancellationToken ct)
await _sourceNavigator.NavigateViewModelAsync<HomeViewModel>(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)
Expand Down
2 changes: 1 addition & 1 deletion reference/ToDo/src/ToDo/Views/Dialogs/SettingsFlyout.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
<utu:ChipGroup x:Name="ThemeChipGroup"
SelectionMode="Single"
ItemsSource="{Binding AppThemes}"
SelectedItem="{Binding SelectedAppTheme, Mode=TwoWay}"
SelectedItem="{Binding SelectedAppTheme}"
ItemChecked="ThemeChipGroup_ItemChecked"
Style="{StaticResource FilterChipGroupStyle}">
<utu:ChipGroup.ItemTemplate>
Expand Down
9 changes: 3 additions & 6 deletions reference/ToDo/src/ToDo/Views/Dialogs/SettingsFlyout.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ internal record ThemeChangedMessage(AppTheme Theme)

public sealed partial class SettingsFlyout : Flyout, IRecipient<ThemeChangedMessage>
{
#if ANDROID
private bool _isThemeInitialized = false;
#endif

public SettingsFlyout()
{
Expand All @@ -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
}
}
Expand Down