Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement pagination for rankings overlay #31189

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions osu.Game.Tests/Visual/Online/TestSceneRankingsOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,32 @@ public void TestShowCountry()
AddStep("Show US", () => rankingsOverlay.ShowCountry(CountryCode.US));
}

[Test]
public void TestPageSelection()
{
AddStep("Set scope to performance", () => scope.Value = RankingsScope.Performance);
AddStep("Move to next page", () => rankingsOverlay.Header.CurrentPage.Value += 1);
AddStep("Switch to another scope", () => scope.Value = RankingsScope.Score);
AddAssert("Check page is first one", () => rankingsOverlay.Header.CurrentPage.Value == 0);
AddStep("Move to next page", () => rankingsOverlay.Header.CurrentPage.Value += 1);
AddStep("Switch to another ruleset", () => rankingsOverlay.Header.Ruleset.Value = new ManiaRuleset().RulesetInfo);
AddAssert("Check page is first one", () => rankingsOverlay.Header.CurrentPage.Value == 0);

AddStep("Set scope to kudosu", () => scope.Value = RankingsScope.Kudosu);
AddAssert("Check available pages is 20", () => rankingsOverlay.Header.AvailablesPages.Value == 20);
AddStep("Set scope to performance", () => scope.Value = RankingsScope.Performance);
AddAssert("Check available pages is 200", () => rankingsOverlay.Header.AvailablesPages.Value == 200);
}

private void loadRankingsOverlay()
{
Child = rankingsOverlay = new TestRankingsOverlay
{
Country = { BindTarget = countryBindable },
Header = { Current = { BindTarget = scope } },
State = { Value = Visibility.Visible },
};

countryBindable.BindTo(rankingsOverlay.Country);
scope.BindTo(rankingsOverlay.Header.Current);
}

private partial class TestRankingsOverlay : RankingsOverlay
Expand Down
32 changes: 32 additions & 0 deletions osu.Game/Overlays/PageableTabbleOnlineOverlay.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

namespace osu.Game.Overlays
{
public abstract partial class PageableTabbleOnlineOverlay<THeader, TEnum> : TabbableOnlineOverlay<THeader, TEnum>
where THeader : PagedTabControlOverlayHeader<TEnum>
{
protected PageableTabbleOnlineOverlay(OverlayColourScheme colourScheme)
: base(colourScheme)
{
}

protected override void LoadComplete()
{
base.LoadComplete();
Header.CurrentPage.BindValueChanged(page => OnPageChanged(page.NewValue));
}

protected override void OnTabChanged(TEnum tab)
{
// Go back to first page if we switch to another tab
Header.CurrentPage.SetDefault();
base.OnTabChanged(tab);
}

protected virtual void OnPageChanged(int page)
{
base.OnTabChanged(Header.Current.Value);
}
}
}
43 changes: 43 additions & 0 deletions osu.Game/Overlays/PagedTabControlOverlayHeader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

#nullable disable

using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Game.Graphics.UserInterface.PageSelector;

namespace osu.Game.Overlays
{
/// <inheritdoc />
/// <summary>
/// An extended overlay header that add a pagination support for a <see cref="TabControlOverlayHeader{T}" />
/// </summary>
/// <typeparam name="TEnum"></typeparam>
public abstract partial class PagedTabControlOverlayHeader<TEnum> : TabControlOverlayHeader<TEnum>
{
private readonly PageSelector pageSelector;

public BindableInt CurrentPage => pageSelector.CurrentPage;
public BindableInt AvailablesPages => pageSelector.AvailablePages;

protected PagedTabControlOverlayHeader()
{
HeaderInfo.Add(
pageSelector = new PageSelector
{
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Margin = new MarginPadding { Vertical = 15 },
});
}

public void ShowPageSelector(bool visible)
{
if (visible)
pageSelector.Show();
else
pageSelector.Hide();
}
}
}
2 changes: 1 addition & 1 deletion osu.Game/Overlays/Rankings/RankingsOverlayHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace osu.Game.Overlays.Rankings
{
public partial class RankingsOverlayHeader : TabControlOverlayHeader<RankingsScope>
public partial class RankingsOverlayHeader : PagedTabControlOverlayHeader<RankingsScope>
{
public Bindable<RulesetInfo> Ruleset => rulesetSelector.Current;

Expand Down
37 changes: 27 additions & 10 deletions osu.Game/Overlays/RankingsOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@

namespace osu.Game.Overlays
{
public partial class RankingsOverlay : TabbableOnlineOverlay<RankingsOverlayHeader, RankingsScope>
public partial class RankingsOverlay : PageableTabbleOnlineOverlay<RankingsOverlayHeader, RankingsScope>
{
protected Bindable<CountryCode> Country => Header.Country;

// First page is 0, need to apply +1 to get the right data actually.
private int currentPage => Header.CurrentPage.Value + 1;

private APIRequest lastRequest;

[Resolved]
Expand All @@ -45,7 +48,12 @@ protected override void LoadComplete()
{
// if a country is requested, force performance scope.
if (!Country.IsDefault)
{
Header.Current.Value = RankingsScope.Performance;
}

// Hide page selection with country filter
Header.ShowPageSelector(Country.IsDefault);

Scheduler.AddOnce(triggerTabChanged);
});
Expand Down Expand Up @@ -78,12 +86,21 @@ protected override void OnTabChanged(RankingsScope tab)
if (Header.Current.Value != RankingsScope.Performance)
Country.SetDefault();

// Kudosu scope have only 20 fetchable pages.
Header.AvailablesPages.Value = tab == RankingsScope.Kudosu ? 20 : 200;

// Hide page selection for spotlights scope
Header.ShowPageSelector(tab != RankingsScope.Spotlights);

Scheduler.AddOnce(triggerTabChanged);
}

private void triggerTabChanged() => base.OnTabChanged(Header.Current.Value);

protected override RankingsOverlayHeader CreateHeader() => new RankingsOverlayHeader();
protected override RankingsOverlayHeader CreateHeader() => new RankingsOverlayHeader
{
AvailablesPages = { Value = 200 }
};

public void ShowCountry(CountryCode requested)
{
Expand Down Expand Up @@ -128,16 +145,16 @@ private APIRequest createScopedRequest()
switch (Header.Current.Value)
{
case RankingsScope.Performance:
return new GetUserRankingsRequest(ruleset.Value, countryCode: Country.Value);
return new GetUserRankingsRequest(ruleset.Value, page: currentPage, countryCode: Country.Value);

case RankingsScope.Country:
return new GetCountryRankingsRequest(ruleset.Value);
return new GetCountryRankingsRequest(ruleset.Value, page: currentPage);

case RankingsScope.Score:
return new GetUserRankingsRequest(ruleset.Value, UserRankingsType.Score);
return new GetUserRankingsRequest(ruleset.Value, UserRankingsType.Score, page: currentPage);

case RankingsScope.Kudosu:
return new GetKudosuRankingsRequest();
return new GetKudosuRankingsRequest(page: currentPage);
}

return null;
Expand All @@ -154,10 +171,10 @@ private Drawable createTableFromResponse(APIRequest request)
switch (userRequest.Type)
{
case UserRankingsType.Performance:
return new PerformanceTable(1, userRequest.Response.Users);
return new PerformanceTable(currentPage, userRequest.Response.Users);

case UserRankingsType.Score:
return new ScoresTable(1, userRequest.Response.Users);
return new ScoresTable(currentPage, userRequest.Response.Users);
}

return null;
Expand All @@ -167,14 +184,14 @@ private Drawable createTableFromResponse(APIRequest request)
if (countryRequest.Response == null)
return null;

return new CountriesTable(1, countryRequest.Response.Countries);
return new CountriesTable(currentPage, countryRequest.Response.Countries);
}

case GetKudosuRankingsRequest kudosuRequest:
if (kudosuRequest.Response == null)
return null;

return new KudosuTable(1, kudosuRequest.Response.Users);
return new KudosuTable(currentPage, kudosuRequest.Response.Users);
}

return null;
Expand Down