Skip to content

Commit

Permalink
Merge pull request #114 from ChrispyPeaches/User-Profile-Selected-Ite…
Browse files Browse the repository at this point in the history
…m-Fixes

[HOTFIX] User profile selected item fixes / Adjust styling / Connect selected item buttons to correct pages
  • Loading branch information
JohnParks032 authored May 3, 2024
2 parents 14588a4 + 99f9b53 commit b0dbd17
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 108 deletions.
61 changes: 61 additions & 0 deletions src/FocusApp.Client/Helpers/PopupService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ public void ShowPopup<T>() where T : Popup
_popups.Push(popup);
}

/// <summary>
/// Show popup.
/// </summary>
/// <typeparam name="T">Popup</typeparam>
/// <exception cref="MissingMethodException"></exception>
public async Task ShowPopupAsync<T>() where T : Popup
{
var mainPage = App.Current?.MainPage ?? throw new MissingMethodException("Main page is null");
var popup = _services.GetRequiredService<T>();
await MainThread.InvokeOnMainThreadAsync(() => mainPage.ShowPopup<T>(popup));
_popups.Push(popup);
}

/// <summary>
/// Show popup and return the object for populating with shop data
/// </summary>
Expand Down Expand Up @@ -73,6 +86,54 @@ public void HidePopup(bool wasDismissedByTappingOutsideOfPopup = false)
}
}

public void HidePopup<T>(bool wasDismissedByTappingOutsideOfPopup = false) where T : BasePopup
{
if (_popups.Count > 0)
{
if (typeof(T) == _popups.Peek().GetType())
{
// Quick patch for popups closed via tapping outside of the popup
if (wasDismissedByTappingOutsideOfPopup)
// Remove the popup from the stack, but do not close it, as it has already
// been closed/disposed via tapping outside of popup
_popups.Pop();
else
MainThread.BeginInvokeOnMainThread(() => _popups.Pop().Close());
}
}
}

public async Task HidePopupAsync(bool wasDismissedByTappingOutsideOfPopup = false)
{
if (_popups.Count > 0)
{
// Quick patch for popups closed via tapping outside of the popup
if (wasDismissedByTappingOutsideOfPopup)
// Remove the popup from the stack, but do not close it, as it has already
// been closed/disposed via tapping outside of popup
_popups.Pop();
else
await MainThread.InvokeOnMainThreadAsync(() => _popups.Pop().Close());
}
}

public async Task HidePopupAsync<T>(bool wasDismissedByTappingOutsideOfPopup = false) where T : BasePopup
{
if (_popups.Count > 0)
{
if (typeof(T) == _popups.Peek().GetType())
{
// Quick patch for popups closed via tapping outside of the popup
if (wasDismissedByTappingOutsideOfPopup)
// Remove the popup from the stack, but do not close it, as it has already
// been closed/disposed via tapping outside of popup
_popups.Pop();
else
await MainThread.InvokeOnMainThreadAsync(() => _popups.Pop().Close());
}
}
}

public void HideAllPopups()
{
while (_popups.Count > 0)
Expand Down
4 changes: 2 additions & 2 deletions src/FocusApp.Client/Methods/User/GetUserLogin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public async Task<Result> Handle(
user = await GatherUserDataForCreatedUser(createUserResponse, auth0UserId, userEmail, userName, cancellationToken);

bool userExistsLocally = await _localContext.Users
.AnyAsync(u => u.Auth0Id == auth0UserId, cancellationToken);
.AnyAsync(u => u.Auth0Id == auth0UserId || u.Id == user.Id, cancellationToken);

// Add user to the local database if the user doesn't exist locally
if (!userExistsLocally)
Expand Down Expand Up @@ -250,7 +250,7 @@ await GetSelectedDecorQuery(user.SelectedDecorId.Value)
.FirstOrDefaultAsync(cancellationToken);

bool userExistsLocally = await _localContext.Users
.AnyAsync(u => u.Auth0Id == getUserResponse.User.Auth0Id, cancellationToken);
.AnyAsync(u => u.Auth0Id == getUserResponse.User.Auth0Id || getUserResponse.User.Id == u.Id, cancellationToken);

// Add user to the local database if the user doesn't exist locally
if (!userExistsLocally)
Expand Down
6 changes: 5 additions & 1 deletion src/FocusApp.Client/Platforms/Android/MainActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

namespace FocusApp.Client
{
[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
[Activity(
Theme = "@style/Maui.SplashTheme",
MainLauncher = true,
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density,
ScreenOrientation = ScreenOrientation.Portrait)]
public class MainActivity : MauiAppCompatActivity
{
}
Expand Down
1 change: 0 additions & 1 deletion src/FocusApp.Client/Views/LoginPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ private async Task InitializeEmptyUser()

_authenticationService.SelectedIsland ??= result.Island;
_authenticationService.SelectedPet ??= result.Pet;
_authenticationService.SelectedDecor ??= result.Decor;
}
}

Expand Down
8 changes: 0 additions & 8 deletions src/FocusApp.Client/Views/Social/BadgesPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,6 @@ protected override async void OnAppearing()
{
base.OnAppearing();

// Check if the user is logged in
if (string.IsNullOrEmpty(_authenticationService.AuthToken))
{
var loginPopup = (EnsureLoginPopupInterface)_popupService.ShowAndGetPopup<EnsureLoginPopupInterface>();
loginPopup.OriginPage = nameof(ShopPage);
return;
}

// Fetch badges from the local database and display them
var localBadges = await FetchBadgesFromLocalDb();
var userBadges = await FetchUserBadgesFromLocalDb();
Expand Down
99 changes: 47 additions & 52 deletions src/FocusApp.Client/Views/Social/LeaderboardsPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ namespace FocusApp.Client.Views.Social
{
internal class LeaderboardsPage : BasePage
{

public enum LeaderboardType
{
Daily,
Weekly
}
// Row / Column structure for entire page
enum PageRow { PageHeader, LeaderboardSelectors, TopThreeFriendsDisplay, RemainingFriendsDisplay, TabBarSpacer }
enum PageColumn { DailyLeadboardButton, WeeklyLeaderboardButton }
Expand Down Expand Up @@ -69,8 +75,8 @@ public LeaderboardsPage(IAPIClient client, IAuthenticationService authentication
}
.Row(PageRow.LeaderboardSelectors)
.Column(PageColumn.DailyLeadboardButton)
.Invoke(button => button.Released += (sender, eventArgs) =>
GetDailyLeaderboards(sender, eventArgs));
.Invoke(button => button.Released += (_, _) =>
_ = FetchAndDisplayLeaderboards(LeaderboardType.Daily));

// Weekly Leaderboard Button
_weeklyLeaderboardButton = new Button
Expand All @@ -81,16 +87,16 @@ public LeaderboardsPage(IAPIClient client, IAuthenticationService authentication
}
.Row(PageRow.LeaderboardSelectors)
.Column(PageColumn.WeeklyLeaderboardButton)
.Invoke(button => button.Released += (sender, eventArgs) =>
GetWeeklyLeaderboards(sender, eventArgs));
.Invoke(button => button.Released += (_, _) =>
_ = FetchAndDisplayLeaderboards(LeaderboardType.Weekly));

Content = new Grid
{
RowDefinitions = GridRowsColumns.Rows.Define(
(PageRow.PageHeader, GridRowsColumns.Stars(1)),
(PageRow.LeaderboardSelectors, GridRowsColumns.Stars(1)),
(PageRow.LeaderboardSelectors, GridRowsColumns.Stars(1.15)),
(PageRow.TopThreeFriendsDisplay, GridRowsColumns.Stars(4.5)),
(PageRow.RemainingFriendsDisplay, GridRowsColumns.Stars(3.5)),
(PageRow.RemainingFriendsDisplay, GridRowsColumns.Stars(2)),
(PageRow.TabBarSpacer, Consts.TabBarHeight)
),
ColumnDefinitions = GridRowsColumns.Columns.Define(
Expand Down Expand Up @@ -261,7 +267,7 @@ ScrollView GetRemainingFriendsScrollView()

Label friendName = new Label
{
FontSize = 24,
FontSize = 20,
VerticalOptions = LayoutOptions.Center,
}
.Column(RemainingFriendsColumn.Name);
Expand Down Expand Up @@ -340,7 +346,8 @@ void SetTopThreeDynamicElements()

_thirdPlaceUsername = new Label
{
Text = "Username"
Text = "Username",
FontSize = 12
}
.CenterHorizontal();

Expand Down Expand Up @@ -369,7 +376,8 @@ void SetTopThreeDynamicElements()
_secondPlaceUsername = new Label
{
Text = "Username",
TextColor = Colors.Black
TextColor = Colors.Black,
FontSize = 12
}
.CenterHorizontal();

Expand Down Expand Up @@ -398,7 +406,8 @@ void SetTopThreeDynamicElements()
_firstPlaceUsername = new Label
{
Text = "Username",
TextColor = Colors.Black
TextColor = Colors.Black,
FontSize = 12
}
.CenterHorizontal();

Expand All @@ -407,44 +416,6 @@ void SetTopThreeDynamicElements()
_firstPlaceUsername.Bind(Label.TextProperty, "UserName");
}

async void GetDailyLeaderboards(object sender, EventArgs e)
{
try
{
_popupService.ShowPopup<GenericLoadingPopupInterface>();
LeaderboardResponse leaderboardResponse = await _client.GetDailyLeaderboard(new GetDailyLeaderboardQuery { UserId = _authenticationService.CurrentUser.Id }, default);
PopulateLeaderboard(leaderboardResponse.LeaderboardRecords);

// Disable the daily leaderboards button, and enable the weekly leaderboards button
_dailyLeaderboardButton.IsEnabled = false;
_weeklyLeaderboardButton.IsEnabled = true;
}
catch (Exception ex)
{
_logger.Log(LogLevel.Error, "Error retreiving daily leaderboards. Message: " + ex.Message);
}
_popupService.HidePopup();
}

async void GetWeeklyLeaderboards(object sender, EventArgs e)
{
try
{
_popupService.ShowPopup<GenericLoadingPopupInterface>();
LeaderboardResponse leaderboardResponse = await _client.GetWeeklyLeaderboard(new GetWeeklyLeaderboardQuery { UserId = _authenticationService.CurrentUser.Id }, default);
PopulateLeaderboard(leaderboardResponse.LeaderboardRecords);

// Disable the weekly leaderboards button, and enable the daily leaderboards button
_weeklyLeaderboardButton.IsEnabled = false;
_dailyLeaderboardButton.IsEnabled = true;
}
catch (Exception ex)
{
_logger.Log(LogLevel.Error, "Error retreiving weekly leaderboards. Message: " + ex.Message);
}
_popupService.HidePopup();
}

void PopulateLeaderboard(List<LeaderboardDto> leaderboard)
{
ClearLeaderboard();
Expand Down Expand Up @@ -510,15 +481,39 @@ protected override async void OnAppearing()
{
base.OnAppearing();

// On page load, fetch daily leaderboards
await Task.Run(() => FetchAndDisplayLeaderboards(LeaderboardType.Daily));
}

/// <summary>
/// Show loading popup, retrieve leaderboards from API, then populate the UI with leaderboard data
/// </summary>
private async Task FetchAndDisplayLeaderboards(LeaderboardType leaderboardType)
{
try
{
LeaderboardResponse leaderboardResponse = await _client.GetDailyLeaderboard(new GetDailyLeaderboardQuery { UserId = _authenticationService.CurrentUser.Id }, default);
PopulateLeaderboard(leaderboardResponse.LeaderboardRecords);
await _popupService.ShowPopupAsync<GenericLoadingPopupInterface>();
LeaderboardResponse leaderboardResponse = null;
if (leaderboardType == LeaderboardType.Daily)
{
leaderboardResponse = await _client.GetDailyLeaderboard(new GetDailyLeaderboardQuery { UserId = _authenticationService.CurrentUser.Id }, default);
}
else
{
leaderboardResponse = await _client.GetWeeklyLeaderboard(new GetWeeklyLeaderboardQuery { UserId = _authenticationService.CurrentUser.Id }, default);
}

await MainThread.InvokeOnMainThreadAsync(() =>
{
PopulateLeaderboard(leaderboardResponse.LeaderboardRecords);
_dailyLeaderboardButton.IsEnabled = leaderboardType != LeaderboardType.Daily;
_weeklyLeaderboardButton.IsEnabled = leaderboardType != LeaderboardType.Weekly;
});

await _popupService.HidePopupAsync();
}
catch (Exception ex)
{
_logger.Log(LogLevel.Error, "Error retreiving daily leaderboards on page load. Message: " + ex.Message);
_logger.LogError(ex, "Error retrieving {leaderboardType} leaderboards.", leaderboardType);
}
}

Expand Down
Loading

0 comments on commit b0dbd17

Please sign in to comment.