wabbajack/Wabbajack/View Models/MainWindowVM.cs

99 lines
3.6 KiB
C#
Raw Normal View History

using DynamicData;
using DynamicData.Binding;
using ReactiveUI;
2019-11-02 23:23:11 +00:00
using ReactiveUI.Fody.Helpers;
2019-10-22 02:12:55 +00:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
2019-10-22 02:12:55 +00:00
using System.Linq;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Reactive.Subjects;
2019-10-22 02:12:55 +00:00
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Wabbajack.Common;
2019-10-22 02:12:55 +00:00
using Wabbajack.Lib;
namespace Wabbajack
{
/// <summary>
/// Main View Model for the application.
/// Keeps track of which sub view is being shown in the window, and has some singleton wiring like WorkQueue and Logging.
/// </summary>
2019-10-22 02:12:55 +00:00
public class MainWindowVM : ViewModel
{
public MainWindow MainWindow { get; }
2019-10-22 02:12:55 +00:00
public MainSettings Settings { get; }
private readonly ObservableAsPropertyHelper<ViewModel> _ActivePane;
public ViewModel ActivePane => _ActivePane.Value;
2019-10-22 02:12:55 +00:00
public ObservableCollectionExtended<CPUStatus> StatusList { get; } = new ObservableCollectionExtended<CPUStatus>();
public ObservableCollectionExtended<string> Log { get; } = new ObservableCollectionExtended<string>();
2019-11-02 23:23:11 +00:00
[Reactive]
public RunMode Mode { get; set; }
private readonly Lazy<CompilerVM> _Compiler;
private readonly Lazy<InstallerVM> _Installer;
public MainWindowVM(RunMode mode, string source, MainWindow mainWindow, MainSettings settings)
2019-10-22 02:12:55 +00:00
{
this.Mode = mode;
this.MainWindow = mainWindow;
this.Settings = settings;
this._Installer = new Lazy<InstallerVM>(() => new InstallerVM(this, source));
this._Compiler = new Lazy<CompilerVM>(() => new CompilerVM(this));
// Set up logging
Utils.LogMessages
.ObserveOn(RxApp.TaskpoolScheduler)
.ToObservableChangeSet()
.Buffer(TimeSpan.FromMilliseconds(250), RxApp.TaskpoolScheduler)
.Where(l => l.Count > 0)
.FlattenBufferResult()
.Top(5000)
.ObserveOn(RxApp.MainThreadScheduler)
.Bind(this.Log)
.Subscribe()
.DisposeWith(this.CompositeDisposable);
2019-11-17 04:16:42 +00:00
// Wire mode to drive the active pane.
// Note: This is currently made into a derivative property driven by mode,
// but it can be easily changed into a normal property that can be set from anywhere if needed
this._ActivePane = this.WhenAny(x => x.Mode)
.Select<RunMode, ViewModel>(m =>
{
switch (m)
{
case RunMode.Compile:
return this._Compiler.Value;
case RunMode.Install:
return this._Installer.Value;
default:
return default;
}
})
.ToProperty(this, nameof(this.ActivePane));
2019-11-17 06:02:09 +00:00
// Compile progress updates and populate ObservableCollection
2019-11-17 04:16:42 +00:00
/*
WorkQueue.Status
.ObserveOn(RxApp.TaskpoolScheduler)
.ToObservableChangeSet(x => x.ID)
.Batch(TimeSpan.FromMilliseconds(250), RxApp.TaskpoolScheduler)
.EnsureUniqueChanges()
.ObserveOn(RxApp.MainThreadScheduler)
.Sort(SortExpressionComparer<CPUStatus>.Ascending(s => s.ID), SortOptimisations.ComparesImmutableValuesOnly)
.Bind(this.StatusList)
.Subscribe()
2019-11-17 04:16:42 +00:00
.DisposeWith(this.CompositeDisposable);*/
}
2019-10-22 02:12:55 +00:00
}
}