Skip to content

Commit

Permalink
Save theme switch
Browse files Browse the repository at this point in the history
  • Loading branch information
michielpost committed Mar 27, 2024
1 parent e52b059 commit 0ba5ebd
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
6 changes: 6 additions & 0 deletions src/aoWebWallet/Pages/MvvmComponentBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using aoWebWallet.ViewModels;
using CommunityToolkit.Mvvm.ComponentModel;
using Microsoft.AspNetCore.Components;
using System.ComponentModel;
using System.Diagnostics;
Expand Down Expand Up @@ -62,6 +63,11 @@ protected virtual Task LoadDataAsync()
return Task.CompletedTask;
}

protected void WatchObject<D>(D obj) where D : ObservableObject
{
ObjWatch.Add(obj);
}

protected void WatchDataLoaderVM<D>(DataLoaderViewModel<D> vm) where D : class
{
ObjWatch.Add(vm);
Expand Down
27 changes: 15 additions & 12 deletions src/aoWebWallet/Shared/NavMenu.razor
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@
<MudNavLink style="margin-bottom:5px;" Href="about" Match="NavLinkMatch.Prefix" Icon="@Icons.Material.Filled.Info">About</MudNavLink>
</div>
<div style="display: flex; flex-direction: row; justify-content: center; align-items: center; margin-left:10px; padding:5px;">
<MudIcon Icon="@icons[index]" Color="Color.Primary" />
@if(BindingContext.IsDarkMode)
{
<MudIcon Icon="@icons[1]" Color="Color.Primary" />
}
else
{
<MudIcon Icon="@icons[0]" Color="Color.Primary" />
}
<MudButton Variant="Variant.Filled" OnClick="ToggleTheme" DisableElevation="true" Size="Size.Small">Theme</MudButton>
</div>

Expand All @@ -35,19 +42,15 @@
</MudNavMenu>

@code{
private int index = 1;
private string[] icons = { Icons.Material.Filled.WbSunny, Icons.Material.Filled.NightlightRound };

private void ToggleTheme()
protected override void OnInitialized()
{
index = (index + 1) % 2;
if (index == 0)
{
BindingContext.IsDarkMode = false;
}
else
{
BindingContext.IsDarkMode = true;
}
base.OnInitialized();
}

private Task ToggleTheme()
{
return BindingContext.SetIsDarkMode(!BindingContext.IsDarkMode);
}
}
18 changes: 17 additions & 1 deletion src/aoWebWallet/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -395,12 +395,19 @@ partial void OnComputeUnitUrlChanged(string? value)
public async Task LoadUserSettings()
{
UserSettings = await storageService.GetUserSettings();
if (UserSettings != null)
{
IsDarkMode = UserSettings.IsDarkMode ?? true;
}
}

public async Task SaveUserSettings()
{
if(UserSettings != null)
if (UserSettings != null)
{
await storageService.SaveUserSettings(UserSettings);
IsDarkMode = UserSettings.IsDarkMode ?? true;
}
}

public async Task AddWalletAsReadonly()
Expand Down Expand Up @@ -619,5 +626,14 @@ public async Task CopyToClipboard(string? text)
return new Transaction { Id = idResult };
});

public async Task SetIsDarkMode(bool isDarkMode)
{
if(UserSettings != null)
{
UserSettings.IsDarkMode = isDarkMode;
await SaveUserSettings();
}
}
}
}

0 comments on commit 0ba5ebd

Please sign in to comment.