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(); } } /// /// Svuota localstorage (clear) /// /// public async Task StoreLocalClear() { bool answ = false; try { await localStore.ClearAsync(); answ = true; } catch (Exception ex) { Log.Error($"Eccezione in StoreLocalClear{Environment.NewLine}{ex}"); } return answ; } /// /// Restituisce il valore richiesto da localstorage /// /// Chiave /// public async Task StoreLocalGet(string sKey) { string answ = ""; var result = await localStore.GetItemAsync(sKey); if (result != null) { answ = result; } return answ; } /// /// Scrive il valore nel localstorage /// /// Chiave /// Valore associato /// public async Task 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; } /// /// Svuota sessionstorage (clear) /// /// public async Task StoreSessClear() { bool answ = false; try { await sessionStore.ClearAsync(); answ = true; } catch (Exception ex) { Log.Error($"Eccezione in StoreLocalClear{Environment.NewLine}{ex}"); } return answ; } /// /// Restituisce il valore richiesto da sessionstorage /// /// Chiave /// public async Task StoreSessGet(string sKey) { string answ = ""; var result = await sessionStore.GetItemAsync(sKey); if (result != null) { answ = result; } return answ; } /// /// Scrive il valore nel sessionstorage (tab) /// /// Chiave /// Valore associato /// public async Task 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 } }