Skip to content

Commit

Permalink
Merge pull request #2 from michielpost/feature/theme-switch
Browse files Browse the repository at this point in the history
Save theme switch in user settings
  • Loading branch information
michielpost authored Mar 27, 2024
2 parents e52b059 + 1470037 commit 88c2a64
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 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
38 changes: 28 additions & 10 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.UserSettings?.IsDarkMode ?? true)
{
<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,30 @@
</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.PropertyChanged += BindingContext_PropertyChanged;

base.OnInitialized();
}

private void BindingContext_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)

Check warning on line 54 in src/aoWebWallet/Shared/NavMenu.razor

View workflow job for this annotation

GitHub Actions / build

'NavMenu.BindingContext_PropertyChanged(object?, PropertyChangedEventArgs)' hides inherited member 'MvvmComponentBase<MainViewModel>.BindingContext_PropertyChanged(object?, PropertyChangedEventArgs)'. Use the new keyword if hiding was intended.

Check warning on line 54 in src/aoWebWallet/Shared/NavMenu.razor

View workflow job for this annotation

GitHub Actions / build

'NavMenu.BindingContext_PropertyChanged(object?, PropertyChangedEventArgs)' hides inherited member 'MvvmComponentBase<MainViewModel>.BindingContext_PropertyChanged(object?, PropertyChangedEventArgs)'. Use the new keyword if hiding was intended.
{
if (e.PropertyName == nameof(MainViewModel.IsDarkMode))
{
BindingContext.IsDarkMode = true;
this.StateHasChanged();
}
}

public virtual void Dispose()

Check warning on line 62 in src/aoWebWallet/Shared/NavMenu.razor

View workflow job for this annotation

GitHub Actions / build

'NavMenu.Dispose()' hides inherited member 'MvvmComponentBase<MainViewModel>.Dispose()'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.

Check warning on line 62 in src/aoWebWallet/Shared/NavMenu.razor

View workflow job for this annotation

GitHub Actions / build

'NavMenu.Dispose()' hides inherited member 'MvvmComponentBase<MainViewModel>.Dispose()'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.
{
BindingContext.PropertyChanged -= BindingContext_PropertyChanged;
}

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 88c2a64

Please sign in to comment.