1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-13 16:13:34 +08:00

Merge pull request #25510 from Stedoss/login-state-changes-user-profile-overlay

Handle login API state changes in `UserProfileOverlay`
This commit is contained in:
Dean Herbert 2023-11-22 12:41:11 +09:00 committed by GitHub
commit 34a2545d9c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 67 additions and 11 deletions

View File

@ -78,6 +78,31 @@ namespace osu.Game.Tests.Visual.Online
AddStep("complete request", () => pendingRequest.TriggerSuccess(TEST_USER));
}
[Test]
public void TestLogin()
{
GetUserRequest pendingRequest = null!;
AddStep("set up request handling", () =>
{
dummyAPI.HandleRequest = req =>
{
if (dummyAPI.State.Value == APIState.Online && req is GetUserRequest getUserRequest)
{
pendingRequest = getUserRequest;
return true;
}
return false;
};
});
AddStep("logout", () => dummyAPI.Logout());
AddStep("show user", () => profile.ShowUser(new APIUser { Id = 1 }));
AddStep("login", () => dummyAPI.Login("username", "password"));
AddWaitStep("wait some", 3);
AddStep("complete request", () => pendingRequest.TriggerSuccess(TEST_USER));
}
public static readonly APIUser TEST_USER = new APIUser
{
Username = @"Somebody",

View File

@ -5,6 +5,7 @@ using System;
using System.Diagnostics;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Extensions.ObjectExtensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
@ -18,6 +19,7 @@ using osu.Game.Graphics.Cursor;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osu.Game.Online;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Overlays.Profile;
@ -42,6 +44,11 @@ namespace osu.Game.Overlays
private ProfileSectionsContainer? sectionsContainer;
private ProfileSectionTabControl? tabs;
private IUser? user;
private IRulesetInfo? ruleset;
private readonly IBindable<APIState> apiState = new Bindable<APIState>();
[Resolved]
private RulesetStore rulesets { get; set; } = null!;
@ -58,16 +65,36 @@ namespace osu.Game.Overlays
});
}
[BackgroundDependencyLoader]
private void load()
{
apiState.BindTo(API.State);
apiState.BindValueChanged(state => Schedule(() =>
{
if (state.NewValue == APIState.Online && user != null)
Scheduler.AddOnce(fetchAndSetContent);
}));
}
protected override ProfileHeader CreateHeader() => new ProfileHeader();
protected override Color4 BackgroundColour => ColourProvider.Background5;
public void ShowUser(IUser user, IRulesetInfo? ruleset = null)
public void ShowUser(IUser userToShow, IRulesetInfo? userRuleset = null)
{
if (user.OnlineID == APIUser.SYSTEM_USER_ID)
if (userToShow.OnlineID == APIUser.SYSTEM_USER_ID)
return;
user = userToShow;
ruleset = userRuleset;
Show();
Scheduler.AddOnce(fetchAndSetContent);
}
private void fetchAndSetContent()
{
Debug.Assert(user != null);
if (user.OnlineID == Header.User.Value?.User.Id && ruleset?.MatchesOnlineID(Header.User.Value?.Ruleset) == true)
return;
@ -143,24 +170,28 @@ namespace osu.Game.Overlays
sectionsContainer.ScrollToTop();
userReq = user.OnlineID > 1 ? new GetUserRequest(user.OnlineID, ruleset) : new GetUserRequest(user.Username, ruleset);
userReq.Success += u => userLoadComplete(u, ruleset);
API.Queue(userReq);
loadingLayer.Show();
if (API.State.Value != APIState.Offline)
{
userReq = user.OnlineID > 1 ? new GetUserRequest(user.OnlineID, ruleset) : new GetUserRequest(user.Username, ruleset);
userReq.Success += u => userLoadComplete(u, ruleset);
API.Queue(userReq);
loadingLayer.Show();
}
}
private void userLoadComplete(APIUser user, IRulesetInfo? ruleset)
private void userLoadComplete(APIUser loadedUser, IRulesetInfo? userRuleset)
{
Debug.Assert(sections != null && sectionsContainer != null && tabs != null);
var actualRuleset = rulesets.GetRuleset(ruleset?.ShortName ?? user.PlayMode).AsNonNull();
var actualRuleset = rulesets.GetRuleset(userRuleset?.ShortName ?? loadedUser.PlayMode).AsNonNull();
var userProfile = new UserProfileData(user, actualRuleset);
var userProfile = new UserProfileData(loadedUser, actualRuleset);
Header.User.Value = userProfile;
if (user.ProfileOrder != null)
if (loadedUser.ProfileOrder != null)
{
foreach (string id in user.ProfileOrder)
foreach (string id in loadedUser.ProfileOrder)
{
var sec = sections.FirstOrDefault(s => s.Identifier == id);