wabbajack/Wabbajack/View Models/ManifestVM.cs

114 lines
3.9 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;
2020-02-19 16:10:02 +00:00
using CefSharp;
2020-02-15 15:45:53 +00:00
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 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()
};
});
}
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:10:02 +00:00
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;
});
2020-02-15 15:45:53 +00:00
_searchResults =
this.WhenAnyValue(x => x.SearchTerm)
2020-02-19 16:10:02 +00:00
/*.CombineLatest(
this.WhenAnyValue(x => x.SortAscending),
this.WhenAnyValue(x => x.SortEnum),
(term, ascending, sort) => term)*/
2020-02-15 15:45:53 +00:00
.Throttle(TimeSpan.FromMilliseconds(800))
.Select(term => term?.Trim())
.DistinctUntilChanged()
.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:"))
return x.Hash.StartsWith(term.Replace("hash:", ""));
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));
}
}
}