Files
mapo-core/MP-TAB3/Shared/MainLayout.razor.cs
T
Samuele Locatelli c8bb1065c7 Fix naming (maybe)
2023-12-18 11:40:46 +01:00

245 lines
7.5 KiB
C#

using global::Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Routing;
using Microsoft.JSInterop;
using MP.Data.DatabaseModels;
using MP.Data.Services;
using MP_TAB3.Components;
using NLog;
namespace MP_TAB3.Shared
{
public partial class MainLayout : IDisposable
{
#region Public Methods
public void Dispose()
{
NavMan.LocationChanged -= HandleLocationChanged;
}
#endregion Public Methods
#region Protected Fields
protected string ResetClass = "btn-primary";
#endregion Protected Fields
#region Protected Properties
/// <summary>
/// Livello corrente del menu
/// </summary>
protected string CurrLevel { get; set; } = "";
/// <summary>
/// Elenco items da men per pagina corrente...
/// </summary>
protected List<LinkMenu> CurrMenuItems { get; set; } = new List<LinkMenu>();
protected bool HideMenu
{
get => NavMan.Uri.Contains("reg-new-device");
}
[Inject]
protected ListSelectDataSrv MDataService { get; set; } = null!;
[Inject]
protected MessageService MsgServ { get; set; } = null!;
[Inject]
protected SharedMemService MStor { get; set; } = null!;
[Inject]
protected NavigationManager NavMan { get; set; } = null!;
[Inject]
protected TabDataService TDataService { get; set; } = null!;
[Inject]
protected IJSRuntime JSRuntime { get; set; } = null!;
#endregion Protected Properties
#region Protected Methods
protected string bodyTipe
{
get => pageOk ? "mainBodyNoSide" : "mainBody";
}
protected bool pageOk
{
get
{
string currPage = NavMan.Uri;
return currPage.Contains("logout") || currPage.Contains("reg-new-device");
}
}
protected async Task handleBodyClick()
{
await Task.Delay(1);
if (!pageOk)
{
await checkDtDiff2Logout();
}
}
private int MatrOpr
{
get => MsgServ.MatrOpr;
}
protected int typeScadLogin { get; set; } = 0;
protected int dtScadLogin { get; set; } = 0;
protected Guid currDevGuid { get; set; } = new Guid();
protected async Task checkDtDiff2Logout()
{
TimeSpan tsDeltaAct = DateTime.Now.Subtract(MsgServ.dtLastAction);
TimeSpan tsDeltaSave = DateTime.Now.Subtract(MsgServ.dtLastSave);
switch (typeScadLogin)
{
case 0:
if (tsDeltaAct.Minutes >= dtScadLogin)
{
NavMan.NavigateTo("logout");
}
break;
case 1:
if (tsDeltaAct.Minutes >= dtScadLogin)
{
var userTkn = await TDataService.OperatoreGetRedis(MatrOpr, currDevGuid);
if (!string.IsNullOrEmpty(userTkn))
{
await MsgServ.DoLogIn(userTkn, true);
MsgServ.dtLastAction = DateTime.Now;
MsgServ.dtLastSave = DateTime.Now;
}
else
{
NavMan.NavigateTo("logout");
}
}
else
{
// se fosse oltre 1 minuto da ultimo save --> salvo!
if (tsDeltaSave.TotalMinutes > 1)
{
var userTkn = await TDataService.OperatoreGetRedis(MatrOpr, currDevGuid);
if (!string.IsNullOrEmpty(userTkn))
{
await MsgServ.DoLogIn(userTkn, true);
MsgServ.dtLastAction = DateTime.Now;
MsgServ.dtLastSave = DateTime.Now;
}
}
}
break;
}
}
/// <summary>
/// Init struttura dati
/// </summary>
/// <returns></returns>
protected override async Task OnInitializedAsync()
{
typeScadLogin = MStor.GetConfInt("TAB_TypeScadLogin");
dtScadLogin = MStor.GetConfInt("TAB_dtTimerScadLogin");
NavMan.LocationChanged += HandleLocationChanged;
currDevGuid = await MsgServ.GetCurrDevGuidLSAsync();
// verifica preliminare setup mdati
if (!MStor.MenuOk)
{
await ReloadMemStor();
}
CurrLevel = MStor.PageLevel(NavMan.Uri);
// recupero men
if (MStor.DictMenu.ContainsKey(CurrLevel))
{
CurrMenuItems = MStor.DictMenu[CurrLevel];
}
await Task.Delay(1);
}
protected bool userIsOk { get; set; } = false;
protected async Task checkIfUserOk(bool isOk)
{
await Task.Delay(1);
userIsOk = isOk;
}
protected async Task ReloadMemStor()
{
// in primis svuoto...
MStor.ClearCache();
// rileggo link
var allData = await MDataService.ListLinkAll();
MStor.SetupMenu(allData);
// fix config...
var allConf = await MDataService.ConfigGetAll();
MStor.SetConfig(allConf);
// fix MSFD...
var allMSFD = await TDataService.VMSFDGetAll();
MStor.SetMsfd(allMSFD);
// fix slave
var macSlave = await TDataService.Macchine2Slave();
MStor.SetM2S(macSlave);
// fix elenco eventi
var allEvents = await TDataService.AnagEventiGetAll();
MStor.SetEventi(allEvents);
// fix elenco stati
var allStati = await TDataService.AnaStatiGetAll();
MStor.SetStati(allStati);
// non da farsi globalmente // fix macchine var allMach = await
// MDataService.MacchineByMatrOper(0); MStor.DictMacchine = allMach.ToDictionary(x =>
// x.IdxMacchina, x => $"{x.IdxMacchina} | {x.Nome}");
// fix vocabolario
var allVoc = TDataService.VocabolarioGetAll();
MStor.SetVocab(allVoc);
// resetto il tabDServ
await TDataService.FlushCache();
// ricarica la config...
TDataService.SetupConfig();
}
#endregion Protected Methods
#region Private Fields
private static Logger Log = LogManager.GetCurrentClassLogger();
#endregion Private Fields
#region Private Methods
private void HandleLocationChanged(object? sender, LocationChangedEventArgs e)
{
// Logger.LogInformation("URL of new location: {Location}", e.Location);
CurrLevel = MStor.PageLevel(NavMan.Uri);
// recupero men
if (MStor.DictMenu.ContainsKey(CurrLevel))
{
CurrMenuItems = MStor.DictMenu[CurrLevel];
InvokeAsync(StateHasChanged).ConfigureAwait(false);
}
}
private void OnYes()
{
Console.WriteLine($"{DateTime.Now} | 'Yes' button selected.");
}
#endregion Private Methods
}
}