Files
mapo-core/MP.SPEC/Components/ListPARAMS.razor.cs
T
Samuele Locatelli 0ec3a4c31a Correzione Dispose:
- fix sovrapposizione eventi multipli
2022-09-15 18:47:35 +02:00

292 lines
7.3 KiB
C#

using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using MP.Data.DatabaseModels;
using MP.SPEC.Data;
using System.Diagnostics;
namespace MP.SPEC.Components
{
public partial class ListPARAMS : IDisposable
{
#region Public Properties
[Parameter]
public EventCallback<bool> PagerResetReq { get; set; }
[Parameter]
public SelectFluxParams SelFilter { get; set; } = null!;
#if false
{
get => _selFilter;
set
{
if (!_selFilter.Equals(value))
{
_selFilter = value;
}
}
}
#endif
[Parameter]
public EventCallback<int> TotRecordChanged { get; set; }
#endregion Public Properties
#region Public Methods
public string checkSelect(string IdxMacchina)
{
string answ = "";
if (currRecord != null)
{
try
{
answ = (currRecord.IdxMacchina == IdxMacchina) ? "table-info" : "";
}
catch
{ }
}
return answ;
}
public void Dispose()
{
aTimer.Elapsed -= ElapsedTimer;
MessageService.EA_PageUpdated -= MessageService_EA_PageUpdated;
MessageService.EA_SearchUpdated -= OnSeachUpdated;
aTimer.Stop();
aTimer.Dispose();
}
public void ElapsedTimer(object? source, System.Timers.ElapsedEventArgs e)
{
if (!isLoading && LiveUpdate)
{
aTimer.Stop();
// inizio misura esecuzione
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
var pUpd = Task.Run(async () =>
{
await Task.Delay(1);
await InvokeAsync(() => reloadData(true));
});
pUpd.Wait();
// misuro tempo esecuzione
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
int deltaTime = tOutPeriod - (int)ts.TotalMilliseconds;
aTimer.Interval = deltaTime > 100 ? deltaTime : 100;
aTimer.Start();
}
}
public async Task reloadData(bool setChanged)
{
isLoading = true;
SearchRecords = await MDService.FluxLogGetLastFilt(SelMacchina, SelFlux, MaxRecord);
totalCount = SearchRecords.Count;
ListRecords = SearchRecords.Skip(numRecord * (currPage - 1)).Take(numRecord).ToList();
await Task.Delay(1);
if (setChanged)
{
await InvokeAsync(() => StateHasChanged());
}
isLoading = false;
}
public void StartTimer()
{
//int.TryParse(Configuration["ReloadStatusTimer"], out tOutPeriod);
aTimer = new System.Timers.Timer(tOutPeriod);
aTimer.Elapsed += ElapsedTimer;
aTimer.Enabled = true;
//aTimer.AutoReset = true;
aTimer.Start();
}
#endregion Public Methods
#region Protected Properties
[Inject]
protected IJSRuntime JSRuntime { get; set; } = null!;
[Inject]
protected MpDataService MDService { get; set; } = null!;
[Inject]
protected MessageService MessageService { get; set; } = null!;
#endregion Protected Properties
#region Protected Methods
protected override async Task OnInitializedAsync()
{
MessageService.EA_PageUpdated += MessageService_EA_PageUpdated;
MessageService.EA_SearchUpdated += OnSeachUpdated;
StartTimer();
await reloadData(true);
}
protected async void OnSeachUpdated()
{
await InvokeAsync(() =>
{
PagerResetReq.InvokeAsync(false);
//currPage = 1;
Task task = UpdateData();
StateHasChanged();
});
}
protected async Task UpdateData()
{
currRecord = null;
await reloadData(true);
}
#endregion Protected Methods
#region Private Fields
private static System.Timers.Timer aTimer = null!;
private int _totalCount = 0;
private FluxLog? currRecord = null;
private List<FluxLog>? ListRecords;
private List<FluxLog>? SearchRecords;
#endregion Private Fields
#region Private Properties
private int currPage
{
get => MessageService.currPage;
set => MessageService.currPage = value;
}
private bool isLoading { get; set; } = false;
private bool LiveUpdate
{
get => SelFilter.LiveUpdate;
}
private int MaxRecord
{
get => SelFilter.MaxRecord;
}
#if false
[Parameter]
public bool LiveUpdate
{
get => _liveUpdate;
set
{
_liveUpdate = value;
// var pUpd = Task.Run(async () => await reloadData(false)); pUpd.Wait();
}
}
private bool _liveUpdate { get; set; }
#endif
#if false
[Parameter]
public int MaxRecord
{
get => _maxRecord;
set
{
if (_maxRecord != value)
{
_maxRecord = value;
var pUpd = Task.Run(async () => await reloadData(true));
pUpd.Wait();
}
}
}
#endif
private int numRecord
{
get => MessageService.numRecord;
set => MessageService.numRecord = value;
}
private string SelFlux
{
get => SelFilter.CodFlux;
}
private string SelMacchina
{
get => SelFilter.IdxMacchina;
}
#if false
[Parameter]
public string SelFlux
{
get => _selFlux;
set
{
if (_selFlux != value)
{
_selFlux = value;
//var pUpd = Task.Run(async () => await reloadData(false));
//pUpd.Wait();
}
}
}
[Parameter]
public string SelMacchina
{
get => _selMacchina;
set
{
if (_selMacchina != value)
{
_selMacchina = value;
//var pUpd = Task.Run(async () => await reloadData(false));
//pUpd.Wait();
}
}
}
#endif
private int totalCount
{
get => _totalCount;
set
{
if (_totalCount != value)
{
_totalCount = value;
TotRecordChanged.InvokeAsync(value);
}
}
}
private int tOutPeriod { get; set; } = 5000;
#endregion Private Properties
#region Private Methods
private async void MessageService_EA_PageUpdated()
{
await reloadData(true);
}
#endregion Private Methods
}
}