generated from unoplatform/template
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Enabling light/dark switching (with persistence)
- Loading branch information
1 parent
7a252ff
commit 4c15c54
Showing
13 changed files
with
94 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
namespace Commerce; | ||
|
||
// TODO: Extract these to uno extensions | ||
// See https://github.com/unoplatform/uno.extensions/discussions/420 | ||
public class AppTheme : IAppTheme | ||
{ | ||
private readonly Window _window; | ||
private readonly IDispatcher _dispatcher; | ||
public AppTheme(Window window, IDispatcher dispatcher) | ||
{ | ||
_window = window; | ||
_dispatcher = dispatcher; | ||
} | ||
public bool IsDark => SystemThemeHelper.IsRootInDarkMode(_window.Content.XamlRoot); | ||
|
||
public async Task SetThemeAsync(bool darkMode) | ||
{ | ||
await _dispatcher.ExecuteAsync(() => | ||
{ | ||
SystemThemeHelper.SetRootTheme(_window.Content.XamlRoot, darkMode); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
{ | ||
{ | ||
"AppInfo": { | ||
"Title": "Uno Commerce App" | ||
}, | ||
"LocalizationConfiguration": { | ||
"Cultures": [ "en" ] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ | |
public record AppInfo | ||
{ | ||
public string? Title { get; init; } | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace Commerce; | ||
|
||
// TODO: Extract these to uno extensions | ||
// See https://github.com/unoplatform/uno.extensions/discussions/420 | ||
public interface IAppTheme | ||
{ | ||
bool IsDark { get; } | ||
|
||
Task SetThemeAsync(bool darkMode); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Commerce; | ||
|
||
public record CommerceApp | ||
{ | ||
public bool? IsDark { get; init; } | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,47 @@ | ||
namespace Commerce.ViewModels; | ||
|
||
public class ProfileViewModel | ||
public partial class ProfileViewModel | ||
{ | ||
private readonly INavigator _navigator; | ||
|
||
private readonly IWritableOptions<Credentials> _credentials; | ||
|
||
private readonly IProfileService _profileService; | ||
|
||
private readonly IAppTheme _appTheme; | ||
private readonly IWritableOptions<CommerceApp> _appSettings; | ||
|
||
public ProfileViewModel( | ||
INavigator navigator, | ||
IWritableOptions<Credentials> credentials, | ||
IProfileService profileService) | ||
IProfileService profileService, | ||
IAppTheme appTheme, | ||
IWritableOptions<CommerceApp> appSettings) | ||
{ | ||
_navigator = navigator; | ||
_credentials = credentials; | ||
_profileService = profileService; | ||
_appTheme = appTheme; | ||
_appSettings = appSettings; | ||
|
||
IsDarkTheme = State.Value(this, () => appTheme.IsDark); | ||
IsDarkTheme.Execute(ChangeAppTheme); | ||
} | ||
|
||
[Value] | ||
public IState<bool> IsDarkTheme { get; } | ||
|
||
public IFeed<Profile> Profile => Feed.Async(_profileService.GetProfile); | ||
|
||
public async void Logout() | ||
public async ValueTask Logout() | ||
{ | ||
await _credentials.UpdateAsync(c => new Credentials()); | ||
await _navigator.NavigateRouteAsync(this, "/"); | ||
} | ||
|
||
private async ValueTask ChangeAppTheme(bool isDark, CancellationToken ct) | ||
{ | ||
await _appTheme.SetThemeAsync(isDark); | ||
await _appSettings.UpdateAsync(s => s with { IsDark = isDark }); | ||
} | ||
} |