248befa2ec
- metodo ReloadReq inserito a livello di BBMService (singleton) - NON funziona con MService che è scoped = per tab
298 lines
8.0 KiB
C#
298 lines
8.0 KiB
C#
using Blazored.LocalStorage;
|
|
using Blazored.SessionStorage;
|
|
using NLog;
|
|
|
|
namespace SHERPA.BBM.UI.Data
|
|
{
|
|
public class MessageService
|
|
{
|
|
#region Public Constructors
|
|
|
|
public MessageService(ILocalStorageService genLocalStorage, ISessionStorageService sessStore)
|
|
{
|
|
localStore = genLocalStorage;
|
|
sessionStore = sessStore;
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Events
|
|
|
|
/// <summary>
|
|
/// Evento hide ricerca
|
|
/// </summary>
|
|
public event Action? EA_HideSearch;
|
|
|
|
/// <summary>
|
|
/// Evento aggiornamento ricerca
|
|
/// </summary>
|
|
public event Action? EA_SearchUpdated;
|
|
|
|
/// <summary>
|
|
/// Evento show ricerca
|
|
/// </summary>
|
|
public event Action? EA_ShowSearch;
|
|
|
|
/// <summary>
|
|
/// Evento modificata head pagina
|
|
/// </summary>
|
|
public event Action? HeadChanged;
|
|
|
|
#endregion Public Events
|
|
|
|
#region Public Properties
|
|
|
|
/// <summary>
|
|
/// Valore ricerca corrente
|
|
/// </summary>
|
|
public string SearchVal
|
|
{
|
|
get => searchVal;
|
|
set
|
|
{
|
|
if (searchVal != value || string.IsNullOrEmpty(value))
|
|
{
|
|
searchVal = value;
|
|
|
|
if (EA_SearchUpdated != null)
|
|
{
|
|
EA_SearchUpdated?.Invoke();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion Public Properties
|
|
|
|
#region Public Methods
|
|
|
|
public void NotifyHeadChanged() => HeadChanged?.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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Cerca un parametro utente salvato (se presente) e restituisce, se non trova restituisce ""
|
|
/// </summary>
|
|
/// <param name="key"></param>
|
|
/// <returns></returns>
|
|
public string UsrParamGet(string key)
|
|
{
|
|
string answ = "";
|
|
if (userCurrParams.ContainsKey(key))
|
|
{
|
|
answ = userCurrParams[key];
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Cerca un parametro utente salvato (se presente) e restituisce, se non trova restituisce 0
|
|
/// </summary>
|
|
/// <param name="key"></param>
|
|
/// <param name="nullVal">Valore da restituire se NON trovato</param>
|
|
/// <returns></returns>
|
|
public int UsrParamGetUInt(string key, int nullVal)
|
|
{
|
|
int answ = nullVal;
|
|
var sVal = UsrParamGet(key);
|
|
if (!string.IsNullOrEmpty(sVal))
|
|
{
|
|
int.TryParse(sVal, out answ);
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Upsert di un parametro utente
|
|
/// </summary>
|
|
/// <param name="key"></param>
|
|
/// <param name="value"></param>
|
|
/// <returns></returns>
|
|
public string UsrParamSet(string key, string value)
|
|
{
|
|
string answ = "";
|
|
if (userCurrParams.ContainsKey(key))
|
|
{
|
|
userCurrParams[key] = value;
|
|
}
|
|
else
|
|
{
|
|
userCurrParams.Add(key, value);
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Protected Properties
|
|
|
|
protected ILocalStorageService localStore { get; set; } = null!;
|
|
|
|
protected ISessionStorageService sessionStore { get; set; } = null!;
|
|
|
|
#endregion Protected Properties
|
|
|
|
#region Protected Methods
|
|
|
|
protected void reportSearch()
|
|
{
|
|
if (EA_SearchUpdated != null)
|
|
{
|
|
EA_SearchUpdated?.Invoke();
|
|
}
|
|
}
|
|
|
|
#endregion Protected Methods
|
|
|
|
#region Private Fields
|
|
|
|
private Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
private string searchVal = "";
|
|
|
|
private bool showSearch;
|
|
|
|
/// <summary>
|
|
/// Dizionario parametri utente (in memoria x sessione utente)
|
|
/// </summary>
|
|
private Dictionary<string, string> userCurrParams = new Dictionary<string, string>();
|
|
|
|
#endregion Private Fields
|
|
}
|
|
} |