Skip to content

Commit

Permalink
Review EditDiff usage
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperJMN committed Jun 12, 2024
1 parent bf2f338 commit 86c4b73
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 9 deletions.
4 changes: 2 additions & 2 deletions WalletWasabi.Fluent/Extensions/ObservableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ public static IObservable<T> ReplayLastActive<T>(this IObservable<T> observable)
public static IObservable<(T1, T2, T3)> Flatten<T1, T2, T3>(this IObservable<((T1, T2), T3)> source) =>
source.Select(t => (t.Item1.Item1, t.Item1.Item2, t.Item2));

public static IObservableCache<TObject, TKey> Fetch<TObject, TKey>(this IObservable<Unit> signal, Func<IEnumerable<TObject>> source, Func<TObject, TKey> keySelector)
public static IObservableCache<TObject, TKey> Fetch<TObject, TKey>(this IObservable<Unit> signal, Func<IEnumerable<TObject>> source, Func<TObject, TKey> keySelector, IEqualityComparer<TObject> equalityComparer)
where TKey : notnull where TObject : notnull
{
return signal.Select(_ => source())
.EditDiff(keySelector)
.EditDiff(keySelector, equalityComparer)
.DisposeMany()
.AsObservableCache();
}
Expand Down
31 changes: 31 additions & 0 deletions WalletWasabi.Fluent/Helpers/LambdaComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace WalletWasabi.Fluent.Helpers;

using System;
using System.Collections.Generic;

public class LambdaComparer<T> : IEqualityComparer<T>
{
private readonly Func<T?, T?, bool> _lambdaComparer;
private readonly Func<T?, int> _lambdaHash;

public LambdaComparer(Func<T?, T?, bool> lambdaComparer) :
this(lambdaComparer, _ => 0)
{
}

public LambdaComparer(Func<T?, T?, bool> lambdaComparer, Func<T?, int> lambdaHash)
{
_lambdaComparer = lambdaComparer ?? throw new ArgumentNullException("lambdaComparer");
_lambdaHash = lambdaHash ?? throw new ArgumentNullException("lambdaHash");
}

public bool Equals(T? x, T? y)
{
return _lambdaComparer(x, y);
}

public int GetHashCode(T obj)
{
return _lambdaHash(obj);
}
}
4 changes: 2 additions & 2 deletions WalletWasabi.Fluent/Models/Wallets/WalletCoinsModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public WalletCoinsModel(Wallet wallet, IWalletModel walletModel)
.Merge(isCoinjoinRunningChanged)
.Publish();

Pockets = signals.Fetch(GetPockets, x => x.Labels).DisposeWith(_disposables);
Pockets = signals.Fetch(GetPockets, x => x.Labels, new LambdaComparer<Pocket>((a, b) => Equals(a?.Labels, b?.Labels))).DisposeWith(_disposables);
List = Pockets.Connect().MergeMany(x => x.Coins.Select(GetCoinModel).AsObservableChangeSet()).AddKey(x => x.Key).AsObservableCache();

signals
.Do(_ => Logger.LogDebug($"Refresh signal emitted in {walletModel.Name}"))
.Subscribe()
Expand Down
3 changes: 2 additions & 1 deletion WalletWasabi.Fluent/Models/Wallets/WalletRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Threading.Tasks;
using WalletWasabi.Blockchain.Keys;
using WalletWasabi.Fluent.Extensions;
using WalletWasabi.Fluent.Helpers;
using WalletWasabi.Helpers;
using WalletWasabi.Hwi.Models;
using WalletWasabi.Models;
Expand All @@ -34,7 +35,7 @@ public WalletRepository(IAmountProvider amountProvider)
.StartWith(System.Reactive.Unit.Default);

Wallets =
signals.Fetch(() => Services.WalletManager.GetWallets(), x => x.WalletId)
signals.Fetch(() => Services.WalletManager.GetWallets(), x => x.WalletId, new LambdaComparer<Wallet>((a, b) => Equals(a?.WalletId, b?.WalletId)))
.DisposeWith(_disposable)
.Connect()
.TransformWithInlineUpdate(CreateWalletModel, (_, _) => { })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public WalletTransactionsModel(IWalletModel walletModel, Wallet wallet)
Cache =
TransactionProcessed
.Merge(RequestedUnconfirmedTxChainArrived)
.Fetch(BuildSummary, model => model.Id)
.Fetch(BuildSummary, model => model.Id, new LambdaComparer<TransactionModel>((a, b) => Equals(a?.Id, b?.Id)))
.DisposeWith(_disposable);

IsEmpty = Cache.Empty();
Expand Down
2 changes: 2 additions & 0 deletions WalletWasabi.Fluent/ViewModels/Wallets/Coins/CoinListItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,6 @@ public bool? IsSelected
public virtual bool HasChildren() => Children.Count != 0;

public void Dispose() => _disposables.Dispose();

public abstract string Key { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using ReactiveUI;
using WalletWasabi.Blockchain.Analysis.Clustering;
using WalletWasabi.Fluent.Controls.Sorting;
using WalletWasabi.Fluent.Helpers;
using WalletWasabi.Fluent.Models;
using WalletWasabi.Fluent.Models.Wallets;
using WalletWasabi.Fluent.ViewModels.CoinControl.Core;
Expand Down Expand Up @@ -112,7 +113,7 @@ public CoinListViewModel(IWalletModel wallet, IList<ICoinModel> initialCoinSelec
new SortableItem("Amount") { SortByAscendingCommand = ReactiveCommand.Create(() => TreeDataGridSource.SortBy(TreeDataGridSource.Columns[2], ListSortDirection.Ascending)), SortByDescendingCommand = ReactiveCommand.Create(() => TreeDataGridSource.SortBy(TreeDataGridSource.Columns[2], ListSortDirection.Descending)) },
new SortableItem("Label") { SortByAscendingCommand = ReactiveCommand.Create(() => TreeDataGridSource.SortBy(TreeDataGridSource.Columns[3], ListSortDirection.Ascending)), SortByDescendingCommand = ReactiveCommand.Create(() => TreeDataGridSource.SortBy(TreeDataGridSource.Columns[3], ListSortDirection.Descending)) },
];

SetInitialSelection(initialCoinSelection);
}

Expand Down Expand Up @@ -171,7 +172,7 @@ private void Rebuild(ISourceList<CoinListItem> source, IEnumerable<Pocket> pocke
return new PocketViewModel(_wallet, pocket, _allowCoinjoiningCoinSelection, _ignorePrivacyMode);
});

source.EditDiff(newItems);
source.EditDiff(newItems, new LambdaComparer<CoinListItem>((a, b) => Equals(a?.Key, b?.Key)));
}

private void RestoreExpandedRows(IEnumerable<LabelsArray> oldItemsLabels)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ public CoinViewModel(LabelsArray labels, ICoinModel coin, bool canSelectWhenCoin
.Where(b => !b)
.Do(_ => IsSelected = false)
.Subscribe();

if (!canSelectWhenCoinjoining)
{
this.WhenAnyValue(x => x.Coin.IsCoinJoinInProgress, b => !b).BindTo(this, x => x.CanBeSelected).DisposeWith(_disposables);
}
}

public ICoinModel Coin { get; }
public override string Key => Coin.Key.ToString();
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,6 @@ private IObservable<bool> ThereAreSelectableCoins() => Children

return commonItem;
}

public override string Key => Labels.ToString();
}

0 comments on commit 86c4b73

Please sign in to comment.