wabbajack/Wabbajack/View Models/UserInterventionHandlers.cs

90 lines
2.8 KiB
C#
Raw Normal View History

2019-12-08 17:00:22 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
2019-12-08 17:00:22 +00:00
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using ReactiveUI;
using Wabbajack.Common;
2019-12-08 17:00:22 +00:00
using Wabbajack.Lib.Downloaders;
using Wabbajack.Lib.NexusApi;
using Wabbajack.Lib.WebAutomation;
2019-12-20 20:51:10 +00:00
using Wabbajack.UserInterventions;
2019-12-08 17:00:22 +00:00
namespace Wabbajack
{
public class UserInterventionHandlers
{
public MainWindowVM MainWindow { get; }
2019-12-08 17:00:22 +00:00
public UserInterventionHandlers(MainWindowVM mvm)
{
MainWindow = mvm;
2019-12-08 17:00:22 +00:00
}
private async Task WrapBrowserJob(IUserIntervention intervention, Func<WebBrowserVM, CancellationTokenSource, Task> toDo)
2019-12-08 17:00:22 +00:00
{
CancellationTokenSource cancel = new CancellationTokenSource();
var oldPane = MainWindow.ActivePane;
var vm = await WebBrowserVM.GetNew();
MainWindow.ActivePane = vm;
vm.BackCommand = ReactiveCommand.Create(() =>
2019-12-08 17:00:22 +00:00
{
cancel.Cancel();
2019-12-08 17:00:22 +00:00
MainWindow.ActivePane = oldPane;
intervention.Cancel();
2019-12-08 17:00:22 +00:00
});
try
{
await toDo(vm, cancel);
}
catch (TaskCanceledException)
{
intervention.Cancel();
}
catch (Exception ex)
{
Utils.Error(ex);
intervention.Cancel();
}
MainWindow.ActivePane = oldPane;
2019-12-08 17:00:22 +00:00
}
public async Task Handle(IUserIntervention msg)
2019-12-08 17:00:22 +00:00
{
switch (msg)
{
case RequestNexusAuthorization c:
await WrapBrowserJob(msg, async (vm, cancel) =>
{
await vm.Driver.WaitForInitialized();
var key = await NexusApiClient.SetupNexusLogin(new CefSharpWrapper(vm.Browser), m => vm.Instructions = m, cancel.Token);
c.Resume(key);
});
2019-12-08 17:00:22 +00:00
break;
case AbstractNeedsLoginDownloader.RequestSiteLogin c:
await WrapBrowserJob(msg, async (vm, cancel) =>
{
await vm.Driver.WaitForInitialized();
var data = await c.Downloader.GetAndCacheCookies(new CefSharpWrapper(vm.Browser), m => vm.Instructions = m, cancel.Token);
c.Resume(data);
});
2019-12-08 17:00:22 +00:00
break;
case ConfirmationIntervention c:
break;
2019-12-20 20:51:10 +00:00
case ShowLoginManager c:
MainWindow.NavigateTo(MainWindow.LoginManagerVM);
break;
2019-12-08 17:00:22 +00:00
default:
throw new NotImplementedException($"No handler for {msg}");
}
}
2019-12-20 20:51:10 +00:00
2019-12-08 17:00:22 +00:00
}
}