159 lines
4.5 KiB
C#
159 lines
4.5 KiB
C#
using Pf2eDataScrapper.Processors;
|
|
|
|
namespace Pf2eDataScrapper.Helpers;
|
|
|
|
internal class ProcessProgress
|
|
{
|
|
/// <summary>
|
|
/// Прогресс обработки обработчиков <see cref="IProcessor"/>.
|
|
/// </summary>
|
|
public ProgressItem Processors { get; } = new();
|
|
|
|
/// <summary>
|
|
/// Прогресс работы конкретного обработчика <see cref="IProcessor"/>.
|
|
/// </summary>
|
|
public ProgressItem Local { get; } = new();
|
|
|
|
/// <summary>
|
|
/// Конструктор класса <see cref="ProcessProgress"/>.
|
|
/// </summary>
|
|
public ProcessProgress()
|
|
{
|
|
Processors.Update += Update;
|
|
Local.Update += Update;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Событие обновления
|
|
/// </summary>
|
|
private void Update()
|
|
{
|
|
ProgressUpdate?.Invoke(this);
|
|
}
|
|
|
|
public void Message(string message)
|
|
{
|
|
ProgressMessage?.Invoke(this, message);
|
|
}
|
|
|
|
public delegate void ProgressUpdateDelegate(ProcessProgress sender);
|
|
public event ProgressUpdateDelegate? ProgressUpdate;
|
|
|
|
public delegate void ProgressMessageDelegate(ProcessProgress sender, string message);
|
|
public event ProgressMessageDelegate? ProgressMessage;
|
|
|
|
/// <summary>
|
|
/// Класс прогресса.
|
|
/// </summary>
|
|
public class ProgressItem
|
|
{
|
|
/// <summary>
|
|
/// Количество обрабатываемых процессов.
|
|
/// </summary>
|
|
public int Count
|
|
{
|
|
get => _count;
|
|
set
|
|
{
|
|
_count = value;
|
|
Update?.Invoke();
|
|
}
|
|
}
|
|
private int _count = 0;
|
|
|
|
/// <summary>
|
|
/// Индекс текущего обрабатываемого процесса.
|
|
/// </summary>
|
|
public int Current
|
|
{
|
|
get => _current;
|
|
set
|
|
{
|
|
_current = value;
|
|
Update?.Invoke();
|
|
}
|
|
}
|
|
private int _current = 0;
|
|
|
|
/// <summary>
|
|
/// Название текущего обрабатываемого процесса.
|
|
/// </summary>
|
|
public string Text
|
|
{
|
|
get => _text;
|
|
set
|
|
{
|
|
_text = value;
|
|
Update?.Invoke();
|
|
}
|
|
}
|
|
private string _text = "";
|
|
|
|
/// <summary>
|
|
/// Значение прогресса.
|
|
/// </summary>
|
|
public double Percent => Count > 0 ? 1.0 * Current / Count : 1.0;
|
|
|
|
/// <summary>
|
|
/// Сброс прогресса до нуля и установка нового максимального значения.
|
|
/// </summary>
|
|
/// <param name="max"></param>
|
|
public void Reset(int max = 0)
|
|
{
|
|
_count = max;
|
|
_current = 0;
|
|
_text = "";
|
|
Update?.Invoke();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Отмечает начало работы над процессом, описываемым строкой <paramref name="text"/>.
|
|
/// </summary>
|
|
/// <param name="text">Строка, описывающая процесс.</param>
|
|
public void Start(string text)
|
|
{
|
|
_text = text;
|
|
Update?.Invoke();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Отмечает конец работы над процессом и увеличивает значение прогресса.
|
|
/// </summary>
|
|
public void End()
|
|
{
|
|
_text = "";
|
|
_current++;
|
|
Update?.Invoke();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Отмечает прогресс завершённым.
|
|
/// </summary>
|
|
public void Complete()
|
|
{
|
|
_current = _count;
|
|
_text = "Готово!";
|
|
Update?.Invoke();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Получение строкового представления прогресса.
|
|
/// </summary>
|
|
/// <returns>Строковое представление прогресса.</returns>
|
|
public override string ToString()
|
|
{
|
|
return $"{Text} [{Current} / {Count}; {Percent:P}]";
|
|
}
|
|
|
|
/// <summary>
|
|
/// Делегат события обновления.
|
|
/// </summary>
|
|
public delegate void UpdateDelegate();
|
|
|
|
/// <summary>
|
|
/// Событие обновление.
|
|
/// </summary>
|
|
public event UpdateDelegate? Update;
|
|
}
|
|
}
|