Refactor WalletViewModel
MODIFY
WalletWasabi.Gui/Controls/WalletExplorer/WalletExplorerViewModel.cs
WalletWasabi.Gui/Controls/WalletExplorer/WalletExplorerViewModel.cs
@@ -50,12 +50,9 @@ namespace WalletWasabi.Gui.Controls.WalletExplorer
return;
}
- WalletViewModel walletViewModel = new WalletViewModel(wallet, receiveDominant);
+ WalletViewModel walletViewModel = new WalletViewModel(wallet);
_wallets.Add(walletViewModel);
- walletViewModel.OnWalletOpened();
-
- // TODO if we ever implement closing a wallet OnWalletClosed needs to be called
- // to prevent memory leaks.
+ walletViewModel.OpenWallet(receiveDominant);
}
public void BeforeActivation()

MODIFY
WalletWasabi.Gui/Controls/WalletExplorer/WalletViewModel.cs
WalletWasabi.Gui/Controls/WalletExplorer/WalletViewModel.cs
@@ -11,19 +11,58 @@ using WalletWasabi.Gui.ViewModels;
using WalletWasabi.Logging;
using Splat;
using WalletWasabi.Wallets;
+using WalletWasabi.Helpers;
+using WalletWasabi.Blockchain.Keys;
namespace WalletWasabi.Gui.Controls.WalletExplorer
{
public class WalletViewModel : ViewModelBase
{
- private CompositeDisposable Disposables { get; set; }
-
private ObservableCollection<WasabiDocumentTabViewModel> _actions;
-
private bool _isExpanded;
-
private string _title;
+ public WalletViewModel(Wallet wallet)
+ {
+ Wallet = Guard.NotNull(nameof(wallet), wallet);
+
+ Disposables = Disposables is null ? new CompositeDisposable() : throw new NotSupportedException($"Cannot open {GetType().Name} before closing it.");
+ Actions = new ObservableCollection<WasabiDocumentTabViewModel>();
+ Global = Locator.Current.GetService<Global>();
+ Title = Name;
+
+ LurkingWifeModeCommand = ReactiveCommand.Create(() =>
+ {
+ Global.UiConfig.LurkingWifeMode = !Global.UiConfig.LurkingWifeMode;
+ Global.UiConfig.ToFile();
+ });
+
+ LurkingWifeModeCommand.ThrownExceptions
+ .ObserveOn(RxApp.TaskpoolScheduler)
+ .Subscribe(ex => Logger.LogError(ex));
+
+ Observable.Merge(
+ Observable.FromEventPattern(Wallet.TransactionProcessor, nameof(Wallet.TransactionProcessor.WalletRelevantTransactionProcessed)).Select(_ => Unit.Default))
+ .Throttle(TimeSpan.FromSeconds(0.1))
+ .Merge(Global.UiConfig.WhenAnyValue(x => x.LurkingWifeMode).Select(_ => Unit.Default))
+ .ObserveOn(RxApp.MainThreadScheduler)
+ .Subscribe(_ =>
+ {
+ try
+ {
+ Money balance = Wallet.Coins.TotalAmount();
+ Title = $"{Name} ({(Global.UiConfig.LurkingWifeMode ? "#########" : balance.ToString(false, true))} BTC)";
+ }
+ catch (Exception ex)
+ {
+ Logger.LogError(ex);
+ }
+ })
+ .DisposeWith(Disposables);
+ }
+
+ public Global Global { get; }
+
public bool IsExpanded
{
get => _isExpanded;
@@ -36,27 +75,30 @@ namespace WalletWasabi.Gui.Controls.WalletExplorer
set => this.RaiseAndSetIfChanged(ref _title, value);
}
- public WalletViewModel(Wallet wallet, bool receiveDominant)
- {
- var global = Locator.Current.GetService<Global>();
- Wallet = wallet;
-
- Title = Path.GetFileNameWithoutExtension(Wallet.KeyManager.FilePath);
+ public string Name => Wallet.Name;
+ public KeyManager KeyManager => Wallet.KeyManager;
+ public Wallet Wallet { get; }
+ public ReactiveCommand<Unit, Unit> LurkingWifeModeCommand { get; }
- var keyManager = Wallet.KeyManager;
- Name = Path.GetFileNameWithoutExtension(keyManager.FilePath);
+ public ObservableCollection<WasabiDocumentTabViewModel> Actions
+ {
+ get => _actions;
+ set => this.RaiseAndSetIfChanged(ref _actions, value);
+ }
- Actions = new ObservableCollection<WasabiDocumentTabViewModel>();
+ private CompositeDisposable Disposables { get; set; }
+ public void OpenWallet(bool receiveDominant)
+ {
SendTabViewModel sendTab = null;
// If hardware wallet then we need the Send tab.
- if (Wallet?.KeyManager?.IsHardwareWallet is true)
+ if (KeyManager.IsHardwareWallet is true)
{
sendTab = new SendTabViewModel(Wallet);
Actions.Add(sendTab);
}
// If not hardware wallet, but neither watch only then we also need the send tab.
- else if (Wallet?.KeyManager?.IsWatchOnly is false)
+ else if (KeyManager.IsWatchOnly is false)
{
sendTab = new SendTabViewModel(Wallet);
Actions.Add(sendTab);
@@ -91,7 +133,7 @@ namespace WalletWasabi.Gui.Controls.WalletExplorer
}
else
{
- WasabiDocumentTabViewModel tabToOpen = global.UiConfig.LastActiveTab switch
+ WasabiDocumentTabViewModel tabToOpen = Global.UiConfig.LastActiveTab switch
{
nameof(SendTabViewModel) => sendTab,
nameof(ReceiveTabViewModel) => receiveTab,
@@ -103,60 +145,7 @@ namespace WalletWasabi.Gui.Controls.WalletExplorer
tabToOpen?.DisplayActionTab();
}
- LurkingWifeModeCommand = ReactiveCommand.Create(() =>
- {
- global.UiConfig.LurkingWifeMode = !global.UiConfig.LurkingWifeMode;
- global.UiConfig.ToFile();
- });
-
- LurkingWifeModeCommand.ThrownExceptions
- .ObserveOn(RxApp.TaskpoolScheduler)
- .Subscribe(ex => Logger.LogError(ex));
- }
-
- public void OnWalletOpened()
- {
- Disposables = Disposables is null ? new CompositeDisposable() : throw new NotSupportedException($"Cannot open {GetType().Name} before closing it.");
-
- var global = Locator.Current.GetService<Global>();
-
- Observable.Merge(
- Observable.FromEventPattern(Wallet.TransactionProcessor, nameof(Wallet.TransactionProcessor.WalletRelevantTransactionProcessed)).Select(_ => Unit.Default))
- .Throttle(TimeSpan.FromSeconds(0.1))
- .Merge(global.UiConfig.WhenAnyValue(x => x.LurkingWifeMode).Select(_ => Unit.Default))
- .ObserveOn(RxApp.MainThreadScheduler)
- .Subscribe(_ =>
- {
- try
- {
- Money balance = Wallet.Coins.TotalAmount();
- Title = $"{Name} ({(global.UiConfig.LurkingWifeMode ? "#########" : balance.ToString(false, true))} BTC)";
- }
- catch (Exception ex)
- {
- Logger.LogError(ex);
- }
- })
- .DisposeWith(Disposables);
-
IsExpanded = true;
}
-
- public void OnWalletClosed()
- {
- Disposables?.Dispose();
- }
-
- public string Name { get; }
-
- public Wallet Wallet { get; }
-
- public ReactiveCommand<Unit, Unit> LurkingWifeModeCommand { get; }
-
- public ObservableCollection<WasabiDocumentTabViewModel> Actions
- {
- get => _actions;
- set => this.RaiseAndSetIfChanged(ref _actions, value);
- }
}
}

Refactor WalletViewModel