Migliorata sincronizzazione dei dati

This commit is contained in:
2025-09-03 17:26:55 +02:00
parent 374b99501e
commit 8508820350
6 changed files with 107 additions and 60 deletions

View File

@@ -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 ?? [];
}

View File

@@ -58,6 +58,7 @@ namespace salesbook.Maui
builder.Services.AddScoped<IIntegryApiService, IntegryApiService>();
builder.Services.AddScoped<ISyncDbService, SyncDbService>();
builder.Services.AddScoped<IManageDataService, ManageDataService>();
builder.Services.AddScoped<PreloadService>();
//SessionData
builder.Services.AddSingleton<JobSteps>();

View File

@@ -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
<SpinnerLayout FullScreen="true" />
@@ -19,6 +21,15 @@
return;
}
_ = StartSyncUser();
NavigationManager.NavigateTo("/Calendar");
}
private Task StartSyncUser()
{
return Task.Run(() =>
{
_ = PreloadService.PreloadUsersAsync();
});
}
}

View File

@@ -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
<HeaderLayout Title="Contatti"/>
@@ -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)

View File

@@ -6,4 +6,16 @@ public class UserListState
{
public List<UserDisplayItem>? GroupedUserList { get; set; }
public List<UserDisplayItem>? FilteredGroupedUserList { get; set; }
}
public bool IsLoaded { get; set; }
public bool IsLoading { get; set; }
public event Action? OnUsersLoaded;
public void NotifyUsersLoaded()
{
IsLoaded = true;
IsLoading = false;
OnUsersLoaded?.Invoke();
}
}

View File

@@ -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<UserDisplayItem> BuildGroupedList(List<ContactDTO> users)
{
var grouped = new List<UserDisplayItem>();
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;
}
}