Organize CoinListViewModel disposing.
MODIFY
WalletWasabi.Gui/Controls/WalletExplorer/CoinListViewModel.cs
WalletWasabi.Gui/Controls/WalletExplorer/CoinListViewModel.cs
@@ -21,17 +21,11 @@ namespace WalletWasabi.Gui.Controls.WalletExplorer
{
public class CoinListViewModel : ViewModelBase, IDisposable
{
- public ReadOnlyObservableCollection<CoinViewModel> Coins => _coinViewModels;
+ private CompositeDisposable Disposables { get; }
+
private readonly ReadOnlyObservableCollection<CoinViewModel> _coinViewModels;
- private SourceList<CoinViewModel> _rootlist = new SourceList<CoinViewModel>();
private SortExpressionComparer<CoinViewModel> _myComparer;
- private SortExpressionComparer<CoinViewModel> MyComparer
- {
- get => _myComparer;
- set => this.RaiseAndSetIfChanged(ref _myComparer, value);
- }
-
private CoinViewModel _selectedCoin;
private bool? _selectAllCheckBoxState;
private SortOrder _statusSortDirection;
@@ -41,8 +35,6 @@ namespace WalletWasabi.Gui.Controls.WalletExplorer
private bool? _selectNonPrivateCheckBoxState;
private GridLength _coinJoinStatusWidth;
private SortOrder _historySortDirection;
- private List<CoinViewModel> RemovedCoinViewModels { get; }
- private CompositeDisposable Disposables { get; }
public ReactiveCommand EnqueueCoin { get; }
public ReactiveCommand DequeueCoin { get; }
@@ -55,6 +47,16 @@ namespace WalletWasabi.Gui.Controls.WalletExplorer
public event EventHandler<CoinViewModel> SelectionChanged;
+ private List<CoinViewModel> RemovedCoinViewModels { get; }
+
+ public ReadOnlyObservableCollection<CoinViewModel> Coins => _coinViewModels;
+
+ private SortExpressionComparer<CoinViewModel> MyComparer
+ {
+ get => _myComparer;
+ set => this.RaiseAndSetIfChanged(ref _myComparer, value);
+ }
+
public CoinViewModel SelectedCoin
{
get => _selectedCoin;
@@ -147,6 +149,8 @@ namespace WalletWasabi.Gui.Controls.WalletExplorer
set => this.RaiseAndSetIfChanged(ref _coinJoinStatusWidth, value);
}
+ public SourceList<CoinViewModel> RootList { get; }
+
private bool? GetCheckBoxesSelectedState(Func<CoinViewModel, bool> coinFilterPredicate)
{
var coins = Coins.Where(coinFilterPredicate).ToArray();
@@ -180,14 +184,16 @@ namespace WalletWasabi.Gui.Controls.WalletExplorer
public CoinListViewModel()
{
- RemovedCoinViewModels = new List<CoinViewModel>();
Disposables = new CompositeDisposable();
+
+ RemovedCoinViewModels = new List<CoinViewModel>();
AmountSortDirection = SortOrder.Decreasing;
RefreshOrdering();
var sortChanged = this.WhenValueChanged(@this => MyComparer).Select(_ => MyComparer);
- _rootlist.Connect()
+ RootList = new SourceList<CoinViewModel>().DisposeWith(Disposables);
+ RootList.Connect()
.OnItemAdded(cvm => cvm.PropertyChanged += Coin_PropertyChanged)
.OnItemRemoved(cvm => RemovedCoinViewModels.Add(cvm)) //TODO: fix and test. If I directly unsubscribe from Coin_PropertyChanged then Unspent propchange not triggered in some cases => spent money stays in list
.Sort(MyComparer, comparerChanged: sortChanged, resetThreshold: 5)
@@ -197,7 +203,7 @@ namespace WalletWasabi.Gui.Controls.WalletExplorer
foreach (var sc in Global.WalletService.Coins.Where(sc => sc.Unspent))
{
- _rootlist.Add(new CoinViewModel(sc));
+ RootList.Add(new CoinViewModel(sc).DisposeWith(Disposables));
}
Global.WalletService.Coins.CollectionChanged += Coins_CollectionGlobalChanged;
@@ -327,23 +333,23 @@ namespace WalletWasabi.Gui.Controls.WalletExplorer
case NotifyCollectionChangedAction.Add:
foreach (SmartCoin c in e.NewItems.Cast<SmartCoin>().Where(sc => sc.Unspent))
{
- _rootlist.Add(new CoinViewModel(c));
+ RootList.Add(new CoinViewModel(c).DisposeWith(Disposables));
}
break;
case NotifyCollectionChangedAction.Remove:
foreach (var c in e.OldItems.Cast<SmartCoin>())
{
- CoinViewModel toRemove = _rootlist.Items.FirstOrDefault(cvm => cvm.Model == c);
+ CoinViewModel toRemove = RootList.Items.FirstOrDefault(cvm => cvm.Model == c);
if (toRemove != default)
{
- _rootlist.Remove(toRemove);
+ RootList.Remove(toRemove);
}
}
break;
case NotifyCollectionChangedAction.Reset:
- _rootlist.Clear();
+ RootList.Clear();
break;
}
});
@@ -393,7 +399,7 @@ namespace WalletWasabi.Gui.Controls.WalletExplorer
var cvm = (CoinViewModel)sender;
if (!cvm.Unspent)
{
- _rootlist.Remove(cvm);
+ RootList.Remove(cvm);
}
SetSelections();
@@ -417,16 +423,17 @@ namespace WalletWasabi.Gui.Controls.WalletExplorer
Global.WalletService.Coins.CollectionChanged -= Coins_CollectionGlobalChanged;
}
- if (_rootlist?.Items != null)
+ if (RootList?.Items != null)
{
- foreach (CoinViewModel cvm in _rootlist.Items)
+ foreach (CoinViewModel cvm in RootList.Items)
{
if (cvm != null)
{
cvm.PropertyChanged -= Coin_PropertyChanged;
- cvm.Dispose();
+ cvm?.Dispose();
}
}
+ RootList?.Dispose();
}
if (RemovedCoinViewModels != null)
@@ -436,16 +443,14 @@ namespace WalletWasabi.Gui.Controls.WalletExplorer
if (cvm != null)
{
cvm.PropertyChanged -= Coin_PropertyChanged;
- cvm.Dispose();
+ cvm?.Dispose();
}
}
}
- _rootlist?.Dispose();
Disposables?.Dispose();
}
- _rootlist = null;
_disposedValue = true;
}
}

Organize CoinListViewModel disposing.