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 2 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
24 changes: 13 additions & 11 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,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; }

Expand All @@ -36,12 +36,11 @@ public SettingsViewModel(
_userSvc = userSvc;
LocalizationSettings = localizationSettings;
_themeService = themeService;
_dispatcher = dispatcher;

AppThemes = new string[] { localizer["SettingsFlyout_ThemeLight"], localizer["SettingsFlyout_ThemeDark"] };

_ = dispatcher.TryEnqueue(() => {
_isDark = _themeService.IsDark;
});

_ = dispatcher.TryEnqueue(() => { _isDark = _themeService.IsDark; });
Kunal22shah marked this conversation as resolved.
Show resolved Hide resolved

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 +55,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 _ => await Task.FromResult(_themeService.IsDark), ct);
Kunal22shah marked this conversation as resolved.
Show resolved Hide resolved
return AppThemes[isDark ? 1 : 0];
});

public async ValueTask SignOut(CancellationToken ct)
{
Expand All @@ -70,11 +73,10 @@ 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;
var newTheme = selectedTheme == AppThemes[1] ? AppTheme.Dark : AppTheme.Light;
await _themeService.SetThemeAsync(newTheme);
WeakReferenceMessenger.Default.Send(new ThemeChangedMessage(newTheme));
}
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
4 changes: 2 additions & 2 deletions reference/ToDo/src/ToDo/Views/Dialogs/SettingsFlyout.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down