wabbajack/Wabbajack.Common/StatusUpdateTracker.cs

72 lines
2.4 KiB
C#
Raw Normal View History

2019-11-17 04:16:42 +00:00
using System;
using System.Reactive.Subjects;
2019-12-04 04:12:08 +00:00
using Wabbajack.Common.StatusFeed;
2019-11-17 04:16:42 +00:00
namespace Wabbajack.Common
{
public class StatusUpdateTracker
{
private Subject<string> _stepName = new Subject<string>();
2020-11-03 14:45:08 +00:00
public IObservable<string> StepName => _stepName.Debounce(TimeSpan.FromMilliseconds(100));
2019-11-17 04:16:42 +00:00
private Subject<int> _step = new Subject<int>();
public IObservable<int> Step => _step;
private Subject<int> _maxStep = new Subject<int>();
public IObservable<int> MaxStep => _maxStep;
2020-02-08 04:35:08 +00:00
private Subject<Percent> _progress = new Subject<Percent>();
public IObservable<Percent> Progress => _progress;
2019-11-17 04:16:42 +00:00
private int _internalCurrentStep;
private int _internalMaxStep;
2020-11-03 14:45:08 +00:00
private string _currentStepName = "";
2019-11-17 04:16:42 +00:00
public StatusUpdateTracker(int maxStep)
{
_internalMaxStep = maxStep;
}
public void Reset()
{
_maxStep.OnNext(_internalMaxStep);
}
public void NextStep(string name)
{
2020-11-03 14:45:08 +00:00
_currentStepName = name;
2019-11-17 04:16:42 +00:00
_internalCurrentStep += 1;
2019-11-24 23:03:36 +00:00
Utils.Log(name);
2019-11-17 04:16:42 +00:00
_step.OnNext(_internalCurrentStep);
2020-11-03 14:45:08 +00:00
_stepName.OnNext($"({_internalCurrentStep}/{_internalMaxStep}) {_currentStepName}");
2020-02-08 04:35:08 +00:00
MakeUpdate(Percent.Zero);
2019-11-24 23:03:36 +00:00
}
/// <summary>
/// Converts a percent that's within the scope of a single step
/// to the overall percent when all steps are considered
/// </summary>
/// <param name="singleStepPercent">Percent progress of the current single step</param>
/// <returns>Overall progress in relation to all steps</returns>
private Percent OverAllStatus(Percent singleStepPercent)
2019-11-24 23:03:36 +00:00
{
var per_step = 1.0f / _internalMaxStep;
var macro = _internalCurrentStep * per_step;
return Percent.FactoryPutInRange(macro + (per_step * singleStepPercent.Value));
2019-11-17 04:16:42 +00:00
}
2020-02-08 04:35:08 +00:00
public void MakeUpdate(Percent progress)
2019-11-17 04:16:42 +00:00
{
// Need to convert from single step progress to overall progress for output subject
_progress.OnNext(OverAllStatus(progress));
2019-11-17 04:16:42 +00:00
}
public void MakeUpdate(int max, int curr)
{
2020-02-08 04:35:08 +00:00
MakeUpdate(Percent.FactoryPutInRange(curr, max == 0 ? 1 : max));
2020-11-03 14:45:08 +00:00
_stepName.OnNext($"({_internalCurrentStep}/{_internalMaxStep}) {_currentStepName}, {curr} of {max}");
2019-11-17 04:16:42 +00:00
}
}
}