Skip to content

Commit

Permalink
Sort and optionally hide Override (#1341)
Browse files Browse the repository at this point in the history
* Add last and fix tests

Tests were using `BeEquivalentTo` which doesn't care about the order of
elements.

* Sort last by putting self after others

* Hide Override
  • Loading branch information
erri120 authored May 13, 2024
1 parent 3764766 commit 1bf5814
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,8 @@ await Parallel.ForEachAsync(files, async (file, cancellationToken) =>
{
if (mod.Category == ModCategory.GameFiles)
return [new First<Mod.Model, ModId>()];
if (mod.Category == ModCategory.Overrides)
return [new Last<Mod.Model, ModId>()];
if (mod.TryGet(Mod.SortAfter, out var other))
return [new After<Mod.Model, ModId> { Other = ModId.From(other) }];
return [];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using JetBrains.Annotations;

namespace NexusMods.Abstractions.DataModel.Entities.Sorting;

[PublicAPI]
public record Last<TType, TId> : ISortRule<TType, TId>;
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,13 @@ public LoadoutGridViewModel(
.SelectMany(id => loadoutRepository.Revisions(id.Value))
.Select(loadout =>
{
var showGameFiles = settingsManager.Get<LoadoutGridSettings>().ShowGameFiles;
var settings = settingsManager.Get<LoadoutGridSettings>();
var showGameFiles = settings.ShowGameFiles;
var showOverride = settings.ShowOverride;

return loadout.Mods
.Where(m => showGameFiles || m.Category != ModCategory.GameFiles)
.Where(m => showOverride || m.Category != ModCategory.Overrides)
.Select(m => m.ModId);
})
.OnUI()
Expand Down
11 changes: 10 additions & 1 deletion src/NexusMods.App.UI/Settings/LoadoutGridSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public record LoadoutGridSettings : ISettings
{
public bool ShowGameFiles { get; set; }

public bool ShowOverride { get; set; }

public static ISettingsBuilder Configure(ISettingsBuilder settingsBuilder)
{
// TODO: put in some section
Expand All @@ -16,7 +18,14 @@ public static ISettingsBuilder Configure(ISettingsBuilder settingsBuilder)
.AddToSection(sectionId)
.WithDisplayName("Show Game Files")
.WithDescription("Shows the Game Files in the Mods page.")
.UseBooleanContainer())
.UseBooleanContainer()
)
.AddPropertyToUI(x => x.ShowOverride, propertyBuilder => propertyBuilder
.AddToSection(sectionId)
.WithDisplayName("Show Override")
.WithDescription("Shows Override in the Mods page.")
.UseBooleanContainer()
)
);
}
}
15 changes: 8 additions & 7 deletions src/NexusMods.DataModel/Sorting/Sorter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ private static TId[] RefineRules<TItem, TId>(TItem thisItem,
{
idsBuffer.Clear();
var haveFirst = false;
var isLast = false;
var rulesForThisItem = ruleFn(thisItem);

/*
Expand All @@ -170,20 +171,21 @@ private static TId[] RefineRules<TItem, TId>(TItem thisItem,
- Sewer
*/
// ReSharper disable once ForCanBeConvertedToForeach
for (var x = 0; x < rulesForThisItem.Count; x++)
{
switch (rulesForThisItem[x])
{
case First<TItem, TId>:
// Handled later
haveFirst = true;
break;
case Last<TItem, TId>:
isLast = true;
break;
case After<TItem, TId> after:
idsBuffer.Add(after.Other);
break;
case Before<TItem, TId>:
// Handled later
break;

}
}

Expand All @@ -195,6 +197,8 @@ private static TId[] RefineRules<TItem, TId>(TItem thisItem,
if (otherId.Equals(idForThisItem))
continue;

if (isLast) idsBuffer.Add(otherId);

var rulesForOtherItem = ruleFn(itm);
for (var y = 0; y < rulesForOtherItem.Count; y++)
{
Expand All @@ -205,9 +209,6 @@ private static TId[] RefineRules<TItem, TId>(TItem thisItem,
if (!haveFirst)
idsBuffer.Add(otherId);
break;
case After<TItem, TId>:
// Handled above
break;
case Before<TItem, TId> b:
if (b.Other.Equals(idForThisItem))
idsBuffer.Add(otherId);
Expand Down
28 changes: 25 additions & 3 deletions tests/NexusMods.DataModel.Tests/SortTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,29 @@ public void FirstItemsComeFirst()

new Sorter().Sort<Item, string>(data, x => x.Id, x => x.Rules)
.Select(i => i.Id)
.Should().BeEquivalentTo("A", "B");
.Should().Equal("A", "B");
}

[Fact]
public void FirstAndLast()
{
var data = new List<Item>
{
new() { Id = "B", Rules = [] },
new() { Id = "C", Rules = [] },
new() { Id = "D", Rules = [ new Last<Item, string>()] },
new() { Id = "E", Rules = [] },
new() { Id = "F", Rules = [] },
new() { Id = "G", Rules = [] },
new() { Id = "A", Rules = [ new First<Item, string>()] },
};

var res = new Sorter().Sort<Item, string>(data, x => x.Id, x => x.Rules)
.Select(i => i.Id)
.ToArray();

res.First().Should().Be("A");
res.Last().Should().Be("D");
}

[Fact]
Expand All @@ -46,7 +68,7 @@ public void BeforeAndAfterWorks()

new Sorter().Sort<Item, string>(data, x => x.Id, x => x.Rules)
.Select(i => i.Id)
.Should().BeEquivalentTo("A", "B", "C");
.Should().Equal("A", "B", "C");
}

[Fact]
Expand Down Expand Up @@ -97,7 +119,7 @@ public void LargeComplexCollectionsCanBeSorted()

new Sorter().Sort<Item, string>(rules, x => x.Id, x => x.Rules)
.Select(i => i.Id)
.Should().BeEquivalentTo(letters.Concat(numbers));
.Should().Equal(letters.Concat(numbers));
}

private IEnumerable<Item> Shuffle(List<Item> rules)
Expand Down
1 change: 1 addition & 0 deletions tests/NexusMods.UI.Tests/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public void ConfigureServices(IServiceCollection services)
.OverrideSettingsForTests<LoadoutGridSettings>(settings => settings with
{
ShowGameFiles = true,
ShowOverride = true,
})
.AddStubbedGameLocators()
.AddSingleton<AvaloniaApp>()
Expand Down

0 comments on commit 1bf5814

Please sign in to comment.