wabbajack/Wabbajack.App.Wpf/View Models/Gallery/ModListGalleryVM.cs

187 lines
7.0 KiB
C#
Raw Normal View History


using System;
2021-12-26 21:56:44 +00:00
using System.Collections.Generic;
using System.Collections.ObjectModel;
2021-12-26 21:56:44 +00:00
using System.Linq;
using System.Reactive;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Threading.Tasks;
2021-12-26 21:56:44 +00:00
using System.Windows.Input;
using DynamicData;
using DynamicData.Binding;
2021-12-27 23:15:30 +00:00
using Microsoft.Extensions.DependencyInjection;
2021-12-27 06:10:11 +00:00
using Microsoft.Extensions.Logging;
2021-12-26 21:56:44 +00:00
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using Wabbajack.Common;
2021-12-27 06:10:11 +00:00
using Wabbajack.Downloaders.GameFile;
using Wabbajack.DTOs;
using Wabbajack.Hashing.xxHash64;
using Wabbajack.Lib.Extensions;
using Wabbajack.Networking.WabbajackClientApi;
using Wabbajack.Services.OSIntegrated;
2021-12-27 23:15:30 +00:00
using Wabbajack.Services.OSIntegrated.Services;
2021-12-26 21:56:44 +00:00
namespace Wabbajack
{
public class ModListGalleryVM : BackNavigatingVM
{
public MainWindowVM MWVM { get; }
private readonly SourceCache<ModListMetadataVM, string> _modLists = new(x => x.Metadata.Links.MachineURL);
public ReadOnlyObservableCollection<ModListMetadataVM> _filteredModLists;
public ReadOnlyObservableCollection<ModListMetadataVM> ModLists => _filteredModLists;
2021-12-26 21:56:44 +00:00
private const string ALL_GAME_TYPE = "All";
[Reactive] public IErrorResponse Error { get; set; }
2021-12-26 21:56:44 +00:00
[Reactive] public string Search { get; set; }
2021-12-26 21:56:44 +00:00
[Reactive] public bool OnlyInstalled { get; set; }
2021-12-26 21:56:44 +00:00
[Reactive] public bool ShowNSFW { get; set; }
2021-12-26 21:56:44 +00:00
[Reactive] public bool ShowUtilityLists { get; set; }
2021-12-26 21:56:44 +00:00
[Reactive] public string GameType { get; set; }
2021-12-26 21:56:44 +00:00
public List<string> GameTypeEntries => GetGameTypeEntries();
2021-12-26 21:56:44 +00:00
private ObservableAsPropertyHelper<bool> _Loaded;
2021-12-27 06:10:11 +00:00
private readonly Client _wjClient;
private readonly ILogger<ModListGalleryVM> _logger;
private readonly GameLocator _locator;
private readonly ModListDownloadMaintainer _maintainer;
2021-12-26 21:56:44 +00:00
private FiltersSettings settings { get; set; } = new();
2021-12-26 21:56:44 +00:00
public bool Loaded => _Loaded.Value;
public ICommand ClearFiltersCommand { get; set; }
2021-12-26 21:56:44 +00:00
public ModListGalleryVM(ILogger<ModListGalleryVM> logger, Client wjClient,
GameLocator locator, SettingsManager settingsManager, ModListDownloadMaintainer maintainer)
: base(logger)
2021-12-26 21:56:44 +00:00
{
2021-12-27 06:10:11 +00:00
_wjClient = wjClient;
_logger = logger;
_locator = locator;
_maintainer = maintainer;
2021-12-26 21:56:44 +00:00
ClearFiltersCommand = ReactiveCommand.Create(
() =>
{
OnlyInstalled = false;
ShowNSFW = false;
ShowUtilityLists = false;
Search = string.Empty;
GameType = ALL_GAME_TYPE;
});
this.WhenActivated(disposables =>
{
var _ = LoadModLists();
2021-12-29 17:50:36 +00:00
var searchTextPredicates = this.ObservableForProperty(vm => vm.Search)
.Select(change => change.Value)
.StartWith("")
.Select<string, Func<ModListMetadataVM, bool>>(txt =>
{
if (string.IsNullOrWhiteSpace(txt)) return _ => true;
return item => item.Metadata.Title.ContainsCaseInsensitive(txt) ||
item.Metadata.Description.ContainsCaseInsensitive(txt);
});
var onlyInstalledGamesFilter = this.ObservableForProperty(vm => vm.OnlyInstalled)
.Select(v => v.Value)
.Select<bool, Func<ModListMetadataVM, bool>>(onlyInstalled =>
{
if (onlyInstalled == false) return _ => true;
return item => _locator.IsInstalled(item.Metadata.Game);
})
.StartWith(_ => true);
var onlyUtilityListsFilter = this.ObservableForProperty(vm => vm.ShowUtilityLists)
.Select(v => v.Value)
.Select<bool, Func<ModListMetadataVM, bool>>(utility =>
{
if (utility == false) return item => item.Metadata.UtilityList == false;
return item => item.Metadata.UtilityList;
})
.StartWith(item => item.Metadata.UtilityList == false);
var showNSFWFilter = this.ObservableForProperty(vm => vm.ShowNSFW)
.Select(v => v.Value)
.Select<bool, Func<ModListMetadataVM, bool>>(showNsfw => { return item => item.Metadata.NSFW == showNsfw; })
.StartWith(item => item.Metadata.NSFW == false);
var gameFilter = this.ObservableForProperty(vm => vm.GameType)
.Select(v => v.Value)
.Select<string, Func<ModListMetadataVM, bool>>(selected =>
{
if (selected is null or ALL_GAME_TYPE) return _ => true;
return item => item.Metadata.Game.MetaData().HumanFriendlyGameName == selected;
})
.StartWith(_ => true);
_modLists.Connect()
.ObserveOn(RxApp.MainThreadScheduler)
2021-12-29 17:50:36 +00:00
.Filter(searchTextPredicates)
.Filter(onlyInstalledGamesFilter)
.Filter(onlyUtilityListsFilter)
.Filter(showNSFWFilter)
.Filter(gameFilter)
.Bind(out _filteredModLists)
.Subscribe()
.DisposeWith(disposables);
});
2021-12-26 21:56:44 +00:00
}
public override void Unload()
{
Error = null;
}
private async Task LoadModLists()
{
using var ll = LoadingLock.WithLoading();
try
{
var modLists = await _wjClient.LoadLists();
_modLists.Edit(e =>
{
e.Clear();
e.AddOrUpdate(modLists.Select(m =>
new ModListMetadataVM(_logger, this, m, _maintainer, _wjClient)));
});
}
catch (Exception ex)
{
_logger.LogError(ex, "While loading lists");
ll.Fail();
}
ll.Succeed();
}
2021-12-26 21:56:44 +00:00
private List<string> GetGameTypeEntries()
{
List<string> gameEntries = new List<string> {ALL_GAME_TYPE};
2021-12-27 06:10:11 +00:00
gameEntries.AddRange(GameRegistry.Games.Values.Select(gameType => gameType.HumanFriendlyGameName));
2021-12-26 21:56:44 +00:00
gameEntries.Sort();
return gameEntries;
}
private void UpdateFiltersSettings()
{
settings.Game = GameType;
settings.Search = Search;
settings.ShowNSFW = ShowNSFW;
settings.ShowUtilityLists = ShowUtilityLists;
settings.OnlyInstalled = OnlyInstalled;
}
}
}