using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Reactive; using System.Reactive.Disposables; using System.Reactive.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Input; using DynamicData; using DynamicData.Binding; using ReactiveUI; using ReactiveUI.Fody.Helpers; using Wabbajack.Common; using Wabbajack.Lib; using Wabbajack.Lib.ModListRegistry; namespace Wabbajack { public class ModListGalleryVM : BackNavigatingVM { public MainWindowVM MWVM { get; } public ObservableCollectionExtended ModLists { get; } = new ObservableCollectionExtended(); private FiltersSettings settings; private int missingHashFallbackCounter; private const string ALL_GAME_TYPE = "All"; public ICommand OnlyInstalledCheckedCommand { get; } [Reactive] public IErrorResponse Error { get; set; } [Reactive] public string Search { get; set; } [Reactive] public bool OnlyInstalled { get; set; } [Reactive] public bool ShowNSFW { get; set; } [Reactive] public string GameType { get; set; } [Reactive] public bool GameTypeEnabled { get; set; } public List GameTypeEntries { get { return GetGameTypeEntries(); } } private readonly ObservableAsPropertyHelper _Loaded; public bool Loaded => _Loaded.Value; public ICommand ClearFiltersCommand { get; } public ModListGalleryVM(MainWindowVM mainWindowVM) : base(mainWindowVM) { MWVM = mainWindowVM; GameTypeEnabled = true; // load persistent filter settings settings = MWVM.Settings.Filters; if (settings.isPersistent) { GameType = !string.IsNullOrEmpty(settings.Game) ? settings.Game : ALL_GAME_TYPE; ShowNSFW = settings.showNSFW; OnlyInstalled = settings.OnlyInstalled; if (OnlyInstalled) GameTypeEnabled = false; Search = settings.Search; } else GameType = ALL_GAME_TYPE; ClearFiltersCommand = ReactiveCommand.Create( () => { OnlyInstalled = false; ShowNSFW = false; Search = string.Empty; GameType = ALL_GAME_TYPE; GameTypeEnabled = true; UpdateFiltersSettings(); }); var random = new Random(); var sourceList = Observable.Return(Unit.Default) .ObserveOn(RxApp.TaskpoolScheduler) .SelectTask(async _ => { try { Error = null; var list = await ModlistMetadata.LoadFromGithub(); Error = ErrorResponse.Success; return list // Sort randomly initially, just to give each list a fair shake .Shuffle(random) .AsObservableChangeSet(x => x.DownloadMetadata?.Hash ?? Hash.Empty); } catch (Exception ex) { Utils.Error(ex); Error = ErrorResponse.Fail(ex); return Observable.Empty>(); } }) // Unsubscribe and release when not active .FlowSwitch( this.WhenAny(x => x.IsActive), valueWhenOff: Observable.Return(ChangeSet.Empty)) .Switch() .RefCount(); _Loaded = sourceList.CollectionCount() .Select(c => c > 0) .ToProperty(this, nameof(Loaded)); OnlyInstalledCheckedCommand = ReactiveCommand.Create(() => { if (OnlyInstalled) { GameType = ALL_GAME_TYPE; GameTypeEnabled = false; } else GameTypeEnabled = true; }); // Convert to VM and bind to resulting list sourceList .ObserveOnGuiThread() .Transform(m => new ModListMetadataVM(this, m)) .DisposeMany() // Filter only installed .Filter(this.WhenAny(x => x.OnlyInstalled) .Select>(onlyInstalled => (vm) => { UpdateFiltersSettings(); if (!onlyInstalled) return true; if (!GameRegistry.Games.TryGetValue(vm.Metadata.Game, out var gameMeta)) return false; return gameMeta.IsInstalled; })) // Filter on search box .Filter(this.WhenAny(x => x.Search) .Debounce(TimeSpan.FromMilliseconds(150), RxApp.MainThreadScheduler) .Select>(search => (vm) => { if (string.IsNullOrWhiteSpace(search)) return true; UpdateFiltersSettings(); return vm.Metadata.Title.ContainsCaseInsensitive(search); })) .Filter(this.WhenAny(x => x.ShowNSFW) .Select>(showNSFW => vm => { UpdateFiltersSettings(); if (!vm.Metadata.NSFW) return true; return vm.Metadata.NSFW && showNSFW; })) // Filter by Game .Filter(this.WhenAny(x => x.GameType) .Debounce(TimeSpan.FromMilliseconds(150), RxApp.MainThreadScheduler) .Select>(GameType => (vm) => { if (GameType == ALL_GAME_TYPE) return true; if (string.IsNullOrEmpty(GameType)) return false; UpdateFiltersSettings(); return GameType == vm.Metadata.Game.GetDescription().ToString(); })) .Filter(this.WhenAny(x => x.ShowNSFW) .Select>(showNSFW => vm => { UpdateFiltersSettings(); if (!vm.Metadata.NSFW) return true; return vm.Metadata.NSFW && showNSFW; })) // Put broken lists at bottom .Sort(Comparer.Create((a, b) => a.IsBroken.CompareTo(b.IsBroken))) .Bind(ModLists) .Subscribe() .DisposeWith(CompositeDisposable); // Extra GC when navigating away, just to immediately clean up modlist metadata this.WhenAny(x => x.IsActive) .Where(x => !x) .Skip(1) .Delay(TimeSpan.FromMilliseconds(50), RxApp.MainThreadScheduler) .Subscribe(_ => { GC.Collect(); }) .DisposeWith(CompositeDisposable); } public override void Unload() { Error = null; } private List GetGameTypeEntries() { List gameEntries = new List { ALL_GAME_TYPE }; gameEntries.AddRange(EnumExtensions.GetAllItems().Select(gameType => gameType.GetDescription())); return gameEntries; } private void UpdateFiltersSettings() { if (!settings.isPersistent) return; if (!string.IsNullOrEmpty(GameType)) settings.Game = GameType; if (Search != null) settings.Search = Search; settings.showNSFW = ShowNSFW; settings.OnlyInstalled = OnlyInstalled; } } }