Merge pull request #555 from erri120/manifest-sorting

Manifest sorting
This commit is contained in:
Timothy Baldridge 2020-02-19 20:51:10 -07:00 committed by GitHub
commit 2cadc5e91d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 9 deletions

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive;
using System.Reactive.Linq;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
@ -9,8 +10,10 @@ using Wabbajack.Lib;
namespace Wabbajack
{
public enum SortBy { Name, Size }
public class ManifestVM : ViewModel
{
{
public Manifest Manifest { get; set; }
public string Name => !string.IsNullOrWhiteSpace(Manifest.Name) ? Manifest.Name : "Wabbajack Modlist";
@ -27,28 +30,73 @@ namespace Wabbajack
private readonly ObservableAsPropertyHelper<IEnumerable<Archive>> _searchResults;
public IEnumerable<Archive> SearchResults => _searchResults.Value;
[Reactive]
public bool SortAscending { get; set; } = true;
[Reactive]
public SortBy SortEnum { get; set; } = SortBy.Name;
public ReactiveCommand<Unit, Unit> SortByNameCommand;
public ReactiveCommand<Unit, Unit> SortBySizeCommand;
private IEnumerable<Archive> Order(IEnumerable<Archive> list)
{
if (SortAscending)
{
return SortEnum switch
{
SortBy.Name => list.OrderBy(x => x.Name),
SortBy.Size => list.OrderBy(x => x.Size),
_ => throw new ArgumentOutOfRangeException()
};
}
return SortEnum switch
{
SortBy.Name => list.OrderByDescending(x => x.Name),
SortBy.Size => list.OrderByDescending(x => x.Size),
_ => throw new ArgumentOutOfRangeException()
};
}
private void Swap(SortBy to)
{
if (SortEnum != to)
SortEnum = to;
else
SortAscending = !SortAscending;
}
public ManifestVM(Manifest manifest)
{
Manifest = manifest;
SortByNameCommand = ReactiveCommand.Create(() => Swap(SortBy.Name));
SortBySizeCommand = ReactiveCommand.Create(() => Swap(SortBy.Size));
_searchResults =
this.WhenAnyValue(x => x.SearchTerm)
.CombineLatest(
this.WhenAnyValue(x => x.SortAscending),
this.WhenAnyValue(x => x.SortEnum),
(term, ascending, sort) => term)
.Throttle(TimeSpan.FromMilliseconds(800))
.Select(term => term?.Trim())
.DistinctUntilChanged()
//.DistinctUntilChanged()
.Select(term =>
{
if (string.IsNullOrWhiteSpace(term))
return Archives;
return Order(Archives);
return Archives.Where(x =>
return Order(Archives.Where(x =>
{
if (term.StartsWith("hash:"))
return x.Hash.StartsWith(term.Replace("hash:", ""));
return x.Name.StartsWith(term);
});
}));
})
.ToGuiProperty(this, nameof(SearchResults), Archives);
.ToGuiProperty(this, nameof(SearchResults), Order(Archives));
}
}
}

View File

@ -33,6 +33,16 @@
<TextBlock Padding="6 0 0 0" FontSize="20" Text="Search:"/>
<TextBox x:Name="SearchBar" BorderThickness="2" BorderBrush="{StaticResource SearchBarBrush}" FontSize="16" MaxLength="100"/>
<StackPanel Margin="6 6 0 0" Orientation="Horizontal">
<TextBlock Padding="0 1 0 0" FontSize="14">Order by: </TextBlock>
<Button x:Name="OrderByNameButton" Padding="4 0 4 0">
<TextBlock FontSize="14">Name</TextBlock>
</Button>
<Button x:Name="OrderBySizeButton">
<TextBlock FontSize="14">Size</TextBlock>
</Button>
</StackPanel>
<ItemsControl Padding="0 3 0 6" x:Name="ModsList">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>

View File

@ -5,11 +5,8 @@ using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ReactiveUI;
using Wabbajack.Common;
using Wabbajack.Lib;
namespace Wabbajack
@ -44,6 +41,10 @@ namespace Wabbajack
.DisposeWith(disposable);
this.Bind(ViewModel, x => x.SearchTerm, x => x.SearchBar.Text)
.DisposeWith(disposable);
this.BindCommand(ViewModel, x => x.SortByNameCommand, x => x.OrderByNameButton)
.DisposeWith(disposable);
this.BindCommand(ViewModel, x => x.SortBySizeCommand, x => x.OrderBySizeButton)
.DisposeWith(disposable);
});
}