Files
mapo-core/MP.Land/Data/LMessageService.cs
2025-06-28 09:24:16 +02:00

333 lines
8.1 KiB
C#

using Blazored.LocalStorage;
using Blazored.SessionStorage;
using NLog;
using System;
using System.Threading.Tasks;
namespace MP.Land.Data
{
public class LMessageService
{
#region Public Constructors
public LMessageService(ILocalStorageService genLocalStorage, ISessionStorageService sessStore)
{
// gestione sessioni in browser
localStore = genLocalStorage;
sessionStore = sessStore;
}
#endregion Public Constructors
#region Public Events
public event Action EA_FilterUpdated;
public event Action EA_HideSearch;
public event Action EA_PageUpdated;
public event Action EA_SearchUpdated;
public event Action EA_ShowSearch;
public event Action EA_UpdateLic;
#endregion Public Events
#region Public Properties
public string CodGruppo
{
get => _groupName;
set
{
if (_groupName != value)
{
_groupName = value;
ReportFilter();
}
}
}
public string PageIcon
{
get => _pageIcon;
set
{
if (_pageIcon != value)
{
_pageIcon = value;
ReportPageUpd();
}
}
}
public string PageName
{
get => _pageName;
set
{
if (_pageName != value)
{
_pageName = value;
ReportPageUpd();
}
}
}
public string SearchVal
{
get => _searchVal;
set
{
if (_searchVal != value)
{
_searchVal = value;
ReportSearch();
}
}
}
public SelectData SelFilter
{
get => _selFilter;
set
{
if (_selFilter != value)
{
_selFilter = value;
ReportFilter();
}
}
}
public bool ShowSearch
{
get => showSearch;
set
{
if (showSearch != value)
{
showSearch = value;
if (showSearch)
{
ReportShowSearch();
}
else
{
ReportHideSearch();
}
}
}
}
public bool YearAuth
{
get => _yearAuth;
set
{
if (_yearAuth != value)
{
_yearAuth = value;
ReportYearAuth();
}
}
}
#endregion Public Properties
#region Public Methods
public void ReportYearAuth()
{
if (EA_UpdateLic != null)
{
EA_UpdateLic?.Invoke();
}
}
/// <summary>
/// Svuota localstorage (clear)
/// </summary>
/// <returns></returns>
public async Task<bool> StoreLocalClear()
{
bool answ = false;
try
{
await localStore.ClearAsync();
answ = true;
}
catch (Exception ex)
{
Log.Error($"Eccezione in StoreLocalClear{Environment.NewLine}{ex}");
}
return answ;
}
/// <summary>
/// Restituisce il valore richiesto da localstorage
/// </summary>
/// <param name="sKey">Chiave</param>
/// <returns></returns>
public async Task<string> StoreLocalGet(string sKey)
{
string answ = "";
var result = await localStore.GetItemAsync<string>(sKey);
if (result != null)
{
answ = result;
}
return answ;
}
/// <summary>
/// Scrive il valore nel localstorage
/// </summary>
/// <param name="sKey">Chiave</param>
/// <param name="sVal">Valore associato</param>
/// <returns></returns>
public async Task<bool> StoreLocalSet(string sKey, string sVal)
{
bool answ = false;
try
{
await localStore.SetItemAsStringAsync(sKey, sVal);
answ = true;
}
catch (Exception ex)
{
Log.Error($"Eccezione in StoreLocalSet{Environment.NewLine}{ex}");
}
return answ;
}
/// <summary>
/// Svuota sessionstorage (clear)
/// </summary>
/// <returns></returns>
public async Task<bool> StoreSessClear()
{
bool answ = false;
try
{
await sessionStore.ClearAsync();
answ = true;
}
catch (Exception ex)
{
Log.Error($"Eccezione in StoreLocalClear{Environment.NewLine}{ex}");
}
return answ;
}
/// <summary>
/// Restituisce il valore richiesto da sessionstorage
/// </summary>
/// <param name="sKey">Chiave</param>
/// <returns></returns>
public async Task<string> StoreSessGet(string sKey)
{
string answ = "";
var result = await sessionStore.GetItemAsync<string>(sKey);
if (result != null)
{
answ = result;
}
return answ;
}
/// <summary>
/// Scrive il valore nel sessionstorage (tab)
/// </summary>
/// <param name="sKey">Chiave</param>
/// <param name="sVal">Valore associato</param>
/// <returns></returns>
public async Task<bool> StoreSessSet(string sKey, string sVal)
{
bool answ = false;
try
{
await sessionStore.SetItemAsStringAsync(sKey, sVal);
answ = true;
}
catch (Exception ex)
{
Log.Error($"Eccezione in StoreSessSet{Environment.NewLine}{ex}");
}
return answ;
}
#endregion Public Methods
#region Protected Properties
protected string _groupName { get; set; } = "";
#endregion Protected Properties
#region Private Fields
private string _pageIcon;
private string _pageName;
private string _searchVal = "";
private SelectData _selFilter = SelectData.Init(4, 15);
private bool _yearAuth = false;
private Logger Log = LogManager.GetCurrentClassLogger();
private bool showSearch;
#endregion Private Fields
#region Private Properties
private ILocalStorageService localStore { get; set; } = null!;
private ISessionStorageService sessionStore { get; set; } = null!;
#endregion Private Properties
#region Private Methods
private void ReportFilter()
{
if (EA_FilterUpdated != null)
{
EA_FilterUpdated?.Invoke();
}
}
private void ReportHideSearch()
{
if (EA_HideSearch != null)
{
EA_HideSearch?.Invoke();
}
}
private void ReportPageUpd()
{
if (EA_PageUpdated != null)
{
EA_PageUpdated?.Invoke();
}
}
private void ReportSearch()
{
if (EA_SearchUpdated != null)
{
EA_SearchUpdated?.Invoke();
}
}
private void ReportShowSearch()
{
if (EA_ShowSearch != null)
{
EA_ShowSearch?.Invoke();
}
}
#endregion Private Methods
}
}