From 0ba5ebd484c22a6abcacc3a468446c72f3553392 Mon Sep 17 00:00:00 2001 From: Michiel Post Date: Wed, 27 Mar 2024 14:30:23 +0100 Subject: [PATCH] Save theme switch --- src/aoWebWallet/Pages/MvvmComponentBase.cs | 6 +++++ src/aoWebWallet/Shared/NavMenu.razor | 27 ++++++++++++--------- src/aoWebWallet/ViewModels/MainViewModel.cs | 18 +++++++++++++- 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/src/aoWebWallet/Pages/MvvmComponentBase.cs b/src/aoWebWallet/Pages/MvvmComponentBase.cs index b02005d..0687f07 100644 --- a/src/aoWebWallet/Pages/MvvmComponentBase.cs +++ b/src/aoWebWallet/Pages/MvvmComponentBase.cs @@ -1,4 +1,5 @@ using aoWebWallet.ViewModels; +using CommunityToolkit.Mvvm.ComponentModel; using Microsoft.AspNetCore.Components; using System.ComponentModel; using System.Diagnostics; @@ -62,6 +63,11 @@ protected virtual Task LoadDataAsync() return Task.CompletedTask; } + protected void WatchObject(D obj) where D : ObservableObject + { + ObjWatch.Add(obj); + } + protected void WatchDataLoaderVM(DataLoaderViewModel vm) where D : class { ObjWatch.Add(vm); diff --git a/src/aoWebWallet/Shared/NavMenu.razor b/src/aoWebWallet/Shared/NavMenu.razor index 68037c8..bb520c1 100644 --- a/src/aoWebWallet/Shared/NavMenu.razor +++ b/src/aoWebWallet/Shared/NavMenu.razor @@ -21,7 +21,14 @@ About
- + @if(BindingContext.IsDarkMode) + { + + } + else + { + + } Theme
@@ -35,19 +42,15 @@ @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); } } diff --git a/src/aoWebWallet/ViewModels/MainViewModel.cs b/src/aoWebWallet/ViewModels/MainViewModel.cs index ffddfe1..546b9b6 100644 --- a/src/aoWebWallet/ViewModels/MainViewModel.cs +++ b/src/aoWebWallet/ViewModels/MainViewModel.cs @@ -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() @@ -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(); + } + } } }