diff --git a/TIAMMobileApp/Services/ComponentUpdateServiceWeb.cs b/TIAMMobileApp/Services/ComponentUpdateServiceWeb.cs index 24b18390..5f437213 100644 --- a/TIAMMobileApp/Services/ComponentUpdateServiceWeb.cs +++ b/TIAMMobileApp/Services/ComponentUpdateServiceWeb.cs @@ -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(); } + } } diff --git a/TIAMSharedUI/Pages/AppLaunchComponent.razor b/TIAMSharedUI/Pages/AppLaunchComponent.razor index a599a681..a917c6b8 100644 --- a/TIAMSharedUI/Pages/AppLaunchComponent.razor +++ b/TIAMSharedUI/Pages/AppLaunchComponent.razor @@ -124,18 +124,19 @@ if (isSuccess) { + await SaveToSessionInfo(user); + _logger.Info($"Saved user in db is: {user.DisplayName}, setting autenthicated state"); + _adminSignalRClient.GetByIdAsync(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 CheckIfDriver(List Permissions) + public bool CheckIfDriver(List 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; diff --git a/TIAMSharedUI/Pages/Components/AuthComponent.razor.cs b/TIAMSharedUI/Pages/Components/AuthComponent.razor.cs index 1a33dbd1..4f9ec305 100644 --- a/TIAMSharedUI/Pages/Components/AuthComponent.razor.cs +++ b/TIAMSharedUI/Pages/Components/AuthComponent.razor.cs @@ -48,7 +48,7 @@ namespace TIAMSharedUI.Pages.Components //Something is cancelled //args.Cancel = true; //} - componentUpdateService.CallRequestRefresh(); + componentUpdateService.CallRequestRefresh(); StateHasChanged(); //throw new NotImplementedException(); diff --git a/TIAMSharedUI/Pages/Components/SettingsBasic.razor.cs b/TIAMSharedUI/Pages/Components/SettingsBasic.razor.cs index cb31b53f..64abe0ea 100644 --- a/TIAMSharedUI/Pages/Components/SettingsBasic.razor.cs +++ b/TIAMSharedUI/Pages/Components/SettingsBasic.razor.cs @@ -49,7 +49,7 @@ namespace TIAMSharedUI.Pages.Components { secureStorageHandler?.SaveToSecureStorageAsync(nameof(Setting.Locale), SelectedCulture.Name); - componentUpdateService.CallRequestRefresh(); + componentUpdateService.CallRequestRefreshAll(); } diff --git a/TIAMSharedUI/Pages/Login.razor.cs b/TIAMSharedUI/Pages/Login.razor.cs index 6515959e..123c8abc 100644 --- a/TIAMSharedUI/Pages/Login.razor.cs +++ b/TIAMSharedUI/Pages/Login.razor.cs @@ -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("/"); } diff --git a/TIAMSharedUI/Pages/User/SysAdmins/MessageDetailGridComponent.razor b/TIAMSharedUI/Pages/User/SysAdmins/MessageDetailGridComponent.razor index 993898fb..1deda6e8 100644 --- a/TIAMSharedUI/Pages/User/SysAdmins/MessageDetailGridComponent.razor +++ b/TIAMSharedUI/Pages/User/SysAdmins/MessageDetailGridComponent.razor @@ -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 LogWriters @inject AdminSignalRClient AdminSignalRClient; - +@inject SiteViewModel SiteViewModel; +@inject IComponentUpdateService ComponentUpdateService x.Id == emailMessage.Id); + ComponentUpdateService.CallRequestRefreshAll(); //InvokeAsync(StateHasChanged).ContinueWith(x => _messageGrid.UpdateDataItemAsync(emailMessage).Forget()); } } diff --git a/TIAMSharedUI/Shared/Components/Navbar.razor b/TIAMSharedUI/Shared/Components/Navbar.razor index 5c96843c..5c4c43a2 100644 --- a/TIAMSharedUI/Shared/Components/Navbar.razor +++ b/TIAMSharedUI/Shared/Components/Navbar.razor @@ -80,7 +80,7 @@ @{ string url3 = $"user/messages/{userId}"; - + @(" (" + SiteViewModel.UnreadMessages.Count + ")") } diff --git a/TIAMSharedUI/Shared/Components/Navbar.razor.cs b/TIAMSharedUI/Shared/Components/Navbar.razor.cs index a87d4831..058e763e 100644 --- a/TIAMSharedUI/Shared/Components/Navbar.razor.cs +++ b/TIAMSharedUI/Shared/Components/Navbar.razor.cs @@ -195,7 +195,7 @@ namespace TIAMSharedUI.Shared.Components base.OnAfterRender(firstRender); if (firstRender) - componentUpdateService.RefreshRequested += RefreshMe; + componentUpdateService.GetOrAddComponent().RefreshRequested += RefreshMe; } } } diff --git a/TIAMWebApp/Client/Services/ComponentUpdateServiceWeb.cs b/TIAMWebApp/Client/Services/ComponentUpdateServiceWeb.cs index e7549185..dbb092c1 100644 --- a/TIAMWebApp/Client/Services/ComponentUpdateServiceWeb.cs +++ b/TIAMWebApp/Client/Services/ComponentUpdateServiceWeb.cs @@ -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(); } } } diff --git a/TIAMWebApp/Shared/Interfaces/IComponentUpdateService.cs b/TIAMWebApp/Shared/Interfaces/IComponentUpdateService.cs index 8c60c09f..434ac3ca 100644 --- a/TIAMWebApp/Shared/Interfaces/IComponentUpdateService.cs +++ b/TIAMWebApp/Shared/Interfaces/IComponentUpdateService.cs @@ -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 ComponentsByType = []; + + public virtual void CallRequestRefreshAll() + { + foreach (var component in ComponentsByType.Values) + component.CallRequestRefresh(); + } + + public void CallRequestRefresh() where T : class, IComponent + { + if (ComponentsByType.TryGetValue(typeof(T), out var componentUpdateItem)) + componentUpdateItem.CallRequestRefresh(); + + } + + public IComponentUpdateItem GetOrAddComponent() 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() where T : class, IComponent; + + IComponentUpdateItem GetOrAddComponent() where T : class, IComponent; + } }