Skip to content

Commit

Permalink
Remove usage of CurrentUser throughout ap
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrispyPeaches committed May 9, 2024
1 parent 76d7c08 commit de8470c
Show file tree
Hide file tree
Showing 24 changed files with 115 additions and 109 deletions.
46 changes: 29 additions & 17 deletions src/FocusApp.Client/Helpers/AuthenticationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,39 @@ namespace FocusApp.Client.Helpers;

internal interface IAuthenticationService
{
bool IsLoggedIn { get; }
Guid? Id { get; set; }
string? Auth0Id { get; set; }
string? Email { get; set; }
User? CurrentUser { get; set; }
string? UserName { get; set; }
string? Pronouns { get; set; }
int Balance { get; set; }
DateTime? DateCreated { get; set; }
byte[]? ProfilePicture { get; set; }
Island? SelectedIsland { get; set; }
Pet? SelectedPet { get; set; }
Badge? SelectedBadge { get; set; }
Decor? SelectedDecor { get; set; }
int Balance { get; set; }
Task? StartupSyncTask { get; set; }

event PropertyChangedEventHandler? PropertyChanged;

void ClearUser();
Task Logout(IAuth0Client auth0Client);
void PopulateWithUserData(User user);
}

public class AuthenticationService : INotifyPropertyChanged, IAuthenticationService
{
public bool IsLoggedIn => Auth0Id is not null;
public event PropertyChangedEventHandler? PropertyChanged;
public Guid? Id { get; set; }
public string? Auth0Id { get; set; } = "";
public string? Email { get; set; } = "";

private User? _currentUser;
public User? CurrentUser
{
get => _currentUser;
set => SetProperty(ref _currentUser, value);
}
public string? UserName { get; set; } = "";
public string? Pronouns { get; set; } = "";
public DateTime? DateCreated { get; set; }
public byte[]? ProfilePicture { get; set; }
public Task? StartupSyncTask { get; set; }

private int? _balance;
public int Balance
Expand Down Expand Up @@ -99,26 +104,33 @@ public async Task Logout(IAuth0Client auth0Client)
/// </summary>
public void ClearUser()
{
Auth0Id = string.Empty;
Email = string.Empty;
Id = null;
Auth0Id = null;
Email = null;
UserName = null;
Pronouns = null;
Balance = 0;
CurrentUser = null;
DateCreated = DateTime.MinValue;
ProfilePicture = null;
SelectedIsland = null;
SelectedPet = null;
SelectedBadge = null;
SelectedDecor = null;
SelectedPet = null;
}

public void PopulateWithUserData(User user)
{
CurrentUser = user;

Id = user.Id;
Auth0Id = user.Auth0Id;
Email = user.Email;
UserName = user.UserName;
Pronouns = user.Pronouns;
Balance = user.Balance;
DateCreated = user.DateCreated;
ProfilePicture = user.ProfilePicture;
SelectedIsland = user.SelectedIsland;
SelectedBadge = user.SelectedBadge;
SelectedDecor = user.SelectedDecor;
SelectedIsland = user.SelectedIsland;
SelectedPet = user.SelectedPet;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public async Task<BadgeEligibilityResult> Handle(Query query, CancellationToken
IsEligible = false
};

if (_authenticationService.CurrentUser is null)
if (!_authenticationService.IsLoggedIn)
throw new InvalidOperationException("User is not logged in.");

string? badgeName = "Downtime";
Expand All @@ -50,7 +50,7 @@ private async Task AddBadgeToUser(string? badgeName, BadgeEligibilityResult resu
{
bool hasBadge = await _localContext.UserBadges
.Where(userBadge =>
userBadge.UserId == _authenticationService.CurrentUser.Id &&
userBadge.UserId == _authenticationService.Id.Value &&
userBadge.Badge.Name == badgeName)
.AnyAsync(cancellationToken);

Expand All @@ -62,7 +62,7 @@ private async Task AddBadgeToUser(string? badgeName, BadgeEligibilityResult resu
_localContext.UserBadges.Add(new UserBadge()
{
BadgeId = result.EarnedBadge.Id,
UserId = _authenticationService.CurrentUser.Id,
UserId = _authenticationService.Id.Value,
DateAcquired = DateTime.UtcNow
});

Expand All @@ -72,7 +72,7 @@ private async Task AddBadgeToUser(string? badgeName, BadgeEligibilityResult resu
await _client.AddUserBadge(new AddUserBadgeCommand()
{
BadgeId = result.EarnedBadge.Id,
UserId = _authenticationService.CurrentUser.Id
UserId = _authenticationService.Id.Value
},
cancellationToken);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public async Task<BadgeEligibilityResult> Handle(Query query, CancellationToken
{
Shared.Models.User? user = await _localContext.Users
.Include(u => u.Decor)
.SingleOrDefaultAsync(u => u.Id == _authenticationService.CurrentUser.Id, cancellationToken);
.SingleOrDefaultAsync(u => u.Id == _authenticationService.Id.Value, cancellationToken);

if (user == null)
throw new InvalidOperationException("User not found in local database.");
Expand Down Expand Up @@ -62,7 +62,7 @@ public async Task<BadgeEligibilityResult> Handle(Query query, CancellationToken
// Save new user badge to server database
try
{
await _client.AddUserBadge(new AddUserBadgeCommand { UserId = _authenticationService.CurrentUser.Id, BadgeId = result.EarnedBadge.Id });
await _client.AddUserBadge(new AddUserBadgeCommand { UserId = _authenticationService.Id.Value, BadgeId = result.EarnedBadge.Id });
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ public async Task<BadgeEligibilityResult> Handle(Query query, CancellationToken
IsEligible = false
};

if (_authenticationService.CurrentUser is null)
if (_authenticationService.IsLoggedIn)
{
throw new InvalidOperationException("User is not logged in.");
}

int sessionCount = await _localContext.UserSessionHistory
.CountAsync(u => u.UserId == _authenticationService.CurrentUser.Id, cancellationToken);
.CountAsync(u => u.UserId == _authenticationService.Id.Value, cancellationToken);

string? badgeName = sessionCount switch
{
Expand All @@ -61,7 +61,7 @@ private async Task AddBadgeToUser(string? badgeName, BadgeEligibilityResult resu
{
bool hasBadge = await _localContext.UserBadges
.Where(userBadge =>
userBadge.UserId == _authenticationService.CurrentUser.Id &&
userBadge.UserId == _authenticationService.Id.Value &&
userBadge.Badge.Name == badgeName)
.AnyAsync(cancellationToken);

Expand All @@ -73,7 +73,7 @@ private async Task AddBadgeToUser(string? badgeName, BadgeEligibilityResult resu
_localContext.UserBadges.Add(new UserBadge()
{
BadgeId = result.EarnedBadge.Id,
UserId = _authenticationService.CurrentUser.Id,
UserId = _authenticationService.Id.Value,
DateAcquired = DateTime.UtcNow
});

Expand All @@ -83,7 +83,7 @@ private async Task AddBadgeToUser(string? badgeName, BadgeEligibilityResult resu
await _client.AddUserBadge(new AddUserBadgeCommand()
{
BadgeId = result.EarnedBadge.Id,
UserId = _authenticationService.CurrentUser.Id
UserId = _authenticationService.Id.Value
},
cancellationToken);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public async Task<BadgeEligibilityResult> Handle(Query query, CancellationToken
{
Shared.Models.User? user = await _localContext.Users
.Include(u => u.Islands)
.SingleOrDefaultAsync(u => u.Id == _authenticationService.CurrentUser.Id, cancellationToken);
.SingleOrDefaultAsync(u => u.Id == _authenticationService.Id.Value, cancellationToken);

if (user == null)
throw new InvalidOperationException("User not found in local database.");
Expand Down Expand Up @@ -63,7 +63,7 @@ public async Task<BadgeEligibilityResult> Handle(Query query, CancellationToken
// Save new user badge to server database
try
{
await _client.AddUserBadge(new AddUserBadgeCommand { UserId = _authenticationService.CurrentUser.Id, BadgeId = result.EarnedBadge.Id });
await _client.AddUserBadge(new AddUserBadgeCommand { UserId = _authenticationService.Id.Value, BadgeId = result.EarnedBadge.Id });
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public async Task<BadgeEligibilityResult> Handle(Query query, CancellationToken
{
Shared.Models.User? user = await _localContext.Users
.Include(u => u.Pets)
.SingleOrDefaultAsync(u => u.Id == _authenticationService.CurrentUser.Id, cancellationToken);
.SingleOrDefaultAsync(u => u.Id == _authenticationService.Id.Value, cancellationToken);

if (user == null)
throw new InvalidOperationException("User not found in local database.");
Expand Down Expand Up @@ -63,7 +63,7 @@ public async Task<BadgeEligibilityResult> Handle(Query query, CancellationToken
// Save new user badge to server database
try
{
await _client.AddUserBadge(new AddUserBadgeCommand { UserId = _authenticationService.CurrentUser.Id, BadgeId = result.EarnedBadge.Id });
await _client.AddUserBadge(new AddUserBadgeCommand { UserId = _authenticationService.Id.Value, BadgeId = result.EarnedBadge.Id });
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public async Task<BadgeEligibilityResult> Handle(Query query, CancellationToken
var friendResult = await _client
.GetAllFriends(new GetAllFriendsQuery()
{
UserId = _authenticationService.CurrentUser.Id
UserId = _authenticationService.Id.Value
},
cancellationToken);

Expand All @@ -54,7 +54,7 @@ public async Task<BadgeEligibilityResult> Handle(Query query, CancellationToken
{
bool hasBadge = await _localContext.UserBadges
.Where(userBadge =>
userBadge.UserId == _authenticationService.CurrentUser.Id &&
userBadge.UserId == _authenticationService.Id.Value &&
userBadge.Badge.Name == badgeName)
.AnyAsync(cancellationToken);

Expand All @@ -80,7 +80,7 @@ private async Task AddBadgeToUser(
_localContext.UserBadges.Add(new UserBadge()
{
BadgeId = result.EarnedBadge.Id,
UserId = _authenticationService.CurrentUser.Id,
UserId = _authenticationService.Id.Value,
DateAcquired = DateTime.UtcNow
});

Expand All @@ -90,7 +90,7 @@ private async Task AddBadgeToUser(
await _client.AddUserBadge(new AddUserBadgeCommand()
{
BadgeId = result.EarnedBadge.Id,
UserId = _authenticationService.CurrentUser.Id
UserId = _authenticationService.Id.Value
},
cancellationToken);
}
Expand Down
12 changes: 6 additions & 6 deletions src/FocusApp.Client/Methods/Shop/PurchaseItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public async Task<Unit> Handle(Command command, CancellationToken cancellationTo
try
{
// Add the user's new pet to the local database
Shared.Models.User user = await _localContext.Users.FirstAsync(u => u.Id == _authenticationService.CurrentUser.Id, cancellationToken);
Shared.Models.User user = await _localContext.Users.FirstAsync(u => u.Id == _authenticationService.Id.Value, cancellationToken);
user.Pets?.Add(new UserPet
{
Pet = await _localContext.Pets.FirstAsync(p => p.Id == command.Item.Id, cancellationToken)
Expand All @@ -59,7 +59,7 @@ public async Task<Unit> Handle(Command command, CancellationToken cancellationTo
{
await _client.AddUserPet(new AddUserPetCommand
{
UserId = _authenticationService.CurrentUser.Id,
UserId = _authenticationService.Id.Value,
PetId = command.Item.Id,
UpdatedBalance = _authenticationService.Balance,
});
Expand All @@ -75,7 +75,7 @@ await _client.AddUserPet(new AddUserPetCommand
// Add the user's new decor to the local database
try
{
Shared.Models.User user = await _localContext.Users.FirstAsync(u => u.Id == _authenticationService.CurrentUser.Id, cancellationToken);
Shared.Models.User user = await _localContext.Users.FirstAsync(u => u.Id == _authenticationService.Id.Value, cancellationToken);
user.Decor?.Add(new UserDecor
{
Decor = await _localContext.Decor.FirstAsync(d => d.Id == command.Item.Id, cancellationToken)
Expand All @@ -95,7 +95,7 @@ await _client.AddUserPet(new AddUserPetCommand
{
await _client.AddUserDecor(new AddUserDecorCommand
{
UserId = _authenticationService.CurrentUser.Id,
UserId = _authenticationService.Id.Value,
DecorId = command.Item.Id,
UpdatedBalance = _authenticationService.Balance,
});
Expand All @@ -110,7 +110,7 @@ await _client.AddUserDecor(new AddUserDecorCommand
try
{
// Add the user's new island to the local database
Shared.Models.User user = await _localContext.Users.FirstAsync(u => u.Id == _authenticationService.CurrentUser.Id, cancellationToken);
Shared.Models.User user = await _localContext.Users.FirstAsync(u => u.Id == _authenticationService.Id.Value, cancellationToken);
user.Islands?.Add(new UserIsland
{
Island = await _localContext.Islands.FirstAsync(i => i.Id == command.Item.Id, cancellationToken)
Expand All @@ -129,7 +129,7 @@ await _client.AddUserDecor(new AddUserDecorCommand
{
await _client.AddUserIsland(new AddUserIslandCommand
{
UserId = _authenticationService.CurrentUser.Id,
UserId = _authenticationService.Id.Value,
IslandId = command.Item.Id,
UpdatedBalance = _authenticationService.Balance,
});
Expand Down
6 changes: 3 additions & 3 deletions src/FocusApp.Client/Methods/User/EditUserProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public async Task<MediatrResult> Handle(EditUserProfileCommand command, Cancella
user.ProfilePicture = command.ProfilePicture == null ? user.ProfilePicture : command.ProfilePicture;

// Update the authentication service to reflect the new changes in the current session
_authenticationService.CurrentUser.UserName = user.UserName;
_authenticationService.CurrentUser.Pronouns = user.Pronouns;
_authenticationService.CurrentUser.ProfilePicture = user.ProfilePicture;
_authenticationService.UserName = user.UserName;
_authenticationService.Pronouns = user.Pronouns;
_authenticationService.ProfilePicture = user.ProfilePicture;

await _localContext.SaveChangesAsync(cancellationToken);
}
Expand Down
8 changes: 0 additions & 8 deletions src/FocusApp.Client/Views/LoginPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,6 @@ private async Task InitializeEmptyUser()
_authenticationService.ClearUser();
}

_authenticationService.CurrentUser ??= new User()
{
Auth0Id = "",
Email = "",
UserName = "",
Balance = 0
};

// Add the initial/default island and pet if they don't exist
if (_authenticationService.SelectedIsland is null || _authenticationService.SelectedPet is null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public SettingsAboutPopupInterface(Helpers.PopupService popupService)
StrokeShape = new RoundRectangle() { CornerRadius = new CornerRadius(20, 20, 20, 20) },
BackgroundColor = AppStyles.Palette.LightMauve,
WidthRequest = 360,
HeightRequest = 460,
HeightRequest = 300,
Content = new Grid()
{
RowDefinitions = GridRowsColumns.Rows.Define(
Expand Down Expand Up @@ -69,10 +69,12 @@ public SettingsAboutPopupInterface(Helpers.PopupService popupService)
{
Text = "Contact us",
FontSize = 20,
TextColor = Colors.Black
}.Margins(bottom: 2),
new Label()
{
Text = "[email protected]",
TextColor = Colors.Black,
FontSize = 17
},
}
Expand Down
6 changes: 3 additions & 3 deletions src/FocusApp.Client/Views/Shop/ShopItemPopupInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,16 +186,16 @@ private async Task<bool> UserOwnsItem()
case ShopItemType.Pets:
return await _localContext.UserPets.AnyAsync(p =>
p.PetId == _currentItem.Id
&& p.UserId == _authenticationService.CurrentUser.Id);
&& p.UserId == _authenticationService.Id.Value);
case ShopItemType.Decor:
return await _localContext.UserDecor.AnyAsync(d =>
d.DecorId == _currentItem.Id
&& d.UserId == _authenticationService.CurrentUser.Id);
&& d.UserId == _authenticationService.Id.Value);

case ShopItemType.Islands:
return await _localContext.UserIslands.AnyAsync(i =>
i.IslandId == _currentItem.Id
&& i.UserId == _authenticationService.CurrentUser.Id);
&& i.UserId == _authenticationService.Id.Value);

default:
return false;
Expand Down
Loading

0 comments on commit de8470c

Please sign in to comment.