wabbajack/Wabbajack.App.Wpf/View Models/MainWindowVM.cs

252 lines
9.3 KiB
C#
Raw Normal View History

2021-12-27 05:18:52 +00:00
using DynamicData.Binding;
2021-12-26 21:56:44 +00:00
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using System;
using System.Collections.Generic;
2021-12-26 21:56:44 +00:00
using System.Diagnostics;
2021-12-30 23:55:41 +00:00
using System.Linq;
using System.Reactive.Disposables;
2021-12-26 21:56:44 +00:00
using System.Reactive.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
2021-12-28 00:53:14 +00:00
using Microsoft.Extensions.DependencyInjection;
2021-12-27 23:15:30 +00:00
using Microsoft.Extensions.Logging;
2021-12-26 21:56:44 +00:00
using Wabbajack.Common;
2021-12-28 00:53:14 +00:00
using Wabbajack.Downloaders.GameFile;
2021-12-30 00:15:37 +00:00
using Wabbajack;
using Wabbajack.Interventions;
using Wabbajack.LoginManagers;
using Wabbajack.Messages;
2021-12-30 23:55:41 +00:00
using Wabbajack.Models;
2021-12-27 23:15:30 +00:00
using Wabbajack.Networking.WabbajackClientApi;
2021-12-27 05:18:52 +00:00
using Wabbajack.Paths;
using Wabbajack.UserIntervention;
2021-12-27 05:18:52 +00:00
using Wabbajack.View_Models;
2021-12-26 21:56:44 +00:00
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>
public class MainWindowVM : ViewModel
{
public MainWindow MainWindow { get; }
public MainSettings Settings { get; }
[Reactive]
public ViewModel ActivePane { get; private set; }
public ObservableCollectionExtended<IStatusMessage> Log { get; } = new ObservableCollectionExtended<IStatusMessage>();
2022-01-05 05:52:37 +00:00
public readonly CompilerVM Compiler;
2021-12-30 23:55:41 +00:00
public readonly InstallerVM Installer;
2022-01-05 05:52:37 +00:00
public readonly SettingsVM SettingsPane;
public readonly ModListGalleryVM Gallery;
2021-12-26 21:56:44 +00:00
public readonly ModeSelectionVM ModeSelectionVM;
public readonly WebBrowserVM WebBrowserVM;
2021-12-26 21:56:44 +00:00
public readonly Lazy<ModListContentsVM> ModListContentsVM;
public readonly UserInterventionHandlers UserInterventionHandlers;
2021-12-27 23:15:30 +00:00
private readonly Client _wjClient;
private readonly ILogger<MainWindowVM> _logger;
2021-12-30 23:55:41 +00:00
private readonly ResourceMonitor _resourceMonitor;
2021-12-26 21:56:44 +00:00
private List<ViewModel> PreviousPanes = new();
private readonly IServiceProvider _serviceProvider;
2021-12-26 21:56:44 +00:00
public ICommand CopyVersionCommand { get; }
public ICommand ShowLoginManagerVM { get; }
public ICommand OpenSettingsCommand { get; }
public string VersionDisplay { get; }
2021-12-30 23:55:41 +00:00
[Reactive]
public string ResourceStatus { get; set; }
2021-12-26 21:56:44 +00:00
[Reactive]
public bool UpdateAvailable { get; private set; }
2021-12-28 20:00:17 +00:00
public MainWindowVM(ILogger<MainWindowVM> logger, MainSettings settings, Client wjClient,
2021-12-30 23:55:41 +00:00
IServiceProvider serviceProvider, ModeSelectionVM modeSelectionVM, ModListGalleryVM modListGalleryVM, ResourceMonitor resourceMonitor,
2022-01-05 05:52:37 +00:00
InstallerVM installer, CompilerVM compilerVM, SettingsVM settingsVM, WebBrowserVM webBrowserVM)
2021-12-26 21:56:44 +00:00
{
2021-12-27 23:15:30 +00:00
_logger = logger;
_wjClient = wjClient;
2021-12-30 23:55:41 +00:00
_resourceMonitor = resourceMonitor;
_serviceProvider = serviceProvider;
2021-12-26 21:56:44 +00:00
ConverterRegistration.Register();
Settings = settings;
2021-12-30 23:55:41 +00:00
Installer = installer;
2022-01-05 05:52:37 +00:00
Compiler = compilerVM;
SettingsPane = settingsVM;
Gallery = modListGalleryVM;
ModeSelectionVM = modeSelectionVM;
WebBrowserVM = webBrowserVM;
2021-12-28 23:18:55 +00:00
ModListContentsVM = new Lazy<ModListContentsVM>(() => new ModListContentsVM(serviceProvider.GetRequiredService<ILogger<ModListContentsVM>>(), this));
UserInterventionHandlers = new UserInterventionHandlers(serviceProvider.GetRequiredService<ILogger<UserInterventionHandlers>>(), this);
2021-12-26 21:56:44 +00:00
MessageBus.Current.Listen<NavigateToGlobal>()
.Subscribe(m => HandleNavigateTo(m.Screen))
.DisposeWith(CompositeDisposable);
MessageBus.Current.Listen<NavigateTo>()
.Subscribe(m => HandleNavigateTo(m.ViewModel))
.DisposeWith(CompositeDisposable);
MessageBus.Current.Listen<NavigateBack>()
.Subscribe(HandleNavigateBack)
.DisposeWith(CompositeDisposable);
MessageBus.Current.Listen<NexusLogin>()
.Subscribe(HandleLogin)
.DisposeWith(CompositeDisposable);
MessageBus.Current.Listen<LoversLabLogin>()
.Subscribe(HandleLogin)
.DisposeWith(CompositeDisposable);
MessageBus.Current.Listen<VectorPlexusLogin>()
.Subscribe(HandleLogin)
.DisposeWith(CompositeDisposable);
2021-12-30 23:55:41 +00:00
_resourceMonitor.Updates
.Select(r => string.Join(", ", r.Where(r => r.Throughput > 0)
.Select(s => $"{s.Name} - {s.Throughput.ToFileSizeString()}/sec")))
.BindToStrict(this, view => view.ResourceStatus);
2021-12-26 21:56:44 +00:00
if (IsStartingFromModlist(out var path))
{
LoadModlistForInstalling.Send(path, null);
NavigateToGlobal.Send(NavigateToGlobal.ScreenType.Installer);
2021-12-26 21:56:44 +00:00
}
else
{
// Start on mode selection
NavigateToGlobal.Send(NavigateToGlobal.ScreenType.ModeSelectionView);
2021-12-26 21:56:44 +00:00
}
try
{
var assembly = Assembly.GetExecutingAssembly();
var fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
Consts.CurrentMinimumWabbajackVersion = Version.Parse(fvi.FileVersion);
VersionDisplay = $"v{fvi.FileVersion}";
2021-12-27 23:15:30 +00:00
_logger.LogInformation("Wabbajack Version: {FileVersion}", fvi.FileVersion);
2021-12-26 21:56:44 +00:00
2021-12-27 23:15:30 +00:00
Task.Run(() => _wjClient.SendMetric("started_wabbajack", fvi.FileVersion)).FireAndForget();
Task.Run(() => _wjClient.SendMetric("started_sha", ThisAssembly.Git.Sha));
2021-12-26 21:56:44 +00:00
}
catch (Exception ex)
{
2021-12-27 23:15:30 +00:00
_logger.LogError(ex, "During App configuration");
2021-12-26 21:56:44 +00:00
VersionDisplay = "ERROR";
}
CopyVersionCommand = ReactiveCommand.Create(() =>
{
Clipboard.SetText($"Wabbajack {VersionDisplay}\n{ThisAssembly.Git.Sha}");
});
OpenSettingsCommand = ReactiveCommand.Create(
canExecute: this.WhenAny(x => x.ActivePane)
2022-01-05 05:52:37 +00:00
.Select(active => !object.ReferenceEquals(active, SettingsPane)),
execute: () => NavigateToGlobal.Send(NavigateToGlobal.ScreenType.Settings));
}
private void HandleNavigateTo(ViewModel objViewModel)
{
ActivePane = objViewModel;
}
private void HandleLogin(NexusLogin nexusLogin)
{
var handler = _serviceProvider.GetRequiredService<NexusLoginHandler>();
handler.Configure(ActivePane, nexusLogin);
handler.Begin().FireAndForget();
}
private void HandleLogin(LoversLabLogin loversLabLogin)
{
var handler = _serviceProvider.GetRequiredService<LoversLabLoginHandler>();
handler.Configure(ActivePane, loversLabLogin);
handler.Begin().FireAndForget();
}
private void HandleLogin(VectorPlexusLogin vectorPlexusLogin)
{
var handler = _serviceProvider.GetRequiredService<VectorPlexusLoginHandler>();
handler.Configure(ActivePane, vectorPlexusLogin);
handler.Begin().FireAndForget();
}
private void HandleNavigateBack(NavigateBack navigateBack)
{
ActivePane = PreviousPanes.Last();
PreviousPanes.RemoveAt(PreviousPanes.Count - 1);
}
private void HandleNavigateTo(NavigateToGlobal.ScreenType s)
{
if (s is NavigateToGlobal.ScreenType.Settings)
PreviousPanes.Add(ActivePane);
ActivePane = s switch
{
NavigateToGlobal.ScreenType.ModeSelectionView => ModeSelectionVM,
NavigateToGlobal.ScreenType.ModListGallery => Gallery,
2021-12-30 23:55:41 +00:00
NavigateToGlobal.ScreenType.Installer => Installer,
2022-01-05 05:52:37 +00:00
NavigateToGlobal.ScreenType.Compiler => Compiler,
NavigateToGlobal.ScreenType.Settings => SettingsPane,
_ => ActivePane
};
2021-12-26 21:56:44 +00:00
}
2021-12-26 21:56:44 +00:00
private static bool IsStartingFromModlist(out AbsolutePath modlistPath)
{
2021-12-28 00:53:14 +00:00
/* TODO
2021-12-26 21:56:44 +00:00
if (CLIArguments.InstallPath == null)
{
modlistPath = default;
return false;
}
modlistPath = (AbsolutePath)CLIArguments.InstallPath;
return true;
2021-12-28 00:53:14 +00:00
*/
modlistPath = default;
2021-12-28 00:53:14 +00:00
return false;
2021-12-26 21:56:44 +00:00
}
/*
2021-12-26 21:56:44 +00:00
public void NavigateTo(ViewModel vm)
{
ActivePane = vm;
}*/
2021-12-26 21:56:44 +00:00
/*
2021-12-26 21:56:44 +00:00
public void NavigateTo<T>(T vm)
where T : ViewModel, IBackNavigatingVM
{
vm.NavigateBackTarget = ActivePane;
ActivePane = vm;
}*/
2021-12-26 21:56:44 +00:00
public async Task ShutdownApplication()
{
2021-12-28 20:00:17 +00:00
/*
2021-12-26 21:56:44 +00:00
Dispose();
Settings.PosX = MainWindow.Left;
Settings.PosY = MainWindow.Top;
Settings.Width = MainWindow.Width;
Settings.Height = MainWindow.Height;
await MainSettings.SaveSettings(Settings);
Application.Current.Shutdown();
2021-12-28 20:00:17 +00:00
*/
2021-12-26 21:56:44 +00:00
}
}
}