wabbajack/Wabbajack/View Models/Compilers/CompilerVM.cs

162 lines
6.8 KiB
C#
Raw Normal View History

using DynamicData;
using DynamicData.Binding;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using System;
using System.Linq;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Windows.Media.Imaging;
using Wabbajack.Common;
2019-12-04 04:12:08 +00:00
using Wabbajack.Common.StatusFeed;
using Wabbajack.Lib;
namespace Wabbajack
{
public class CompilerVM : ViewModel
{
public MainWindowVM MWVM { get; }
2019-11-21 15:45:00 +00:00
private readonly ObservableAsPropertyHelper<BitmapImage> _image;
public BitmapImage Image => _image.Value;
[Reactive]
public ModManager SelectedCompilerType { get; set; }
2019-11-21 15:45:00 +00:00
private readonly ObservableAsPropertyHelper<ISubCompilerVM> _compiler;
public ISubCompilerVM Compiler => _compiler.Value;
2019-11-21 15:45:00 +00:00
private readonly ObservableAsPropertyHelper<ModlistSettingsEditorVM> _currentModlistSettings;
public ModlistSettingsEditorVM CurrentModlistSettings => _currentModlistSettings.Value;
2019-11-21 15:45:00 +00:00
private readonly ObservableAsPropertyHelper<bool> _compiling;
public bool Compiling => _compiling.Value;
2019-11-21 05:15:47 +00:00
private readonly ObservableAsPropertyHelper<float> _percentCompleted;
public float PercentCompleted => _percentCompleted.Value;
public ObservableCollectionExtended<CPUStatus> StatusList { get; } = new ObservableCollectionExtended<CPUStatus>();
2019-12-04 04:12:08 +00:00
public ObservableCollectionExtended<IStatusMessage> Log => MWVM.Log;
2019-11-24 23:42:28 +00:00
public IReactiveCommand BackCommand { get; }
private readonly ObservableAsPropertyHelper<IUserIntervention> _ActiveGlobalUserIntervention;
public IUserIntervention ActiveGlobalUserIntervention => _ActiveGlobalUserIntervention.Value;
public CompilerVM(MainWindowVM mainWindowVM)
{
2019-11-21 15:04:33 +00:00
MWVM = mainWindowVM;
// Load settings
2019-11-21 15:04:33 +00:00
CompilerSettings settings = MWVM.Settings.Compiler;
SelectedCompilerType = settings.LastCompiledModManager;
MWVM.Settings.SaveSignal
.Subscribe(_ =>
{
2019-11-21 15:04:33 +00:00
settings.LastCompiledModManager = SelectedCompilerType;
})
2019-11-21 15:04:33 +00:00
.DisposeWith(CompositeDisposable);
// Swap to proper sub VM based on selected type
2019-11-21 15:45:00 +00:00
_compiler = this.WhenAny(x => x.SelectedCompilerType)
2019-11-15 04:54:34 +00:00
// Delay so the initial VM swap comes in immediately, subVM comes right after
.DelayInitial(TimeSpan.FromMilliseconds(50), RxApp.MainThreadScheduler)
.Select<ModManager, ISubCompilerVM>(type =>
{
switch (type)
{
case ModManager.MO2:
return new MO2CompilerVM(this);
case ModManager.Vortex:
2019-11-16 23:10:17 +00:00
return new VortexCompilerVM(this);
default:
return null;
}
})
// Unload old VM
.Pairwise()
.Do(pair =>
{
pair.Previous?.Unload();
})
.Select(p => p.Current)
2019-11-21 15:04:33 +00:00
.ToProperty(this, nameof(Compiler));
// Let sub VM determine what settings we're displaying and when
2019-11-21 15:45:00 +00:00
_currentModlistSettings = this.WhenAny(x => x.Compiler.ModlistSettings)
2019-11-21 15:04:33 +00:00
.ToProperty(this, nameof(CurrentModlistSettings));
2019-11-21 15:45:00 +00:00
_image = this.WhenAny(x => x.CurrentModlistSettings.ImagePath.TargetPath)
// Throttle so that it only loads image after any sets of swaps have completed
.Throttle(TimeSpan.FromMilliseconds(50), RxApp.MainThreadScheduler)
.DistinctUntilChanged()
.Select(path =>
{
2019-11-29 23:56:56 +00:00
if (string.IsNullOrWhiteSpace(path)) return UIUtils.BitmapImageFromResource("Resources/Wabba_Mouth_No_Text.png");
if (UIUtils.TryGetBitmapImageFromFile(path, out var image))
{
return image;
}
return null;
})
2019-11-21 15:04:33 +00:00
.ToProperty(this, nameof(Image));
2019-11-21 05:15:47 +00:00
2019-11-21 15:45:00 +00:00
_compiling = this.WhenAny(x => x.Compiler.ActiveCompilation)
2019-11-21 05:15:47 +00:00
.Select(compilation => compilation != null)
.ObserveOnGuiThread()
2019-11-21 15:04:33 +00:00
.ToProperty(this, nameof(Compiling));
2019-11-24 23:42:28 +00:00
BackCommand = ReactiveCommand.Create(
execute: () => mainWindowVM.ActivePane = mainWindowVM.ModeSelectionVM,
canExecute: this.WhenAny(x => x.Compiling)
.Select(x => !x));
// Compile progress updates and populate ObservableCollection
this.WhenAny(x => x.Compiler.ActiveCompilation)
.SelectMany(c => c?.QueueStatus ?? Observable.Empty<CPUStatus>())
.ObserveOn(RxApp.TaskpoolScheduler)
.ToObservableChangeSet(x => x.ID)
.Batch(TimeSpan.FromMilliseconds(250), RxApp.TaskpoolScheduler)
.EnsureUniqueChanges()
.Filter(i => i.IsWorking)
.ObserveOn(RxApp.MainThreadScheduler)
.Sort(SortExpressionComparer<CPUStatus>.Ascending(s => s.ID), SortOptimisations.ComparesImmutableValuesOnly)
.Bind(StatusList)
.Subscribe()
2019-11-21 15:04:33 +00:00
.DisposeWith(CompositeDisposable);
_percentCompleted = this.WhenAny(x => x.Compiler.ActiveCompilation)
.StartWith(default(ACompiler))
.Pairwise()
.Select(c =>
{
if (c.Current == null)
{
return Observable.Return<float>(c.Previous == null ? 0f : 1f);
}
return c.Current.PercentCompleted;
})
.Switch()
.Debounce(TimeSpan.FromMilliseconds(25))
.ToProperty(this, nameof(PercentCompleted));
// Listen for user interventions, and compile a dynamic list of all unhandled ones
var activeInterventions = this.WhenAny(x => x.Compiler.ActiveCompilation)
.SelectMany(c => c?.LogMessages ?? Observable.Empty<IStatusMessage>())
.WhereCastable<IStatusMessage, IUserIntervention>()
.ToObservableChangeSet()
.AutoRefresh(i => i.Handled)
.Filter(i => !i.Handled)
.AsObservableList();
// Find the top intervention /w no CPU ID to be marked as "global"
_ActiveGlobalUserIntervention = activeInterventions.Connect()
.Filter(x => x.CpuID == WorkQueue.UnassignedCpuId)
.QueryWhenChanged(query => query.FirstOrDefault())
.ObserveOnGuiThread()
.ToProperty(this, nameof(ActiveGlobalUserIntervention));
}
}
}