Skip to content

Commit

Permalink
Only wait for items to download if the user has items not already in …
Browse files Browse the repository at this point in the history
…the mobile db
  • Loading branch information
ChrispyPeaches committed May 10, 2024
1 parent 609fc18 commit dd36d28
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
36 changes: 34 additions & 2 deletions src/FocusApp.Client/Methods/User/GetUserLogin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ public async Task<Result> Handle(
string userName,
CancellationToken cancellationToken = default)
{
if (createUserResponse.User == null)
{
return null;
}

Shared.Models.User user = new()
{
Id = createUserResponse.User.Id,
Expand Down Expand Up @@ -233,10 +238,12 @@ public async Task<Result> Handle(
bool userExistsLocally = await _localContext.Users
.AnyAsync(u => u.Auth0Id == getUserResponse.User.Auth0Id || getUserResponse.User.Id == u.Id, cancellationToken);

// If the user doesn't exist locally, wait for all content to be downloaded to ensure the app has all of their items downloaded
// If the user doesn't exist locally and they have content that isnt't in the DB,
// wait for all content to be downloaded to ensure the app has all of their items downloaded
if (!userExistsLocally &&
_authService.StartupSyncTask != null &&
!_authService.StartupSyncTask.IsCompleted)
!_authService.StartupSyncTask.IsCompleted &&
await DoesUserHaveUnsyncedData(getUserResponse))
{
await _popupService.ShowPopupAsync<SyncDataLoadingPopupInterface>();
await _authService.StartupSyncTask.WaitAsync(cancellationToken);
Expand Down Expand Up @@ -300,6 +307,31 @@ await _mediator.Send(new SyncUserData.Query
return user;
}

/// <summary>
/// Check if the user owns any items that aren't in the mobile DB
/// </summary>
private async Task<bool> DoesUserHaveUnsyncedData(GetUserResponse response)
{
var mobileDbHasAllOwnedIslands = (await _localContext.Islands.Where(i => response.UserIslandIds.Contains(i.Id)).CountAsync()) ==
response.UserIslandIds.Count;
if (!mobileDbHasAllOwnedIslands) return true;

var mobileDbHasAllOwnedPets = (await _localContext.Pets.Where(p => response.UserPetIds.Contains(p.Id)).CountAsync()) ==
response.UserPetIds.Count;
if (!mobileDbHasAllOwnedPets) return true;

var mobileDbHasAllOwnedDecor = (await _localContext.Decor.Where(d => response.UserDecorIds.Contains(d.Id)).CountAsync()) ==
response.UserDecorIds.Count;
if (!mobileDbHasAllOwnedDecor) return true;


var mobileDbHasAllOwnedBadges = (await _localContext.Badges.Where(b => response.UserBadgeIds.Contains(b.Id)).CountAsync()) ==
response.UserBadgeIds.Count;
if (!mobileDbHasAllOwnedBadges) return true;

return false;
}

private IQueryable<Island> GetInitialIslandQuery()
{
return _localContext.Islands
Expand Down
8 changes: 4 additions & 4 deletions src/FocusCore/Responses/User/GetUserResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace FocusCore.Responses.User;
public class GetUserResponse
{
public BaseUser? User { get; set; }
public List<Guid>? UserIslandIds { get; set; }
public List<Guid>? UserPetIds { get; set; }
public List<Guid>? UserDecorIds { get; set; }
public List<Guid>? UserBadgeIds { get; set; }
public List<Guid> UserIslandIds { get; set; } = new List<Guid>();
public List<Guid> UserPetIds { get; set; } = new List<Guid>();
public List<Guid> UserDecorIds { get; set; } = new List<Guid>();
public List<Guid> UserBadgeIds { get; set; } = new List<Guid>();
}

0 comments on commit dd36d28

Please sign in to comment.