From 9fbae51ec824d0a663285edd1546dafb1cba1b8b Mon Sep 17 00:00:00 2001 From: erri120 Date: Thu, 12 Sep 2024 13:44:52 +0200 Subject: [PATCH] Use hashset and dictionary --- .../Extensions/R3Extensions.cs | 25 +++++++++++++++++++ .../LibraryPage/FakeParentLibraryItemModel.cs | 2 +- .../Pages/LibraryPage/LibraryItemModel.cs | 4 +-- .../LoadoutPage/FakeParentLoadoutItemModel.cs | 2 +- 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/NexusMods.App.UI/Extensions/R3Extensions.cs b/src/NexusMods.App.UI/Extensions/R3Extensions.cs index c32183f206..b308b77b04 100644 --- a/src/NexusMods.App.UI/Extensions/R3Extensions.cs +++ b/src/NexusMods.App.UI/Extensions/R3Extensions.cs @@ -150,4 +150,29 @@ public static void ApplyChanges(this ObservableList list, } } } + + public static void ApplyChanges(this ObservableHashSet set, IChangeSet changes) + where TValue : notnull + where TKey : notnull + { + foreach (var change in changes) + { + switch (change.Reason) + { + case ChangeReason.Add: + set.Add(change.Current); + break; + case ChangeReason.Remove: + set.Remove(change.Current); + break; + case ChangeReason.Update: + if (set.Remove(change.Previous.Value)) + { + set.Add(change.Current); + } + + break; + } + } + } } diff --git a/src/NexusMods.App.UI/Pages/LibraryPage/FakeParentLibraryItemModel.cs b/src/NexusMods.App.UI/Pages/LibraryPage/FakeParentLibraryItemModel.cs index fdfcbed5b5..2ea905b2e6 100644 --- a/src/NexusMods.App.UI/Pages/LibraryPage/FakeParentLibraryItemModel.cs +++ b/src/NexusMods.App.UI/Pages/LibraryPage/FakeParentLibraryItemModel.cs @@ -12,7 +12,7 @@ public class FakeParentLibraryItemModel : LibraryItemModel { public required IObservable NumInstalledObservable { get; init; } public required IObservable> LibraryItemsObservable { get; init; } - protected ObservableList LibraryItems { get; set; } = []; + protected ObservableHashSet LibraryItems { get; set; } = []; public override IReadOnlyCollection GetLoadoutItemIds() => LibraryItems.Select(static item => item.LibraryItemId).ToArray(); diff --git a/src/NexusMods.App.UI/Pages/LibraryPage/LibraryItemModel.cs b/src/NexusMods.App.UI/Pages/LibraryPage/LibraryItemModel.cs index 80f36666a8..03e36ab3fb 100644 --- a/src/NexusMods.App.UI/Pages/LibraryPage/LibraryItemModel.cs +++ b/src/NexusMods.App.UI/Pages/LibraryPage/LibraryItemModel.cs @@ -25,7 +25,7 @@ public class LibraryItemModel : TreeDataGridItemModel Version { get; set; } = new("-"); public IObservable> LinkedLoadoutItemsObservable { get; init; } = System.Reactive.Linq.Observable.Empty>(); - private ObservableList LinkedLoadoutItems { get; set; } = []; + private ObservableDictionary LinkedLoadoutItems { get; set; } = []; public ReactiveProperty InstalledDate { get; } = new(DateTime.UnixEpoch); public ReactiveProperty CreatedAtDate { get; } = new(DateTime.UnixEpoch); @@ -78,7 +78,7 @@ public LibraryItemModel(LibraryItemId libraryItemId) { model.InstallText.Value = "Installed"; model.IsInstalledInLoadout.Value = true; - model.InstalledDate.Value = model.LinkedLoadoutItems.Select(static item => item.GetCreatedAt()).Max(); + model.InstalledDate.Value = model.LinkedLoadoutItems.Select(static kv => kv.Value.GetCreatedAt()).Max(); model.FormattedInstalledDate.Value = FormatDate(DateTime.Now, model.InstalledDate.Value); } else diff --git a/src/NexusMods.App.UI/Pages/LoadoutPage/FakeParentLoadoutItemModel.cs b/src/NexusMods.App.UI/Pages/LoadoutPage/FakeParentLoadoutItemModel.cs index 8ded97a193..1ef3241e7c 100644 --- a/src/NexusMods.App.UI/Pages/LoadoutPage/FakeParentLoadoutItemModel.cs +++ b/src/NexusMods.App.UI/Pages/LoadoutPage/FakeParentLoadoutItemModel.cs @@ -12,7 +12,7 @@ public class FakeParentLoadoutItemModel : LoadoutItemModel public required IObservable InstalledAtObservable { get; init; } public required IObservable> LoadoutItemIdsObservable { get; init; } - public ObservableList LoadoutItemIds { get; private set; } = []; + public ObservableHashSet LoadoutItemIds { get; private set; } = []; public override IReadOnlyCollection GetLoadoutItemIds() => LoadoutItemIds;