Add sort functions to the new buttons

This commit is contained in:
erri120 2020-02-19 17:10:02 +01:00
parent e5539b5e2e
commit ea39485f27
No known key found for this signature in database
GPG Key ID: A8C0A18D8D4D3135
2 changed files with 68 additions and 5 deletions

View File

@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive;
using System.Reactive.Linq;
using CefSharp;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using Wabbajack.Common;
@ -9,8 +11,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 +31,83 @@ 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 list.OrderBy(x =>
{
return SortEnum switch
{
SortBy.Name => x.Name,
SortBy.Size => x.Name,
_ => throw new ArgumentOutOfRangeException()
};
});
}
return list.OrderByDescending(x =>
{
return SortEnum switch
{
SortBy.Name => x.Name,
SortBy.Size => x.Name,
_ => throw new ArgumentOutOfRangeException()
};
});
}
public ManifestVM(Manifest manifest)
{
Manifest = manifest;
SortByNameCommand = ReactiveCommand.Create(() =>
{
if (SortEnum != SortBy.Name)
SortEnum = SortBy.Name;
else
SortAscending = !SortAscending;
});
SortBySizeCommand = ReactiveCommand.Create(() =>
{
if (SortEnum != SortBy.Size)
SortEnum = SortBy.Size;
else
SortAscending = !SortAscending;
});
_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()
.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

@ -41,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);
});
}