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

Save theme switch in user settings #2

Merged
merged 2 commits into from
Mar 27, 2024
Merged
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
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 @@ -118,7 +118,7 @@
TokenTransferList.Data = all.OrderByDescending(x => x.Timestamp).ToList();

var allTokenIds = all.Select(x => x.TokenId).Distinct().ToList();
TryAddTokenIds(allTokenIds);

Check warning on line 121 in src/aoWebWallet/ViewModels/MainViewModel.cs

View workflow job for this annotation

GitHub Actions / build

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

Check warning on line 121 in src/aoWebWallet/ViewModels/MainViewModel.cs

View workflow job for this annotation

GitHub Actions / build

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

return TokenTransferList.Data;
});
Expand All @@ -135,7 +135,7 @@
TokenTransferList.Data = all.ToList();

var allTokenIds = all.Select(x => x.TokenId).Distinct().ToList();
TryAddTokenIds(allTokenIds);

Check warning on line 138 in src/aoWebWallet/ViewModels/MainViewModel.cs

View workflow job for this annotation

GitHub Actions / build

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

Check warning on line 138 in src/aoWebWallet/ViewModels/MainViewModel.cs

View workflow job for this annotation

GitHub Actions / build

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

return TokenTransferList.Data;
}
Expand Down Expand Up @@ -167,7 +167,7 @@
SelectedTransaction.Data = result;

if(result != null)
TryAddTokenIds(new List<string?>() { result.TokenId });

Check warning on line 170 in src/aoWebWallet/ViewModels/MainViewModel.cs

View workflow job for this annotation

GitHub Actions / build

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

Check warning on line 170 in src/aoWebWallet/ViewModels/MainViewModel.cs

View workflow job for this annotation

GitHub Actions / build

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

return result;
}
Expand All @@ -191,7 +191,7 @@
var balanceData = new DataLoaderViewModel<BalanceDataViewModel>();
balanceData.Data = new BalanceDataViewModel { Token = token };

balanceData.DataLoader.LoadAsync(async () =>

Check warning on line 194 in src/aoWebWallet/ViewModels/MainViewModel.cs

View workflow job for this annotation

GitHub Actions / build

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

Check warning on line 194 in src/aoWebWallet/ViewModels/MainViewModel.cs

View workflow job for this annotation

GitHub Actions / build

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.
{
var balanceData = await tokenClient.GetBalance(token.TokenId, address);
return new BalanceDataViewModel() { BalanceData = balanceData, Token = token };
Expand Down Expand Up @@ -316,7 +316,7 @@

partial void OnSelectedAddressChanged(string? value)
{
SelectWallet(value);

Check warning on line 319 in src/aoWebWallet/ViewModels/MainViewModel.cs

View workflow job for this annotation

GitHub Actions / build

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

Check warning on line 319 in src/aoWebWallet/ViewModels/MainViewModel.cs

View workflow job for this annotation

GitHub Actions / build

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

if (value != null)
this.AddToLog(ActivityLogType.ViewAddress, value);
Expand Down Expand Up @@ -395,12 +395,19 @@
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 @@

return new Transaction { Id = idResult };
});

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