wabbajack/Wabbajack.App/Screens/CompilationViewModel.cs
2021-11-08 06:32:51 -07:00

74 lines
2.2 KiB
C#

using System;
using System.Reactive.Disposables;
using System.Threading;
using System.Threading.Tasks;
using Avalonia.Threading;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using Wabbajack.App.Messages;
using Wabbajack.App.ViewModels;
using Wabbajack.Common;
using Wabbajack.Compiler;
using Wabbajack.RateLimiter;
namespace Wabbajack.App.Screens;
public class CompilationViewModel : ViewModelBase
{
private readonly ILogger<CompilationViewModel> _logger;
private readonly IServiceProvider _provider;
private ACompiler _compiler;
public CompilationViewModel(ILogger<CompilationViewModel> logger, IServiceProvider provider)
{
_logger = logger;
_provider = provider;
Activator = new ViewModelActivator();
MessageBus.Current.Listen<StartCompilation>()
.Subscribe(Receive)
.DisposeWith(VMDisposables);
}
[Reactive] public string StatusText { get; set; } = "";
[Reactive] public Percent StepsProgress { get; set; } = Percent.Zero;
[Reactive] public Percent StepProgress { get; set; } = Percent.Zero;
private void Receive(StartCompilation val)
{
if (val.Settings is MO2CompilerSettings mo2)
{
var compiler = _provider.GetService<MO2Compiler>()!;
compiler.Settings = mo2;
_compiler = compiler;
_compiler.OnStatusUpdate += (sender, update) =>
{
Dispatcher.UIThread.InvokeAsync(() =>
{
StatusText = update.StatusText;
StepsProgress = update.StepsProgress;
StepProgress = update.StepProgress;
});
};
}
Compile().FireAndForget();
}
public async Task Compile()
{
try
{
var result = await Task.Run(async () => await _compiler.Begin(CancellationToken.None));
}
catch (Exception ex)
{
_logger.LogError(ex, "During Compilation: {Message}", ex.Message);
StatusText = $"ERRORED: {ex.Message}";
ErrorPageViewModel.Display("During compilation", ex);
}
}
}