ComponentUpdateService improvements; UnreadMessages improvements; fixes, etc...

This commit is contained in:
Loretta 2024-08-23 17:55:00 +02:00
parent 9fc4cee752
commit a19ba4c86d
10 changed files with 88 additions and 34 deletions

View File

@ -1,13 +1,12 @@
using TIAMWebApp.Shared.Application.Interfaces;
namespace TIAMMobileApp.Services
{
public class ComponentUpdateServiceMobile : IComponentUpdateService
public class ComponentUpdateServiceMobile : ComponentUpdateServiceBase
{
public event Action RefreshRequested;
public void CallRequestRefresh()
public override void CallRequestRefreshAll()
{
RefreshRequested?.Invoke();
base.CallRequestRefreshAll();
}
}
}

View File

@ -124,18 +124,19 @@
if (isSuccess)
{
await SaveToSessionInfo(user);
_logger.Info($"Saved user in db is: {user.DisplayName}, setting autenthicated state");
_adminSignalRClient.GetByIdAsync<SiteViewModel>(SignalRTags.GetSiteViewModelByUserId, signalResponseMessage =>
{
_siteViewModel.Initialize(signalResponseMessage.ResponseData!);
_logger.Debug($"UnreadMessages: {_siteViewModel.UnreadMessages.Count}");
ComponentUpdateService.CallRequestRefreshAll();
return Task.CompletedTask;
}, user.UserId).Forget();
}
await SaveToSessionInfo(user);
_logger.Info($"Saved user in db is: {user.DisplayName}, setting autenthicated state");
//NavManager.NavigateTo("/");
}
else
@ -143,8 +144,8 @@
_logger.Info("No token stored yet");
//NavManager.NavigateTo("/");
}
ComponentUpdateService.CallRequestRefresh();
//ComponentUpdateService.CallRequestRefresh();
}
protected async Task SaveToSessionInfo(UserSessionModel user)
@ -153,7 +154,7 @@
sessionService.IsAuthenticated = true;
sessionService.HasCompany = user.UserModelDto.UserProductMappings.Count > 0;
_logger.Debug($"Checking driver for: {user.UserModelDto.UserProductMappings.Count} mapping");
sessionService.IsDriver = await CheckIfDriver(user.UserModelDto.UserProductMappings);
sessionService.IsDriver = CheckIfDriver(user.UserModelDto.UserProductMappings);
if (user.UserModelDto.Id == TiamConstClient.DevAdminIds[0] || user.UserModelDto.Id == TiamConstClient.DevAdminIds[1])
{
sessionService.IsDevAdmin = true;
@ -168,22 +169,23 @@
_logger.Debug($"Saved to session: IsAuthenticated: {sessionService.IsAuthenticated}, HasCompany: {sessionService.HasCompany}, IsDriver: {sessionService.IsDriver}, IsDevAdmin: {sessionService.IsDevAdmin}, IsSysAdmin: {sessionService.IsSysAdmin}");
}
public async Task<bool> CheckIfDriver(List<UserProductMapping> Permissions)
public bool CheckIfDriver(List<UserProductMapping> permissions)
{
bool _isDriver = false;
bool isDriver = false;
foreach (UserProductMapping Permission in Permissions)
foreach (UserProductMapping permission in permissions)
{
//var permissionToCheck = await ServiceProviderDataService.GetUserProductMappingByIdAsync(Permission.Id);
_logger.Debug($"calling IsPowerOf with values: {Permission.Id}, {Permission.Permissions}, {1}");
var driverPermissionResult = IsBitSet(Permission.Permissions, 1);
_logger.Debug($"calling IsPowerOf with values: {permission.Id}, {permission.Permissions}, {1}");
var driverPermissionResult = IsBitSet(permission.Permissions, 1);
if (driverPermissionResult)
{
_isDriver = true;
sessionService.DriverPersmissionId = Permission.Id;
isDriver = true;
sessionService.DriverPersmissionId = permission.Id;
}
}
return _isDriver;
return isDriver;
}
public bool IsBitSet(int number, int power)
@ -221,7 +223,7 @@
get => CultureInfo.CurrentCulture;
set
{
if (CultureInfo.CurrentCulture != value)
if (!Equals(CultureInfo.CurrentCulture, value))
{
Thread.CurrentThread.CurrentCulture = value;
Thread.CurrentThread.CurrentUICulture = value;

View File

@ -48,7 +48,7 @@ namespace TIAMSharedUI.Pages.Components
//Something is cancelled
//args.Cancel = true;
//}
componentUpdateService.CallRequestRefresh();
componentUpdateService.CallRequestRefresh<Navbar>();
StateHasChanged();
//throw new NotImplementedException();

View File

@ -49,7 +49,7 @@ namespace TIAMSharedUI.Pages.Components
{
secureStorageHandler?.SaveToSecureStorageAsync(nameof(Setting.Locale), SelectedCulture.Name);
componentUpdateService.CallRequestRefresh();
componentUpdateService.CallRequestRefreshAll();
}

View File

@ -38,7 +38,7 @@ namespace TIAMSharedUI.Pages
[Inject]
public AuthenticationStateProvider AuthStateProvider { get; set; }
[Inject] public IComponentUpdateService componentUpdateService { get; set; }
//fill loginmodel with fake but valid data
//LoginModel loginModel = new();
@ -126,11 +126,11 @@ namespace TIAMSharedUI.Pages
_siteViewModel.Initialize(signalResponseMessage.ResponseData!);
BrowserConsoleLogWriter.Debug($"UnreadMessages: {_siteViewModel.UnreadMessages.Count}");
componentUpdateService.CallRequestRefreshAll();
return Task.CompletedTask;
}, user.UserId).Forget();
SaveToSessionInfo(user).Forget();
navManager.NavigateTo("/");
}

View File

@ -20,11 +20,13 @@
@using TIAM.Entities.Emails
@using AyCode.Blazor.Components.Services
@using AyCode.Core.Extensions
@using TIAM.Models
@using TIAM.Services
@inherits UserBasePageComponent
@inject IEnumerable<IAcLogWriterClientBase> LogWriters
@inject AdminSignalRClient AdminSignalRClient;
@inject SiteViewModel SiteViewModel;
@inject IComponentUpdateService ComponentUpdateService
<MessageDetailGrid CssClass="my-grid" @ref="_messageGrid"
Logger="_logger"
@ -138,6 +140,9 @@
emailMessage.IsReaded = true;
_messageGrid.UpdateDataItemAsync(emailMessage).Forget();
SiteViewModel.UnreadMessages.RemoveAll(x => x.Id == emailMessage.Id);
ComponentUpdateService.CallRequestRefreshAll();
//InvokeAsync(StateHasChanged).ContinueWith(x => _messageGrid.UpdateDataItemAsync(emailMessage).Forget());
}
}

View File

@ -80,7 +80,7 @@
@{
string url3 = $"user/messages/{userId}";
<NavLink class="nav-link" href="@url3">
<i class="fa-solid fa-envelope"></i>
<i class="fa-solid fa-envelope">@(" (" + SiteViewModel.UnreadMessages.Count + ")")</i>
</NavLink>
}
</li>

View File

@ -195,7 +195,7 @@ namespace TIAMSharedUI.Shared.Components
base.OnAfterRender(firstRender);
if (firstRender)
componentUpdateService.RefreshRequested += RefreshMe;
componentUpdateService.GetOrAddComponent<Navbar>().RefreshRequested += RefreshMe;
}
}
}

View File

@ -1,13 +1,11 @@
using TIAMWebApp.Shared.Application.Interfaces;
namespace TIAMWebApp.Client.Services
{
public class ComponentUpdateServiceWeb : IComponentUpdateService
public class ComponentUpdateServiceWeb : ComponentUpdateServiceBase
{
public event Action? RefreshRequested;
public void CallRequestRefresh()
public override void CallRequestRefreshAll()
{
RefreshRequested?.Invoke();
base.CallRequestRefreshAll();
}
}
}

View File

@ -1,8 +1,58 @@
namespace TIAMWebApp.Shared.Application.Interfaces
using Microsoft.AspNetCore.Components;
namespace TIAMWebApp.Shared.Application.Interfaces
{
public interface IComponentUpdateService
public abstract class ComponentUpdateServiceBase : IComponentUpdateService
{
event Action RefreshRequested;
protected Dictionary<Type, IComponentUpdateItem> ComponentsByType = [];
public virtual void CallRequestRefreshAll()
{
foreach (var component in ComponentsByType.Values)
component.CallRequestRefresh();
}
public void CallRequestRefresh<T>() where T : class, IComponent
{
if (ComponentsByType.TryGetValue(typeof(T), out var componentUpdateItem))
componentUpdateItem.CallRequestRefresh();
}
public IComponentUpdateItem GetOrAddComponent<T>() where T : class, IComponent
{
var componentType = typeof(T);
if (ComponentsByType.TryGetValue(componentType, out var componentUpdateItem))
return componentUpdateItem;
componentUpdateItem = new ComponentUpdateItem();
ComponentsByType.Add(componentType, componentUpdateItem);
return componentUpdateItem;
}
}
public class ComponentUpdateItem : IComponentUpdateItem
{
public event Action? RefreshRequested;
public virtual void CallRequestRefresh()
{
RefreshRequested?.Invoke();
}
}
public interface IComponentUpdateItem
{
event Action? RefreshRequested;
void CallRequestRefresh();
}
public interface IComponentUpdateService
{
void CallRequestRefreshAll();
void CallRequestRefresh<T>() where T : class, IComponent;
IComponentUpdateItem GetOrAddComponent<T>() where T : class, IComponent;
}
}