Merge pull request #5858 from EVAST9919/simplify-paginated-container

Refactor PaginatedContainer to avoid code duplication
This commit is contained in:
Dan Balasescu 2019-08-28 13:50:17 +09:00 committed by GitHub
commit af7b2190c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 106 additions and 162 deletions

View File

@ -1,21 +1,22 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // 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.Bindables;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests; using osu.Game.Online.API.Requests;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Overlays.Direct; using osu.Game.Overlays.Direct;
using osu.Game.Users; using osu.Game.Users;
using osuTK; using osuTK;
namespace osu.Game.Overlays.Profile.Sections.Beatmaps namespace osu.Game.Overlays.Profile.Sections.Beatmaps
{ {
public class PaginatedBeatmapContainer : PaginatedContainer public class PaginatedBeatmapContainer : PaginatedContainer<APIBeatmapSet>
{ {
private const float panel_padding = 10f; private const float panel_padding = 10f;
private readonly BeatmapSetType type; private readonly BeatmapSetType type;
private GetUserBeatmapsRequest request;
public PaginatedBeatmapContainer(BeatmapSetType type, Bindable<User> user, string header, string missing = "None... yet.") public PaginatedBeatmapContainer(BeatmapSetType type, Bindable<User> user, string header, string missing = "None... yet.")
: base(user, header, missing) : base(user, header, missing)
@ -27,40 +28,15 @@ namespace osu.Game.Overlays.Profile.Sections.Beatmaps
ItemsContainer.Spacing = new Vector2(panel_padding); ItemsContainer.Spacing = new Vector2(panel_padding);
} }
protected override void ShowMore() protected override APIRequest<List<APIBeatmapSet>> CreateRequest() =>
{ new GetUserBeatmapsRequest(User.Value.Id, type, VisiblePages++, ItemsPerPage);
request = new GetUserBeatmapsRequest(User.Value.Id, type, VisiblePages++, ItemsPerPage);
request.Success += sets => Schedule(() => protected override Drawable CreateDrawableItem(APIBeatmapSet model) => !model.OnlineBeatmapSetID.HasValue
? null
: new DirectGridPanel(model.ToBeatmapSet(Rulesets))
{ {
MoreButton.FadeTo(sets.Count == ItemsPerPage ? 1 : 0); Anchor = Anchor.TopCentre,
MoreButton.IsLoading = false; Origin = Anchor.TopCentre,
};
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();
}
} }
} }

View File

@ -1,19 +1,19 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // 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.Bindables;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests; using osu.Game.Online.API.Requests;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Users; using osu.Game.Users;
namespace osu.Game.Overlays.Profile.Sections.Historical namespace osu.Game.Overlays.Profile.Sections.Historical
{ {
public class PaginatedMostPlayedBeatmapContainer : PaginatedContainer public class PaginatedMostPlayedBeatmapContainer : PaginatedContainer<APIUserMostPlayedBeatmap>
{ {
private GetUserMostPlayedBeatmapsRequest request;
public PaginatedMostPlayedBeatmapContainer(Bindable<User> user) public PaginatedMostPlayedBeatmapContainer(Bindable<User> user)
: base(user, "Most Played Beatmaps", "No records. :(") : base(user, "Most Played Beatmaps", "No records. :(")
{ {
@ -22,35 +22,10 @@ namespace osu.Game.Overlays.Profile.Sections.Historical
ItemsContainer.Direction = FillDirection.Vertical; ItemsContainer.Direction = FillDirection.Vertical;
} }
protected override void ShowMore() protected override APIRequest<List<APIUserMostPlayedBeatmap>> CreateRequest() =>
{ new GetUserMostPlayedBeatmapsRequest(User.Value.Id, VisiblePages++, ItemsPerPage);
request = new GetUserMostPlayedBeatmapsRequest(User.Value.Id, VisiblePages++, ItemsPerPage);
request.Success += beatmaps => Schedule(() =>
{
MoreButton.FadeTo(beatmaps.Count == ItemsPerPage ? 1 : 0);
MoreButton.IsLoading = false;
if (!beatmaps.Any() && VisiblePages == 1) protected override Drawable CreateDrawableItem(APIUserMostPlayedBeatmap model) =>
{ new DrawableMostPlayedBeatmap(model.GetBeatmapInfo(Rulesets), model.PlayCount);
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();
}
} }
} }

View File

@ -11,22 +11,27 @@ using osu.Game.Graphics.Sprites;
using osu.Game.Online.API; using osu.Game.Online.API;
using osu.Game.Rulesets; using osu.Game.Rulesets;
using osu.Game.Users; using osu.Game.Users;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace osu.Game.Overlays.Profile.Sections namespace osu.Game.Overlays.Profile.Sections
{ {
public abstract class PaginatedContainer : FillFlowContainer public abstract class PaginatedContainer<TModel> : FillFlowContainer
{ {
protected readonly FillFlowContainer ItemsContainer; private readonly ShowMoreButton moreButton;
protected readonly ShowMoreButton MoreButton; private readonly OsuSpriteText missingText;
protected readonly OsuSpriteText MissingText; private APIRequest<List<TModel>> retrievalRequest;
private CancellationTokenSource loadCancellation;
[Resolved]
private IAPIProvider api { get; set; }
protected int VisiblePages; protected int VisiblePages;
protected int ItemsPerPage; protected int ItemsPerPage;
protected readonly Bindable<User> User = new Bindable<User>(); protected readonly Bindable<User> User = new Bindable<User>();
protected readonly FillFlowContainer ItemsContainer;
protected IAPIProvider Api;
protected APIRequest RetrievalRequest;
protected RulesetStore Rulesets; protected RulesetStore Rulesets;
protected PaginatedContainer(Bindable<User> user, string header, string missing) protected PaginatedContainer(Bindable<User> user, string header, string missing)
@ -51,15 +56,15 @@ namespace osu.Game.Overlays.Profile.Sections
RelativeSizeAxes = Axes.X, RelativeSizeAxes = Axes.X,
Spacing = new Vector2(0, 2), Spacing = new Vector2(0, 2),
}, },
MoreButton = new ShowMoreButton moreButton = new ShowMoreButton
{ {
Anchor = Anchor.TopCentre, Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre, Origin = Anchor.TopCentre,
Alpha = 0, Alpha = 0,
Margin = new MarginPadding { Top = 10 }, Margin = new MarginPadding { Top = 10 },
Action = ShowMore, Action = showMore,
}, },
MissingText = new OsuSpriteText missingText = new OsuSpriteText
{ {
Font = OsuFont.GetFont(size: 15), Font = OsuFont.GetFont(size: 15),
Text = missing, Text = missing,
@ -69,9 +74,8 @@ namespace osu.Game.Overlays.Profile.Sections
} }
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(IAPIProvider api, RulesetStore rulesets) private void load(RulesetStore rulesets)
{ {
Api = api;
Rulesets = rulesets; Rulesets = rulesets;
User.ValueChanged += onUserChanged; User.ValueChanged += onUserChanged;
@ -80,13 +84,54 @@ namespace osu.Game.Overlays.Profile.Sections
private void onUserChanged(ValueChangedEvent<User> e) private void onUserChanged(ValueChangedEvent<User> e)
{ {
loadCancellation?.Cancel();
retrievalRequest?.Cancel();
VisiblePages = 0; VisiblePages = 0;
ItemsContainer.Clear(); ItemsContainer.Clear();
if (e.NewValue != null) if (e.NewValue != null)
ShowMore(); showMore();
} }
protected abstract void ShowMore(); private void showMore()
{
loadCancellation = new CancellationTokenSource();
retrievalRequest = CreateRequest();
retrievalRequest.Success += UpdateItems;
api.Queue(retrievalRequest);
}
protected virtual void UpdateItems(List<TModel> items) => Schedule(() =>
{
if (!items.Any() && VisiblePages == 1)
{
moreButton.Hide();
moreButton.IsLoading = false;
missingText.Show();
return;
}
LoadComponentsAsync(items.Select(CreateDrawableItem).Where(d => d != null), drawables =>
{
missingText.Hide();
moreButton.FadeTo(items.Count == ItemsPerPage ? 1 : 0);
moreButton.IsLoading = false;
ItemsContainer.AddRange(drawables);
}, loadCancellation.Token);
});
protected abstract APIRequest<List<TModel>> CreateRequest();
protected abstract Drawable CreateDrawableItem(TModel model);
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.Online.API.Requests;
using osu.Game.Users; using osu.Game.Users;
using System; using System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Graphics; 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 namespace osu.Game.Overlays.Profile.Sections.Ranks
{ {
public class PaginatedScoreContainer : PaginatedContainer public class PaginatedScoreContainer : PaginatedContainer<APILegacyScoreInfo>
{ {
private readonly bool includeWeight; private readonly bool includeWeight;
private readonly ScoreType type; private readonly ScoreType type;
private GetUserScoresRequest request;
public PaginatedScoreContainer(ScoreType type, Bindable<User> user, string header, string missing, bool includeWeight = false) public PaginatedScoreContainer(ScoreType type, Bindable<User> user, string header, string missing, bool includeWeight = false)
: base(user, header, missing) : base(user, header, missing)
@ -29,52 +29,27 @@ namespace osu.Game.Overlays.Profile.Sections.Ranks
ItemsContainer.Direction = FillDirection.Vertical; ItemsContainer.Direction = FillDirection.Vertical;
} }
protected override void ShowMore() protected override void UpdateItems(List<APILegacyScoreInfo> items)
{ {
request = new GetUserScoresRequest(User.Value.Id, type, VisiblePages++, ItemsPerPage); foreach (var item in items)
request.Success += scores => Schedule(() => item.Ruleset = Rulesets.GetRuleset(item.RulesetID);
{
foreach (var s in scores)
s.Ruleset = Rulesets.GetRuleset(s.RulesetID);
if (!scores.Any() && VisiblePages == 1) base.UpdateItems(items);
{
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);
} }
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 model)
{ {
base.Dispose(isDisposing); switch (type)
request?.Cancel(); {
default:
return new DrawablePerformanceScore(model, includeWeight ? Math.Pow(0.95, ItemsContainer.Count) : (double?)null);
case ScoreType.Recent:
return new DrawableTotalScore(model);
}
} }
} }
} }

View File

@ -4,51 +4,24 @@
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Game.Online.API.Requests; using osu.Game.Online.API.Requests;
using osu.Game.Users; using osu.Game.Users;
using System.Linq;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Game.Online.API.Requests.Responses; using osu.Game.Online.API.Requests.Responses;
using osu.Game.Online.API;
using System.Collections.Generic;
namespace osu.Game.Overlays.Profile.Sections.Recent 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) public PaginatedRecentActivityContainer(Bindable<User> user, string header, string missing)
: base(user, header, missing) : base(user, header, missing)
{ {
ItemsPerPage = 5; ItemsPerPage = 5;
} }
protected override void ShowMore() protected override APIRequest<List<APIRecentActivity>> CreateRequest() =>
{ new GetUserRecentActivitiesRequest(User.Value.Id, VisiblePages++, ItemsPerPage);
request = new GetUserRecentActivitiesRequest(User.Value.Id, VisiblePages++, ItemsPerPage);
request.Success += activities => Schedule(() =>
{
MoreButton.FadeTo(activities.Count == ItemsPerPage ? 1 : 0);
MoreButton.IsLoading = false;
if (!activities.Any() && VisiblePages == 1) protected override Drawable CreateDrawableItem(APIRecentActivity model) => new DrawableRecentActivity(model);
{
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();
}
} }
} }