From 85088203500102370566c0037a57f7bb8fdd9fc8 Mon Sep 17 00:00:00 2001 From: MarcoE Date: Wed, 3 Sep 2025 17:26:55 +0200 Subject: [PATCH] Migliorata sincronizzazione dei dati --- .../Core/Services/ManageDataService.cs | 2 - salesbook.Maui/MauiProgram.cs | 1 + salesbook.Shared/Components/Pages/Home.razor | 11 +++ salesbook.Shared/Components/Pages/Users.razor | 84 ++++++------------- .../Core/Dto/PageState/UserListState.cs | 14 +++- .../Core/Services/PreloadService.cs | 55 ++++++++++++ 6 files changed, 107 insertions(+), 60 deletions(-) create mode 100644 salesbook.Shared/Core/Services/PreloadService.cs diff --git a/salesbook.Maui/Core/Services/ManageDataService.cs b/salesbook.Maui/Core/Services/ManageDataService.cs index 44555b0..bf9fc14 100644 --- a/salesbook.Maui/Core/Services/ManageDataService.cs +++ b/salesbook.Maui/Core/Services/ManageDataService.cs @@ -37,7 +37,6 @@ public class ManageDataService( ReturnPersRif = !whereCond.OnlyContact } ); - _ = UpdateDbUsers(response); clienti = response.AnagClie ?? []; } @@ -69,7 +68,6 @@ public class ManageDataService( ReturnPersRif = !whereCond.OnlyContact } ); - _ = UpdateDbUsers(response); prospect = response.PtbPros ?? []; } diff --git a/salesbook.Maui/MauiProgram.cs b/salesbook.Maui/MauiProgram.cs index d6350f3..6fed6d9 100644 --- a/salesbook.Maui/MauiProgram.cs +++ b/salesbook.Maui/MauiProgram.cs @@ -58,6 +58,7 @@ namespace salesbook.Maui builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); + builder.Services.AddScoped(); //SessionData builder.Services.AddSingleton(); diff --git a/salesbook.Shared/Components/Pages/Home.razor b/salesbook.Shared/Components/Pages/Home.razor index 6e62825..c3b7444 100644 --- a/salesbook.Shared/Components/Pages/Home.razor +++ b/salesbook.Shared/Components/Pages/Home.razor @@ -2,8 +2,10 @@ @attribute [Authorize] @using salesbook.Shared.Core.Interface @using salesbook.Shared.Components.Layout.Spinner +@using salesbook.Shared.Core.Services @inject IFormFactor FormFactor @inject INetworkService NetworkService +@inject PreloadService PreloadService @@ -19,6 +21,15 @@ return; } + _ = StartSyncUser(); NavigationManager.NavigateTo("/Calendar"); } + + private Task StartSyncUser() + { + return Task.Run(() => + { + _ = PreloadService.PreloadUsersAsync(); + }); + } } diff --git a/salesbook.Shared/Components/Pages/Users.razor b/salesbook.Shared/Components/Pages/Users.razor index 449f7f1..610d3c7 100644 --- a/salesbook.Shared/Components/Pages/Users.razor +++ b/salesbook.Shared/Components/Pages/Users.razor @@ -6,7 +6,6 @@ @using salesbook.Shared.Components.SingleElements.BottomSheet @using salesbook.Shared.Components.Layout.Spinner @using salesbook.Shared.Components.SingleElements -@using salesbook.Shared.Core.Dto.Contact @using salesbook.Shared.Core.Dto.PageState @using salesbook.Shared.Core.Dto.Users @using salesbook.Shared.Core.Entity @@ -15,6 +14,7 @@ @inject NewContactService NewContact @inject FilterUserDTO Filter @inject UserListState UserState +@implements IDisposable @@ -71,33 +71,37 @@ private bool OpenFilter { get; set; } private string TypeUser { get; set; } = "all"; - protected override void OnInitialized() + protected override async Task OnInitializedAsync() { + IsLoading = true; + + if (!UserState.IsLoaded) + { + UserState.OnUsersLoaded += OnUsersLoaded; + } + else + { + await LoadData(); + LoadFromSession(); + FilterUsers(); + } + NewContact.OnContactCreated += async response => await OnUserCreated(response); } - protected override async Task OnAfterRenderAsync(bool firstRender) + private void OnUsersLoaded() { - if (firstRender) + InvokeAsync(async () => { - IsLoading = true; - StateHasChanged(); - - if (UserState.FilteredGroupedUserList == null && UserState.GroupedUserList == null) - { - await LoadData(); - SetDataSession(); - } - else - { - LoadFromSession(); - } - + await LoadData(); + LoadFromSession(); FilterUsers(); + }); + } - IsLoading = false; - StateHasChanged(); - } + void IDisposable.Dispose() + { + UserState.OnUsersLoaded -= OnUsersLoaded; } private void LoadFromSession() @@ -106,12 +110,6 @@ FilteredGroupedUserList = UserState.FilteredGroupedUserList!; } - private void SetDataSession() - { - UserState.GroupedUserList = GroupedUserList; - UserState.FilteredGroupedUserList = FilteredGroupedUserList; - } - private async Task LoadData() { if (!Filter.IsInitialized) @@ -123,37 +121,6 @@ Filter.IsInitialized = true; } - - var users = await ManageData.GetContact(new WhereCondContact {FlagStato = "A"}); - - var sortedUsers = users - .Where(u => !string.IsNullOrWhiteSpace(u.RagSoc)) - .OrderBy(u => - { - var firstChar = char.ToUpper(u.RagSoc[0]); - return char.IsLetter(firstChar) ? firstChar.ToString() : "ZZZ"; - }) - .ThenBy(u => u.RagSoc) - .ToList(); - - GroupedUserList = []; - - string? lastHeader = null; - foreach (var user in sortedUsers) - { - var firstChar = char.ToUpper(user.RagSoc[0]); - var currentLetter = char.IsLetter(firstChar) ? firstChar.ToString() : "#"; - - var showHeader = currentLetter != lastHeader; - lastHeader = currentLetter; - - GroupedUserList.Add(new UserDisplayItem - { - User = user, - ShowHeader = showHeader, - HeaderLetter = currentLetter - }); - } } private void FilterUsers() => FilterUsers(false); @@ -220,6 +187,9 @@ } FilteredGroupedUserList = result; + + IsLoading = false; + StateHasChanged(); } private async Task OnUserCreated(CRMCreateContactResponseDTO response) diff --git a/salesbook.Shared/Core/Dto/PageState/UserListState.cs b/salesbook.Shared/Core/Dto/PageState/UserListState.cs index c1eb635..c1451b0 100644 --- a/salesbook.Shared/Core/Dto/PageState/UserListState.cs +++ b/salesbook.Shared/Core/Dto/PageState/UserListState.cs @@ -6,4 +6,16 @@ public class UserListState { public List? GroupedUserList { get; set; } public List? FilteredGroupedUserList { get; set; } -} \ No newline at end of file + + public bool IsLoaded { get; set; } + public bool IsLoading { get; set; } + + public event Action? OnUsersLoaded; + + public void NotifyUsersLoaded() + { + IsLoaded = true; + IsLoading = false; + OnUsersLoaded?.Invoke(); + } +} diff --git a/salesbook.Shared/Core/Services/PreloadService.cs b/salesbook.Shared/Core/Services/PreloadService.cs new file mode 100644 index 0000000..2712494 --- /dev/null +++ b/salesbook.Shared/Core/Services/PreloadService.cs @@ -0,0 +1,55 @@ +using salesbook.Shared.Core.Dto; +using salesbook.Shared.Core.Dto.Contact; +using salesbook.Shared.Core.Dto.PageState; +using salesbook.Shared.Core.Dto.Users; +using salesbook.Shared.Core.Interface; + +namespace salesbook.Shared.Core.Services; + +public class PreloadService(IManageDataService manageData, UserListState userState) +{ + public async Task PreloadUsersAsync() + { + if (userState.IsLoaded || userState.IsLoading) + return; + + userState.IsLoading = true; + + var users = await manageData.GetContact(new WhereCondContact { FlagStato = "A" }); + + var sorted = users + .Where(u => !string.IsNullOrWhiteSpace(u.RagSoc)) + .OrderBy(u => char.IsLetter(char.ToUpper(u.RagSoc[0])) ? char.ToUpper(u.RagSoc[0]).ToString() : "ZZZ") + .ThenBy(u => u.RagSoc) + .ToList(); + + userState.GroupedUserList = BuildGroupedList(sorted); + userState.FilteredGroupedUserList = userState.GroupedUserList; + + userState.NotifyUsersLoaded(); + } + + private static List BuildGroupedList(List users) + { + var grouped = new List(); + string? lastHeader = null; + + foreach (var user in users) + { + var firstChar = char.ToUpper(user.RagSoc[0]); + var currentLetter = char.IsLetter(firstChar) ? firstChar.ToString() : "#"; + + var showHeader = currentLetter != lastHeader; + lastHeader = currentLetter; + + grouped.Add(new UserDisplayItem + { + User = user, + ShowHeader = showHeader, + HeaderLetter = currentLetter + }); + } + + return grouped; + } +} \ No newline at end of file