tiny renaming, refactoring and cleanup
MODIFY
WalletWasabi.Fluent/Behaviors/FadePocketLabelsBehavior.cs
WalletWasabi.Fluent/Behaviors/FadePocketLabelsBehavior.cs
-using System.Collections.Generic;
using System.Linq;
using System.Reactive.Concurrency;
using System.Reactive.Disposables;
@@ -7,9 +6,8 @@ using System.Threading.Tasks;
using Avalonia;
using Avalonia.VisualTree;
using ReactiveUI;
-using WalletWasabi.Blockchain.Analysis.Clustering;
using WalletWasabi.Fluent.Controls;
-using WalletWasabi.Fluent.ViewModels.Wallets.Send;
+using WalletWasabi.Fluent.Models;
namespace WalletWasabi.Fluent.Behaviors;
@@ -19,10 +17,10 @@ public class FadePocketLabelsBehavior : AttachedToVisualTreeBehavior<TagsBox>
private CompositeDisposable? _disposable;
private bool _isRunning = true;
- public static readonly StyledProperty<PocketViewModel[]> PocketsProperty =
- AvaloniaProperty.Register<FadePocketLabelsBehavior, PocketViewModel[]>(nameof(Pockets));
+ public static readonly StyledProperty<Pocket[]> PocketsProperty =
+ AvaloniaProperty.Register<FadePocketLabelsBehavior, Pocket[]>(nameof(Pockets));
- public PocketViewModel[] Pockets
+ public Pocket[] Pockets
{
get => GetValue(PocketsProperty);
set => SetValue(PocketsProperty, value);

ADD
WalletWasabi.Fluent/Models/Pocket.cs
+using NBitcoin;
+using WalletWasabi.Blockchain.Analysis.Clustering;
+using WalletWasabi.Blockchain.TransactionOutputs;
+
+namespace WalletWasabi.Fluent.Models;
+
+public class Pocket
+{
+ public Pocket((SmartLabel labels, ICoinsView coins) pocket)
+ {
+ Coins = pocket.coins;
+ Labels = pocket.labels;
+ }
+
+ public SmartLabel Labels { get; }
+
+ public Money Amount => Coins.TotalAmount();
+
+ public ICoinsView Coins { get; }
+}

DELETE
WalletWasabi.Fluent/ViewModels/Wallets/Send/PocketViewModel.cs
-using NBitcoin;
-using WalletWasabi.Blockchain.Analysis.Clustering;
-using WalletWasabi.Blockchain.TransactionOutputs;
-
-namespace WalletWasabi.Fluent.ViewModels.Wallets.Send;
-
-public partial class PocketViewModel : ViewModelBase
-{
- [AutoNotify] private bool _isSelected;
- [AutoNotify] private decimal _totalBtc;
- [AutoNotify] private SmartLabel _labels;
-
- public PocketViewModel((SmartLabel labels, ICoinsView coins) pocket)
- {
- Coins = pocket.coins;
- _labels = pocket.labels;
- _totalBtc = pocket.coins.TotalAmount().ToDecimal(MoneyUnit.BTC);
- }
-
- public ICoinsView Coins { get; }
-}

MODIFY
WalletWasabi.Fluent/ViewModels/Wallets/Send/PrivacyControlViewModel.cs
WalletWasabi.Fluent/ViewModels/Wallets/Send/PrivacyControlViewModel.cs
@@ -9,6 +9,7 @@ using ReactiveUI;
using WalletWasabi.Blockchain.Analysis.Clustering;
using WalletWasabi.Blockchain.TransactionOutputs;
using WalletWasabi.Fluent.Helpers;
+using WalletWasabi.Fluent.Models;
using WalletWasabi.Fluent.ViewModels.Dialogs.Base;
using WalletWasabi.Wallets;
@@ -20,9 +21,9 @@ public partial class PrivacyControlViewModel : DialogViewModelBase<IEnumerable<S
private readonly Wallet _wallet;
private readonly TransactionInfo _transactionInfo;
private readonly bool _isSilent;
- private readonly Stack<PocketViewModel[]> _removedPocketStack;
+ private readonly Stack<Pocket[]> _removedPocketStack;
- [AutoNotify] private PocketViewModel[] _usedPockets = Array.Empty<PocketViewModel>();
+ [AutoNotify] private Pocket[] _usedPockets = Array.Empty<Pocket>();
private bool _isUpdating;
@@ -31,7 +32,7 @@ public partial class PrivacyControlViewModel : DialogViewModelBase<IEnumerable<S
_wallet = wallet;
_transactionInfo = transactionInfo;
_isSilent = isSilent;
- _removedPocketStack = new Stack<PocketViewModel[]>();
+ _removedPocketStack = new Stack<Pocket[]>();
Labels = new ObservableCollection<string>();
MustHaveLabels = new ObservableCollection<string>();
@@ -39,16 +40,36 @@ public partial class PrivacyControlViewModel : DialogViewModelBase<IEnumerable<S
SetupCancel(enableCancel: false, enableCancelOnEscape: true, enableCancelOnPressed: false);
EnableBack = true;
- NextCommand = ReactiveCommand.Create(() => Complete(UsedPockets));
-
- EnableAutoBusyOn(NextCommand);
-
var undoCommandCanExecute = this.WhenAnyValue(x => x.Labels.Count).Select(_ => _removedPocketStack.Count > 0);
UndoCommand = ReactiveCommand.Create(OnUndo, undoCommandCanExecute);
+ NextCommand = ReactiveCommand.Create(() => Complete(UsedPockets));
+
Observable
.FromEventPattern(Labels, nameof(Labels.CollectionChanged))
- .Subscribe(_ =>
+ .Subscribe(_ => OnLabelsChanged());
+ }
+
+ public ICommand UndoCommand { get; }
+
+ public ObservableCollection<string> Labels { get; set; }
+
+ public ObservableCollection<string> MustHaveLabels { get; }
+
+ private void Complete(IEnumerable<Pocket> pockets)
+ {
+ Close(DialogResultKind.Normal, pockets.SelectMany(x => x.Coins));
+ }
+
+ private void OnUndo()
+ {
+ var pocketsToUndo = _removedPocketStack.Pop();
+ var newPockets = UsedPockets.Union(pocketsToUndo);
+
+ UpdateLabels(newPockets);
+ }
+
+ private void OnLabelsChanged()
{
if (_isUpdating)
{
@@ -61,72 +82,42 @@ public partial class PrivacyControlViewModel : DialogViewModelBase<IEnumerable<S
Dispatcher.UIThread.Post(() =>
{
- _isUpdating = true;
-
_removedPocketStack.Push(pocketsToRemove);
- UsedPockets = UsedPockets.Except(pocketsToRemove).ToArray();
- Labels.Clear();
- UpdateLabels();
+ var newPockets = UsedPockets.Except(pocketsToRemove);
- _isUpdating = false;
- });
+ UpdateLabels(newPockets);
});
}
- private void OnUndo()
+ private void UpdateLabels(IEnumerable<Pocket> pockets)
{
_isUpdating = true;
- var pocketsToUndo = _removedPocketStack.Pop();
- UsedPockets = UsedPockets.Union(pocketsToUndo).ToArray();
-
+ UsedPockets = pockets.ToArray();
MustHaveLabels.Clear();
Labels.Clear();
- UpdateLabels();
-
- _isUpdating = false;
- }
-
- public ICommand UndoCommand { get; }
-
- public ObservableCollection<string> Labels { get; set; }
-
- public ObservableCollection<string> MustHaveLabels { get; }
-
- private void Complete(IEnumerable<PocketViewModel> pockets)
- {
- Close(DialogResultKind.Normal, pockets.SelectMany(x => x.Coins));
- }
-
- private void UpdateLabels()
- {
foreach (var label in SmartLabel.Merge(UsedPockets.Select(x => x.Labels)))
{
- var coinsWithSameLabel = UsedPockets.Where(x => x.Labels.Contains(label));
+ var pocketsWithSameLabel = UsedPockets.Where(x => x.Labels.Contains(label));
- if (UsedPockets.Sum(x => x.Coins.TotalAmount()) - coinsWithSameLabel.Sum(x => x.Coins.TotalAmount()) >= _transactionInfo.Amount)
+ if (UsedPockets.Sum(x => x.Amount) - pocketsWithSameLabel.Sum(x => x.Amount) >= _transactionInfo.Amount)
{
Labels.Add(label);
}
- else if (!MustHaveLabels.Contains(label))
+ else
{
MustHaveLabels.Add(label);
}
}
+
+ _isUpdating = false;
}
private void InitializeLabels()
{
- _isUpdating = true;
-
- UsedPockets = _wallet.Coins.GetPockets(_wallet.ServiceConfiguration.MinAnonScoreTarget).Select(x => new PocketViewModel(x)).ToArray();
- MustHaveLabels.Clear();
- Labels.Clear();
-
- UpdateLabels();
-
- _isUpdating = false;
+ var pockets = _wallet.Coins.GetPockets(_wallet.ServiceConfiguration.MinAnonScoreTarget).Select(x => new Pocket(x));
+ UpdateLabels(pockets);
}
protected override void OnNavigatedTo(bool isInHistory, CompositeDisposable disposables)

tiny renaming, refactoring and cleanup