wabbajack/Wabbajack/View Models/ManifestVM.cs

103 lines
3.6 KiB
C#
Raw Normal View History

2020-02-15 15:45:53 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
2020-02-19 16:10:02 +00:00
using System.Reactive;
2020-02-15 15:45:53 +00:00
using System.Reactive.Linq;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
2020-02-02 12:36:22 +00:00
using Wabbajack.Common;
using Wabbajack.Lib;
2020-02-02 12:36:22 +00:00
namespace Wabbajack
{
2020-02-19 16:10:02 +00:00
public enum SortBy { Name, Size }
public class ManifestVM : ViewModel
2020-02-19 16:10:02 +00:00
{
2020-02-02 12:36:22 +00:00
public Manifest Manifest { get; set; }
2020-02-02 12:36:22 +00:00
public string Name => !string.IsNullOrWhiteSpace(Manifest.Name) ? Manifest.Name : "Wabbajack Modlist";
public string Author => !string.IsNullOrWhiteSpace(Manifest.Author) ? $"Created by {Manifest.Author}" : "Created by Jyggalag";
public string Description => !string.IsNullOrWhiteSpace(Manifest.Description) ? Manifest.Description : "";
public string InstallSize => $"Install Size: {Manifest.InstallSize.ToFileSizeString()}";
public string DownloadSize => $"Download Size: {Manifest.DownloadSize.ToFileSizeString()}";
public IEnumerable<Archive> Archives => Manifest.Archives;
2020-02-15 15:45:53 +00:00
[Reactive]
public string SearchTerm { get; set; }
private readonly ObservableAsPropertyHelper<IEnumerable<Archive>> _searchResults;
public IEnumerable<Archive> SearchResults => _searchResults.Value;
2020-02-19 16:10:02 +00:00
[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
{
2020-02-19 16:35:34 +00:00
SortBy.Name => list.OrderBy(x => x.Name),
SortBy.Size => list.OrderBy(x => x.Size),
2020-02-19 16:10:02 +00:00
_ => throw new ArgumentOutOfRangeException()
};
2020-02-19 16:35:34 +00:00
}
return SortEnum switch
{
SortBy.Name => list.OrderByDescending(x => x.Name),
SortBy.Size => list.OrderByDescending(x => x.Size),
_ => throw new ArgumentOutOfRangeException()
};
2020-02-19 16:10:02 +00:00
}
2020-02-19 16:37:03 +00:00
private void Swap(SortBy to)
{
if (SortEnum != to)
SortEnum = to;
else
SortAscending = !SortAscending;
}
2020-02-02 12:36:22 +00:00
public ManifestVM(Manifest manifest)
{
2020-02-02 12:36:22 +00:00
Manifest = manifest;
2020-02-15 15:45:53 +00:00
2020-02-19 16:37:03 +00:00
SortByNameCommand = ReactiveCommand.Create(() => Swap(SortBy.Name));
SortBySizeCommand = ReactiveCommand.Create(() => Swap(SortBy.Size));
2020-02-19 16:10:02 +00:00
2020-02-15 15:45:53 +00:00
_searchResults =
this.WhenAnyValue(x => x.SearchTerm)
2020-02-19 16:35:34 +00:00
.CombineLatest(
2020-02-19 16:10:02 +00:00
this.WhenAnyValue(x => x.SortAscending),
this.WhenAnyValue(x => x.SortEnum),
2020-02-19 16:35:34 +00:00
(term, ascending, sort) => term)
2020-02-15 15:45:53 +00:00
.Throttle(TimeSpan.FromMilliseconds(800))
.Select(term => term?.Trim())
2020-02-19 16:35:34 +00:00
//.DistinctUntilChanged()
2020-02-15 15:45:53 +00:00
.Select(term =>
{
2020-02-15 15:53:51 +00:00
if (string.IsNullOrWhiteSpace(term))
2020-02-19 16:10:02 +00:00
return Order(Archives);
2020-02-15 15:53:51 +00:00
2020-02-19 16:10:02 +00:00
return Order(Archives.Where(x =>
2020-02-15 15:53:51 +00:00
{
if (term.StartsWith("hash:"))
2020-03-22 15:50:53 +00:00
return x.Hash.ToString().StartsWith(term.Replace("hash:", ""));
2020-02-15 15:53:51 +00:00
return x.Name.StartsWith(term);
2020-02-19 16:10:02 +00:00
}));
2020-02-15 15:45:53 +00:00
})
2020-02-19 16:10:02 +00:00
.ToGuiProperty(this, nameof(SearchResults), Order(Archives));
}
}
}