Skip to content

Commit

Permalink
Use hashset and dictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
erri120 committed Sep 12, 2024
1 parent 6176e03 commit 9fbae51
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
25 changes: 25 additions & 0 deletions src/NexusMods.App.UI/Extensions/R3Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,29 @@ public static void ApplyChanges<TKey, TValue>(this ObservableList<TValue> list,
}
}
}

public static void ApplyChanges<TKey, TValue>(this ObservableHashSet<TValue> set, IChangeSet<TValue, TKey> 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;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class FakeParentLibraryItemModel : LibraryItemModel
{
public required IObservable<int> NumInstalledObservable { get; init; }
public required IObservable<IChangeSet<LibraryItem.ReadOnly, EntityId>> LibraryItemsObservable { get; init; }
protected ObservableList<LibraryItem.ReadOnly> LibraryItems { get; set; } = [];
protected ObservableHashSet<LibraryItem.ReadOnly> LibraryItems { get; set; } = [];

public override IReadOnlyCollection<LibraryItemId> GetLoadoutItemIds() => LibraryItems.Select(static item => item.LibraryItemId).ToArray();

Expand Down
4 changes: 2 additions & 2 deletions src/NexusMods.App.UI/Pages/LibraryPage/LibraryItemModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class LibraryItemModel : TreeDataGridItemModel<LibraryItemModel, EntityId
public BindableReactiveProperty<string> Version { get; set; } = new("-");

public IObservable<IChangeSet<LibraryLinkedLoadoutItem.ReadOnly, EntityId>> LinkedLoadoutItemsObservable { get; init; } = System.Reactive.Linq.Observable.Empty<IChangeSet<LibraryLinkedLoadoutItem.ReadOnly, EntityId>>();
private ObservableList<LibraryLinkedLoadoutItem.ReadOnly> LinkedLoadoutItems { get; set; } = [];
private ObservableDictionary<EntityId, LibraryLinkedLoadoutItem.ReadOnly> LinkedLoadoutItems { get; set; } = [];

public ReactiveProperty<DateTime> InstalledDate { get; } = new(DateTime.UnixEpoch);
public ReactiveProperty<DateTime> CreatedAtDate { get; } = new(DateTime.UnixEpoch);
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class FakeParentLoadoutItemModel : LoadoutItemModel
public required IObservable<DateTime> InstalledAtObservable { get; init; }

public required IObservable<IChangeSet<LoadoutItemId, EntityId>> LoadoutItemIdsObservable { get; init; }
public ObservableList<LoadoutItemId> LoadoutItemIds { get; private set; } = [];
public ObservableHashSet<LoadoutItemId> LoadoutItemIds { get; private set; } = [];

public override IReadOnlyCollection<LoadoutItemId> GetLoadoutItemIds() => LoadoutItemIds;

Expand Down

0 comments on commit 9fbae51

Please sign in to comment.