157 lines
4.2 KiB
C#
157 lines
4.2 KiB
C#
using Microsoft.AspNetCore.Components;
|
|
|
|
namespace EgwCoreLib.Razor
|
|
{
|
|
public partial class ProgressDisplay : IDisposable
|
|
{
|
|
#region Public Properties
|
|
|
|
[Parameter]
|
|
public double CurrVal
|
|
{
|
|
get => ActVal;
|
|
set
|
|
{
|
|
ActVal = value;
|
|
StartVal = value;
|
|
IsLoading = ActVal < MaxVal;
|
|
}
|
|
}
|
|
|
|
[Parameter]
|
|
public double ExpTimeMSec { get; set; } = 10000;
|
|
|
|
[Parameter]
|
|
public bool IsLoading { get; set; } = false;
|
|
|
|
[Parameter]
|
|
public double MaxVal { get; set; } = 100;
|
|
|
|
[Parameter]
|
|
public double NextVal { get; set; } = 100;
|
|
|
|
[Parameter]
|
|
public string Title { get; set; } = "Progress";
|
|
|
|
#endregion Public Properties
|
|
|
|
#region Public Methods
|
|
|
|
public void Dispose()
|
|
{
|
|
periodicTimer?.Dispose();
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Protected Fields
|
|
|
|
protected PeriodicTimer? periodicTimer = null;
|
|
|
|
#endregion Protected Fields
|
|
|
|
#region Protected Properties
|
|
|
|
protected string currWidth { get; set; } = "";
|
|
protected string modalCss { get; set; } = "alert alert-success";
|
|
|
|
#endregion Protected Properties
|
|
|
|
#region Protected Methods
|
|
|
|
#if false
|
|
protected override void OnInitialized()
|
|
{
|
|
RunTimer(); // fire-and-forget
|
|
}
|
|
#endif
|
|
|
|
protected override async Task OnParametersSetAsync()
|
|
{
|
|
// salvo valori aggiornamento...
|
|
if (ExpTimeMSec > 0)
|
|
{
|
|
lastUpdate = DateTime.Now;
|
|
nextUpdate = lastUpdate.AddMilliseconds(ExpTimeMSec);
|
|
await RefreshDisplay();
|
|
}
|
|
if(IsLoading)
|
|
{
|
|
periodicTimer?.Dispose();
|
|
periodicTimer = new PeriodicTimer(TimeSpan.FromMilliseconds(RefInt));
|
|
RunTimer();
|
|
}
|
|
else
|
|
{
|
|
periodicTimer = null;
|
|
}
|
|
}
|
|
|
|
protected async Task RefreshDisplay()
|
|
{
|
|
currWidth = $"{(double)ActVal / MaxVal:P0}";
|
|
DisplayMsg = $"{ActVal / MaxVal:P2}";
|
|
await InvokeAsync(StateHasChanged);
|
|
}
|
|
|
|
#endregion Protected Methods
|
|
|
|
#region Private Fields
|
|
|
|
[Parameter]
|
|
public int RefreshInterval
|
|
{
|
|
get => RefInt;
|
|
set
|
|
{
|
|
if (RefInt != value && value > 0)
|
|
{
|
|
RefInt = value;
|
|
#if false
|
|
periodicTimer?.Dispose();
|
|
periodicTimer = new PeriodicTimer(TimeSpan.FromMilliseconds(value));
|
|
RunTimer();
|
|
#endif
|
|
}
|
|
}
|
|
}
|
|
private int RefInt { get; set; } = 100;
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Private Properties
|
|
|
|
private double ActVal { get; set; } = 100;
|
|
private string DisplayMsg { get; set; } = "Loading...";
|
|
private DateTime lastUpdate { get; set; } = DateTime.Now.AddMinutes(-1);
|
|
private DateTime nextUpdate { get; set; } = DateTime.Now;
|
|
private double StartVal { get; set; } = 100;
|
|
|
|
#endregion Private Properties
|
|
|
|
#region Private Methods
|
|
|
|
private async void RunTimer()
|
|
{
|
|
while (periodicTimer!=null && await periodicTimer.WaitForNextTickAsync())
|
|
{
|
|
if (IsLoading)
|
|
{
|
|
DateTime adesso = DateTime.Now;
|
|
if (nextUpdate <= adesso)
|
|
{
|
|
nextUpdate = adesso.AddMilliseconds(RefreshInterval);
|
|
}
|
|
// calcolo delta ms...
|
|
var deltaMs = adesso.Subtract(lastUpdate).TotalMilliseconds;
|
|
var denomMs = nextUpdate.Subtract(lastUpdate).TotalMilliseconds;
|
|
// aggiorno display...
|
|
ActVal = StartVal + (NextVal - StartVal) * (deltaMs / denomMs);
|
|
await RefreshDisplay();
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion Private Methods
|
|
}
|
|
} |