wabbajack/Wabbajack/View Models/Compilers/CompilerVM.cs
Justin Swanson b31f336ddb Lots of compiler backend changes to support multiple
CompilerVM is lightweight now, with sub view models per mod manager mode
2019-11-14 20:25:09 -06:00

83 lines
3.0 KiB
C#

using Microsoft.WindowsAPICodePack.Dialogs;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using System;
using System.IO;
using System.Linq;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
using Wabbajack.Common;
using Wabbajack.Lib;
namespace Wabbajack
{
public class CompilerVM : ViewModel
{
public MainWindowVM MWVM { get; }
[Reactive]
public bool Compiling { get; set; }
private readonly ObservableAsPropertyHelper<BitmapImage> _Image;
public BitmapImage Image => _Image.Value;
[Reactive]
public ModManager SelectedCompilerType { get; set; }
private readonly ObservableAsPropertyHelper<ISubCompilerVM> _Compiler;
public ISubCompilerVM Compiler => _Compiler.Value;
private readonly ObservableAsPropertyHelper<ModlistSettingsEditorVM> _CurrentModlistSettings;
public ModlistSettingsEditorVM CurrentModlistSettings => _CurrentModlistSettings.Value;
public CompilerVM(MainWindowVM mainWindowVM)
{
this.MWVM = mainWindowVM;
// Load settings
CompilerSettings settings = this.MWVM.Settings.Compiler;
this.SelectedCompilerType = settings.LastCompiledModManager;
this.MWVM.Settings.SaveSignal
.Subscribe(_ =>
{
settings.LastCompiledModManager = this.SelectedCompilerType;
})
.DisposeWith(this.CompositeDisposable);
// Swap to proper sub VM based on selected type
this._Compiler = this.WhenAny(x => x.SelectedCompilerType)
.Select<ModManager, ISubCompilerVM>(type =>
{
switch (type)
{
case ModManager.MO2:
return new MO2CompilerVM(this);
case ModManager.Vortex:
return new VortexCompilerVM();
default:
return null;
}
})
.ToProperty(this, nameof(this.Compiler));
// Let sub VM determine what settings we're displaying and when
this._CurrentModlistSettings = this.WhenAny(x => x.Compiler.ModlistSettings)
.ToProperty(this, nameof(this.CurrentModlistSettings));
this._Image = this.WhenAny(x => x.CurrentModlistSettings.ImagePath.TargetPath)
.Select(path =>
{
if (string.IsNullOrWhiteSpace(path)) return UIUtils.BitmapImageFromResource("Wabbajack.Resources.Banner_Dark.png");
if (UIUtils.TryGetBitmapImageFromFile(path, out var image))
{
return image;
}
return UIUtils.BitmapImageFromResource("Wabbajack.Resources.none.png");
})
.ToProperty(this, nameof(this.Image));
}
}
}