1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 21:02:55 +08:00

Refactor PaginatedContainer to centralise repetitive logic

This commit is contained in:
Andrei Zavatski 2019-08-23 14:11:21 +03:00
parent 9616eedcfc
commit 050130e159
6 changed files with 110 additions and 193 deletions

View File

@ -1,21 +1,22 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System.Linq;
using System.Collections.Generic;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Overlays.Direct;
using osu.Game.Users;
using osuTK;
namespace osu.Game.Overlays.Profile.Sections.Beatmaps
{
public class PaginatedBeatmapContainer : PaginatedContainer
public class PaginatedBeatmapContainer : PaginatedContainer<APIBeatmapSet>
{
private const float panel_padding = 10f;
private readonly BeatmapSetType type;
private GetUserBeatmapsRequest request;
public PaginatedBeatmapContainer(BeatmapSetType type, Bindable<User> user, string header, string missing = "None... yet.")
: base(user, header, missing)
@ -27,40 +28,13 @@ namespace osu.Game.Overlays.Profile.Sections.Beatmaps
ItemsContainer.Spacing = new Vector2(panel_padding);
}
protected override void ShowMore()
protected override APIRequest<List<APIBeatmapSet>> CreateRequest()
=> new GetUserBeatmapsRequest(User.Value.Id, type, VisiblePages++, ItemsPerPage);
protected override Drawable CreateDrawableItem(APIBeatmapSet item) => new DirectGridPanel(item.ToBeatmapSet(Rulesets))
{
request = new GetUserBeatmapsRequest(User.Value.Id, type, VisiblePages++, ItemsPerPage);
request.Success += sets => Schedule(() =>
{
MoreButton.FadeTo(sets.Count == ItemsPerPage ? 1 : 0);
MoreButton.IsLoading = false;
if (!sets.Any() && VisiblePages == 1)
{
MissingText.Show();
return;
}
foreach (var s in sets)
{
if (!s.OnlineBeatmapSetID.HasValue)
continue;
ItemsContainer.Add(new DirectGridPanel(s.ToBeatmapSet(Rulesets))
{
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
});
}
});
Api.Queue(request);
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
request?.Cancel();
}
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
};
}
}

View File

@ -1,19 +1,19 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System.Linq;
using System.Collections.Generic;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Users;
namespace osu.Game.Overlays.Profile.Sections.Historical
{
public class PaginatedMostPlayedBeatmapContainer : PaginatedContainer
public class PaginatedMostPlayedBeatmapContainer : PaginatedContainer<APIUserMostPlayedBeatmap>
{
private GetUserMostPlayedBeatmapsRequest request;
public PaginatedMostPlayedBeatmapContainer(Bindable<User> user)
: base(user, "Most Played Beatmaps", "No records. :(")
{
@ -22,35 +22,10 @@ namespace osu.Game.Overlays.Profile.Sections.Historical
ItemsContainer.Direction = FillDirection.Vertical;
}
protected override void ShowMore()
{
request = new GetUserMostPlayedBeatmapsRequest(User.Value.Id, VisiblePages++, ItemsPerPage);
request.Success += beatmaps => Schedule(() =>
{
MoreButton.FadeTo(beatmaps.Count == ItemsPerPage ? 1 : 0);
MoreButton.IsLoading = false;
protected override APIRequest<List<APIUserMostPlayedBeatmap>> CreateRequest()
=> new GetUserMostPlayedBeatmapsRequest(User.Value.Id, VisiblePages++, ItemsPerPage);
if (!beatmaps.Any() && VisiblePages == 1)
{
MissingText.Show();
return;
}
MissingText.Hide();
foreach (var beatmap in beatmaps)
{
ItemsContainer.Add(new DrawableMostPlayedBeatmap(beatmap.GetBeatmapInfo(Rulesets), beatmap.PlayCount));
}
});
Api.Queue(request);
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
request?.Cancel();
}
protected override Drawable CreateDrawableItem(APIUserMostPlayedBeatmap item)
=> new DrawableMostPlayedBeatmap(item.GetBeatmapInfo(Rulesets), item.PlayCount);
}
}

View File

@ -4,48 +4,24 @@
using osu.Framework.Graphics;
using osu.Game.Online.API.Requests;
using osu.Game.Users;
using System.Linq;
using osu.Framework.Bindables;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Online.API;
using System.Collections.Generic;
namespace osu.Game.Overlays.Profile.Sections.Kudosu
{
public class PaginatedKudosuHistoryContainer : PaginatedContainer
public class PaginatedKudosuHistoryContainer : PaginatedContainer<APIKudosuHistory>
{
private GetUserKudosuHistoryRequest request;
public PaginatedKudosuHistoryContainer(Bindable<User> user, string header, string missing)
: base(user, header, missing)
{
ItemsPerPage = 5;
}
protected override void ShowMore()
{
request = new GetUserKudosuHistoryRequest(User.Value.Id, VisiblePages++, ItemsPerPage);
request.Success += items => Schedule(() =>
{
MoreButton.FadeTo(items.Count == ItemsPerPage ? 1 : 0);
MoreButton.IsLoading = false;
protected override APIRequest<List<APIKudosuHistory>> CreateRequest()
=> new GetUserKudosuHistoryRequest(User.Value.Id, VisiblePages++, ItemsPerPage);
if (!items.Any() && VisiblePages == 1)
{
MissingText.Show();
return;
}
MissingText.Hide();
foreach (var item in items)
ItemsContainer.Add(new DrawableKudosuHistoryItem(item));
});
Api.Queue(request);
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
request?.Cancel();
}
protected override Drawable CreateDrawableItem(APIKudosuHistory item) => new DrawableKudosuHistoryItem(item);
}
}

View File

@ -11,22 +11,25 @@ using osu.Game.Graphics.Sprites;
using osu.Game.Online.API;
using osu.Game.Rulesets;
using osu.Game.Users;
using System.Collections.Generic;
using System.Linq;
namespace osu.Game.Overlays.Profile.Sections
{
public abstract class PaginatedContainer : FillFlowContainer
public abstract class PaginatedContainer<T> : FillFlowContainer
{
protected readonly FillFlowContainer ItemsContainer;
protected readonly ShowMoreButton MoreButton;
protected readonly OsuSpriteText MissingText;
private readonly ShowMoreButton moreButton;
private readonly OsuSpriteText missingText;
private APIRequest<List<T>> retrievalRequest;
[Resolved]
private IAPIProvider api { get; set; }
protected int VisiblePages;
protected int ItemsPerPage;
protected readonly Bindable<User> User = new Bindable<User>();
protected IAPIProvider Api;
protected APIRequest RetrievalRequest;
protected readonly FillFlowContainer ItemsContainer;
protected RulesetStore Rulesets;
protected PaginatedContainer(Bindable<User> user, string header, string missing)
@ -51,15 +54,15 @@ namespace osu.Game.Overlays.Profile.Sections
RelativeSizeAxes = Axes.X,
Spacing = new Vector2(0, 2),
},
MoreButton = new ShowMoreButton
moreButton = new ShowMoreButton
{
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Alpha = 0,
Margin = new MarginPadding { Top = 10 },
Action = ShowMore,
Action = showMore,
},
MissingText = new OsuSpriteText
missingText = new OsuSpriteText
{
Font = OsuFont.GetFont(size: 15),
Text = missing,
@ -69,9 +72,8 @@ namespace osu.Game.Overlays.Profile.Sections
}
[BackgroundDependencyLoader]
private void load(IAPIProvider api, RulesetStore rulesets)
private void load(RulesetStore rulesets)
{
Api = api;
Rulesets = rulesets;
User.ValueChanged += onUserChanged;
@ -84,9 +86,51 @@ namespace osu.Game.Overlays.Profile.Sections
ItemsContainer.Clear();
if (e.NewValue != null)
ShowMore();
showMore();
}
protected abstract void ShowMore();
private void showMore()
{
retrievalRequest = CreateRequest();
retrievalRequest.Success += items => UpdateItems(items);
api.Queue(retrievalRequest);
}
protected virtual void UpdateItems(List<T> items)
{
Schedule(() =>
{
moreButton.FadeTo(items.Count == ItemsPerPage ? 1 : 0);
moreButton.IsLoading = false;
if (!items.Any() && VisiblePages == 1)
{
moreButton.Hide();
moreButton.IsLoading = false;
missingText.Show();
return;
}
LoadComponentsAsync(items.Select(item => CreateDrawableItem(item)), i =>
{
missingText.Hide();
moreButton.FadeTo(items.Count == ItemsPerPage ? 1 : 0);
moreButton.IsLoading = false;
ItemsContainer.AddRange(i);
});
});
}
protected abstract APIRequest<List<T>> CreateRequest();
protected abstract Drawable CreateDrawableItem(T item);
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
retrievalRequest?.Cancel();
}
}
}

View File

@ -5,18 +5,18 @@ using osu.Framework.Graphics.Containers;
using osu.Game.Online.API.Requests;
using osu.Game.Users;
using System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Game.Online.API.Requests.Responses;
using System.Collections.Generic;
using osu.Game.Online.API;
namespace osu.Game.Overlays.Profile.Sections.Ranks
{
public class PaginatedScoreContainer : PaginatedContainer
public class PaginatedScoreContainer : PaginatedContainer<APILegacyScoreInfo>
{
private readonly bool includeWeight;
private readonly ScoreType type;
private GetUserScoresRequest request;
public PaginatedScoreContainer(ScoreType type, Bindable<User> user, string header, string missing, bool includeWeight = false)
: base(user, header, missing)
@ -29,52 +29,27 @@ namespace osu.Game.Overlays.Profile.Sections.Ranks
ItemsContainer.Direction = FillDirection.Vertical;
}
protected override void ShowMore()
protected override void UpdateItems(List<APILegacyScoreInfo> items)
{
request = new GetUserScoresRequest(User.Value.Id, type, VisiblePages++, ItemsPerPage);
request.Success += scores => Schedule(() =>
{
foreach (var s in scores)
s.Ruleset = Rulesets.GetRuleset(s.RulesetID);
foreach (var item in items)
item.Ruleset = Rulesets.GetRuleset(item.RulesetID);
if (!scores.Any() && VisiblePages == 1)
{
MoreButton.Hide();
MoreButton.IsLoading = false;
MissingText.Show();
return;
}
IEnumerable<DrawableProfileScore> drawableScores;
switch (type)
{
default:
drawableScores = scores.Select(score => new DrawablePerformanceScore(score, includeWeight ? Math.Pow(0.95, ItemsContainer.Count) : (double?)null));
break;
case ScoreType.Recent:
drawableScores = scores.Select(score => new DrawableTotalScore(score));
break;
}
LoadComponentsAsync(drawableScores, s =>
{
MissingText.Hide();
MoreButton.FadeTo(scores.Count == ItemsPerPage ? 1 : 0);
MoreButton.IsLoading = false;
ItemsContainer.AddRange(s);
});
});
Api.Queue(request);
base.UpdateItems(items);
}
protected override void Dispose(bool isDisposing)
protected override APIRequest<List<APILegacyScoreInfo>> CreateRequest()
=> new GetUserScoresRequest(User.Value.Id, type, VisiblePages++, ItemsPerPage);
protected override Drawable CreateDrawableItem(APILegacyScoreInfo item)
{
base.Dispose(isDisposing);
request?.Cancel();
switch (type)
{
default:
return new DrawablePerformanceScore(item, includeWeight ? Math.Pow(0.95, ItemsContainer.Count) : (double?)null);
case ScoreType.Recent:
return new DrawableTotalScore(item);
}
}
}
}

View File

@ -4,51 +4,24 @@
using osu.Framework.Graphics;
using osu.Game.Online.API.Requests;
using osu.Game.Users;
using System.Linq;
using osu.Framework.Bindables;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Online.API;
using System.Collections.Generic;
namespace osu.Game.Overlays.Profile.Sections.Recent
{
public class PaginatedRecentActivityContainer : PaginatedContainer
public class PaginatedRecentActivityContainer : PaginatedContainer<APIRecentActivity>
{
private GetUserRecentActivitiesRequest request;
public PaginatedRecentActivityContainer(Bindable<User> user, string header, string missing)
: base(user, header, missing)
{
ItemsPerPage = 5;
}
protected override void ShowMore()
{
request = new GetUserRecentActivitiesRequest(User.Value.Id, VisiblePages++, ItemsPerPage);
request.Success += activities => Schedule(() =>
{
MoreButton.FadeTo(activities.Count == ItemsPerPage ? 1 : 0);
MoreButton.IsLoading = false;
protected override APIRequest<List<APIRecentActivity>> CreateRequest()
=> new GetUserRecentActivitiesRequest(User.Value.Id, VisiblePages++, ItemsPerPage);
if (!activities.Any() && VisiblePages == 1)
{
MissingText.Show();
return;
}
MissingText.Hide();
foreach (APIRecentActivity activity in activities)
{
ItemsContainer.Add(new DrawableRecentActivity(activity));
}
});
Api.Queue(request);
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
request?.Cancel();
}
protected override Drawable CreateDrawableItem(APIRecentActivity item) => new DrawableRecentActivity(item);
}
}