SPT-AKI-Installer/SPTInstaller/Models/InstallerTaskBase.cs

178 lines
4.6 KiB
C#
Raw Permalink Normal View History

using ReactiveUI;
2023-05-14 22:35:06 -04:00
using Serilog;
2023-05-11 23:11:39 -04:00
using SPTInstaller.Interfaces;
using System.Threading.Tasks;
namespace SPTInstaller.Models;
public abstract class InstallerTaskBase : ReactiveObject, IProgressableTask
2023-05-11 23:11:39 -04:00
{
private string _id;
public string Id
2023-05-11 23:11:39 -04:00
{
get => _id;
private set => this.RaiseAndSetIfChanged(ref _id, value);
}
2023-05-11 23:11:39 -04:00
private string _name;
public string Name
{
get => _name;
private set => this.RaiseAndSetIfChanged(ref _name, value);
}
2023-05-11 23:11:39 -04:00
private bool _isComleted;
public bool IsCompleted
{
get => _isComleted;
private set => this.RaiseAndSetIfChanged(ref _isComleted, value);
}
2023-05-11 23:11:39 -04:00
private bool _hasErrors;
public bool HasErrors
{
get => _hasErrors;
private set => this.RaiseAndSetIfChanged(ref _hasErrors, value);
}
2023-05-11 23:11:39 -04:00
private bool _isRunning;
public bool IsRunning
{
get => _isRunning;
private set => this.RaiseAndSetIfChanged(ref _isRunning, value);
}
2023-05-11 23:11:39 -04:00
private int _progress;
public int Progress
{
get => _progress;
private set => this.RaiseAndSetIfChanged(ref _progress, value);
}
2023-05-11 23:11:39 -04:00
private bool _showProgress;
public bool ShowProgress
{
get => _showProgress;
private set => this.RaiseAndSetIfChanged(ref _showProgress, value);
}
2023-05-11 23:11:39 -04:00
private bool _indeterminateProgress;
public bool IndeterminateProgress
{
get => _indeterminateProgress;
private set => this.RaiseAndSetIfChanged(ref _indeterminateProgress, value);
}
2023-05-14 22:35:06 -04:00
private string _statusMessage;
public string StatusMessage
{
get => _statusMessage;
private set => this.RaiseAndSetIfChanged(ref _statusMessage, value);
}
2023-05-11 23:11:39 -04:00
private string _statusDetails;
public string StatusDetails
{
get => _statusDetails;
private set => this.RaiseAndSetIfChanged(ref _statusDetails, value);
}
2023-05-11 23:11:39 -04:00
public enum ProgressStyle
{
Hidden = 0,
Shown,
Indeterminate,
}
2023-05-11 23:11:39 -04:00
/// <summary>
/// Update the status details of the task
/// </summary>
/// <param name="message">The main message to display. Not updated if null</param>
/// <param name="details">The details of the task. Not updated if null</param>
/// <param name="progress">Progress of the task. Overrides progressStyle if a non-null value is supplied</param>
/// <param name="progressStyle">The style of the progress bar</param>
public void SetStatus(string? message, string? details, int? progress = null, ProgressStyle? progressStyle = null, bool noLog = false)
{
if(message != null && message != StatusMessage)
2023-05-11 23:11:39 -04:00
{
if (!noLog && !string.IsNullOrWhiteSpace(message))
2023-05-14 22:35:06 -04:00
{
Log.Information($" <===> {message} <===>");
2023-05-14 22:35:06 -04:00
}
StatusMessage = message;
}
2023-05-14 22:35:06 -04:00
if(details != null && details != StatusDetails)
{
if (!noLog && !string.IsNullOrWhiteSpace(details))
2023-05-14 22:35:06 -04:00
{
Log.Information(details);
2023-05-14 22:35:06 -04:00
}
2023-05-11 23:11:39 -04:00
StatusDetails = details;
2023-05-11 23:11:39 -04:00
}
if (progressStyle != null)
2023-05-11 23:11:39 -04:00
{
switch (progressStyle)
{
case ProgressStyle.Hidden:
ShowProgress = false;
IndeterminateProgress = false;
break;
case ProgressStyle.Shown:
ShowProgress = true;
IndeterminateProgress = false;
break;
case ProgressStyle.Indeterminate:
ShowProgress = true;
IndeterminateProgress = true;
break;
}
2023-05-11 23:11:39 -04:00
}
if (progress != null)
2023-05-11 23:11:39 -04:00
{
ShowProgress = true;
IndeterminateProgress = false;
Progress = progress.Value;
}
}
2023-05-11 23:11:39 -04:00
public InstallerTaskBase(string name)
{
Name = name;
Id = Guid.NewGuid().ToString();
}
2023-05-11 23:11:39 -04:00
/// <summary>
/// A method for the install controller to call. Do not use this within your task
/// </summary>
/// <returns></returns>
public async Task<IResult> RunAsync()
{
IsRunning = true;
2023-05-11 23:11:39 -04:00
var result = await TaskOperation();
2023-05-14 22:35:06 -04:00
IsRunning = false;
2023-05-11 23:11:39 -04:00
if (!result.Succeeded)
{
HasErrors = true;
2023-05-11 23:11:39 -04:00
return result;
}
IsCompleted = true;
return result;
2023-05-11 23:11:39 -04:00
}
/// <summary>
/// The task you want to run
/// </summary>
/// <returns></returns>
public abstract Task<IResult> TaskOperation();
}