wabbajack/Wabbajack/View Models/BackNavigatingVM.cs

75 lines
2.3 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
2020-01-07 05:44:05 +00:00
using System.Reactive;
using System.Reactive.Linq;
2020-12-07 04:26:02 +00:00
using System.Reactive.Subjects;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using Wabbajack.Common;
using Wabbajack.Lib;
namespace Wabbajack
{
public interface IBackNavigatingVM : IReactiveObject
{
ViewModel NavigateBackTarget { get; set; }
2020-01-07 05:44:05 +00:00
ReactiveCommand<Unit, Unit> BackCommand { get; }
2020-12-07 04:26:02 +00:00
Subject<bool> IsBackEnabledSubject { get; }
IObservable<bool> IsBackEnabled { get; }
}
public class BackNavigatingVM : ViewModel, IBackNavigatingVM
{
[Reactive]
public ViewModel NavigateBackTarget { get; set; }
2020-01-07 05:44:05 +00:00
public ReactiveCommand<Unit, Unit> BackCommand { get; protected set; }
2021-01-29 04:02:26 +00:00
protected ObservableAsPropertyHelper<bool> _IsActive;
public bool IsActive => _IsActive.Value;
2020-12-07 04:26:02 +00:00
public Subject<bool> IsBackEnabledSubject { get; } = new Subject<bool>();
public IObservable<bool> IsBackEnabled { get; }
public BackNavigatingVM(MainWindowVM mainWindowVM)
{
2020-12-07 04:26:02 +00:00
IsBackEnabled = IsBackEnabledSubject.StartWith(true);
BackCommand = ReactiveCommand.Create(
execute: () => Utils.CatchAndLog(() =>
{
mainWindowVM.NavigateTo(NavigateBackTarget);
Unload();
}),
canExecute: this.ConstructCanNavigateBack()
.ObserveOnGuiThread());
_IsActive = this.ConstructIsActive(mainWindowVM)
.ToGuiProperty(this, nameof(IsActive));
}
public virtual void Unload()
{
}
}
public static class IBackNavigatingVMExt
{
public static IObservable<bool> ConstructCanNavigateBack(this IBackNavigatingVM vm)
{
return vm.WhenAny(x => x.NavigateBackTarget)
2020-12-07 04:26:02 +00:00
.CombineLatest(vm.IsBackEnabled)
.Select(x => x.First != null && x.Second);
}
public static IObservable<bool> ConstructIsActive(this IBackNavigatingVM vm, MainWindowVM mwvm)
{
return mwvm.WhenAny(x => x.ActivePane)
.Select(x => object.ReferenceEquals(vm, x));
}
}
}