using Blazored.LocalStorage; using Blazored.SessionStorage; using GPW.CORE.Data.DbModels; using GPW.CORE.Data.DTO; using StackExchange.Redis; using System.Reflection.Metadata; namespace GPW.CORE.Data.Services { public class MessageService { #region Public Fields public int orarioDip = 0; #endregion Public Fields #region Public Constructors public MessageService(ILocalStorageService genLocalStorage, ISessionStorageService sessStore) { // gestione sessioni in browser localStore = genLocalStorage; sessionStore = sessStore; } #endregion Public Constructors #region Public Events public event Action EA_HideSearch = null!; public event Action EA_LangSel = null!; public event Action EA_MenuUpdated = null!; public event Action EA_PageUpdated = null!; public event Action EA_SearchUpdated = null!; public event Action EA_ShowSearch = null!; public event Action EA_UserUpdated = null!; #endregion Public Events #region Public Properties public RegAttivitaModel? clonedRA { get; set; } = null; /// /// Calcola HASH del codice impiego = payload utente /// /// public string HashDip { get { DataValidator cDV = new DataValidator(_rigaDip); string hash = cDV.HashDip; return hash; } } public int IdxDipendente { get { int answ = -1; if (_rigaDip != null) { answ = _rigaDip.IdxDipendente; } return answ; } } public bool IsActive { get { bool answ = false; if (_rigaDip != null && _rigaDip.Attivo != null) { answ = (bool)_rigaDip.Attivo; } return answ; } } public List ListMenu { get => _listMenu; set { _listMenu = value; if (EA_MenuUpdated != null) { EA_MenuUpdated?.Invoke(); } } } 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 bool PayloadOk { get; set; } = false; public RegAttivitaModel? recordRA { get; set; } = null; public DipendentiModel? RigaDip { get => _rigaDip; set { if (_rigaDip != value) { _rigaDip = value; if (EA_UserUpdated != null) { EA_UserUpdated?.Invoke(); } } } } public string SearchVal { get => _searchVal; set { if (_searchVal != value) { _searchVal = value; if (EA_SearchUpdated != null) { EA_SearchUpdated?.Invoke(); } } } } public int selWeekNum { get; set; } = 0; public int selYear { get; set; } = DateTime.Today.Year; public bool ShowSearch { get => showSearch; set { if (showSearch != value) { showSearch = value; if (showSearch) { if (EA_ShowSearch != null) { EA_ShowSearch?.Invoke(); } } else { if (EA_HideSearch != null) { EA_HideSearch?.Invoke(); } } } } } public DateTime targetDate { get; set; } = DateTime.Today; public string UserLang { get => userLang; set { if (userLang != value) { userLang = value; if (EA_LangSel != null) { EA_LangSel?.Invoke(); } } } } public string UserName { get { string answ = ""; if (_rigaDip != null) { answ = _rigaDip.Utente; } return answ; } } #endregion Public Properties #region Public Methods /// /// Num record x la pagina corrente /// public async Task NumRowGridGet(string gridName) { var answ = await localStore.GetItemAsync($"{gridName}_nRow"); answ = answ > 0 ? answ : 10; return answ; } /// /// Imposta num record x la pagina corrente /// public async Task NumRowGridSet(string gridName, int newVal) { await localStore.SetItemAsync($"{gridName}_nRow", newVal); } /// /// Lingua richiesta da utente /// public async Task UserLangGet() { var answ = await localStore.GetItemAsync("UserLang"); answ = string.IsNullOrEmpty(answ) ? "IT" : answ; return answ; } /// /// Salva lingua utente /// public async Task UserLangSet(string userLangl) { await localStore.SetItemAsync("UserLang", userLang); } /// /// Preferenza utente salvata in local storage /// public async Task UserPrefGet(string userPref) { string rawData = await localStore.GetItemAsStringAsync(userPref) ?? ""; return rawData; } /// /// Preferenza utente salvata in local storage /// public async ValueTask UserPrefGet(string userPref) { var sData = await localStore.GetItemAsync(userPref); if (string.IsNullOrEmpty(sData)) { return default(T); } else { return (T)Convert.ChangeType(sData, typeof(T)); } } /// /// Imposta pref utente /// public async Task UserPrefSet(string userPref, string newVal) { await localStore.SetItemAsync(userPref, newVal); } #endregion Public Methods #region Protected Properties protected List _listMenu { get; set; } = new List(); protected ILocalStorageService localStore { get; set; } = null!; protected ISessionStorageService sessionStore { get; set; } = null!; #endregion Protected Properties #region Private Fields private string _pageIcon = ""; private string _pageName = ""; private DipendentiModel? _rigaDip; private string _searchVal = ""; private bool showSearch; #endregion Private Fields #region Private Properties private string userLang { get; set; } = "IT"; #endregion Private Properties #region Private Methods private void ReportPageUpd() { if (EA_PageUpdated != null) { EA_PageUpdated?.Invoke(); } } #endregion Private Methods } }