1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-06 02:37:25 +08:00
osu-lazer/osu.Game/Overlays/Profile/Sections/PaginatedProfileSubsection.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

168 lines
5.5 KiB
C#
Raw Normal View History

// 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.
2018-04-13 17:19:50 +08:00
2022-06-17 15:37:17 +08:00
#nullable disable
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using osu.Framework.Allocation;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2021-07-17 21:45:17 +08:00
using osu.Framework.Localisation;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osu.Game.Online.API;
using osu.Game.Graphics.Containers;
using osu.Game.Online.API.Requests;
using osu.Game.Online.API.Requests.Responses;
using osuTK;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Overlays.Profile.Sections
{
2020-11-21 08:18:24 +08:00
public abstract class PaginatedProfileSubsection<TModel> : ProfileSubsection
{
/// <summary>
/// The number of items displayed per page.
/// </summary>
protected virtual int ItemsPerPage => 50;
/// <summary>
/// The number of items displayed initially.
/// </summary>
protected virtual int InitialItemsCount => 5;
[Resolved]
private IAPIProvider api { get; set; }
2018-04-13 17:19:50 +08:00
2022-04-19 07:48:34 +08:00
protected PaginationParameters? CurrentPage { get; private set; }
2018-04-13 17:19:50 +08:00
protected ReverseChildIDFillFlowContainer<Drawable> ItemsContainer { get; private set; }
2018-04-13 17:19:50 +08:00
private APIRequest<List<TModel>> retrievalRequest;
private CancellationTokenSource loadCancellation;
private ShowMoreButton moreButton;
private OsuSpriteText missing;
2021-07-17 21:45:17 +08:00
private readonly LocalisableString? missingText;
protected PaginatedProfileSubsection(Bindable<APIUser> user, LocalisableString? headerText = null, LocalisableString? missingText = null)
: base(user, headerText, CounterVisibilityState.AlwaysVisible)
{
this.missingText = missingText;
}
2018-04-13 17:19:50 +08:00
2020-11-21 08:18:24 +08:00
protected override Drawable CreateContent() => new FillFlowContainer
{
2020-11-21 08:18:24 +08:00
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
Children = new Drawable[]
{
// reverse ID flow is required for correct Z-ordering of the items (last item should be front-most).
// particularly important in PaginatedBeatmapContainer, as it uses beatmap cards, which have expandable overhanging content.
ItemsContainer = new ReverseChildIDFillFlowContainer<Drawable>
{
AutoSizeAxes = Axes.Y,
RelativeSizeAxes = Axes.X,
2019-05-28 22:04:05 +08:00
Spacing = new Vector2(0, 2),
// ensure the container and its contents are in front of the "more" button.
Depth = float.MinValue
},
2020-07-30 09:51:09 +08:00
moreButton = new ShowMoreButton
{
2019-05-31 04:07:04 +08:00
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Alpha = 0,
2019-05-28 22:04:05 +08:00
Margin = new MarginPadding { Top = 10 },
Action = showMore,
},
missing = new OsuSpriteText
{
Font = OsuFont.GetFont(size: 15),
2021-07-17 21:45:17 +08:00
Text = missingText ?? string.Empty,
Alpha = 0,
2020-11-21 08:18:24 +08:00
}
}
};
2018-04-13 17:19:50 +08:00
2020-11-22 04:13:46 +08:00
protected override void LoadComplete()
{
base.LoadComplete();
User.BindValueChanged(onUserChanged, true);
}
private void onUserChanged(ValueChangedEvent<APIUser> e)
{
loadCancellation?.Cancel();
retrievalRequest?.Cancel();
CurrentPage = null;
ItemsContainer.Clear();
2018-04-13 17:19:50 +08:00
2019-02-21 17:56:34 +08:00
if (e.NewValue != null)
2019-12-17 17:50:50 +08:00
{
showMore();
SetCount(GetCount(e.NewValue));
2019-12-17 17:50:50 +08:00
}
}
private void showMore()
{
loadCancellation = new CancellationTokenSource();
2022-04-19 07:48:34 +08:00
CurrentPage = CurrentPage?.TakeNext(ItemsPerPage) ?? new PaginationParameters(InitialItemsCount);
retrievalRequest = CreateRequest(CurrentPage.Value);
retrievalRequest.Success += UpdateItems;
api.Queue(retrievalRequest);
}
2019-08-28 12:28:57 +08:00
protected virtual void UpdateItems(List<TModel> items) => Schedule(() =>
{
OnItemsReceived(items);
if (!items.Any() && CurrentPage?.Offset == 0)
{
2019-08-28 12:28:57 +08:00
moreButton.Hide();
moreButton.IsLoading = false;
2020-09-08 03:30:43 +08:00
2021-07-17 21:45:17 +08:00
if (missingText.HasValue)
missing.Show();
2020-09-08 03:30:43 +08:00
2019-08-28 12:28:57 +08:00
return;
}
2019-08-28 12:28:57 +08:00
LoadComponentsAsync(items.Select(CreateDrawableItem).Where(d => d != null), drawables =>
{
missing.Hide();
moreButton.FadeTo(items.Count == CurrentPage?.Limit ? 1 : 0);
2019-08-28 12:28:57 +08:00
moreButton.IsLoading = false;
2019-08-28 12:28:57 +08:00
ItemsContainer.AddRange(drawables);
}, loadCancellation.Token);
});
2018-04-13 17:19:50 +08:00
protected virtual int GetCount(APIUser user) => 0;
protected virtual void OnItemsReceived(List<TModel> items)
{
}
2022-04-19 07:48:34 +08:00
protected abstract APIRequest<List<TModel>> CreateRequest(PaginationParameters pagination);
2019-08-28 12:31:12 +08:00
protected abstract Drawable CreateDrawableItem(TModel model);
protected override void Dispose(bool isDisposing)
{
retrievalRequest?.Cancel();
2020-11-21 08:18:24 +08:00
loadCancellation?.Cancel();
base.Dispose(isDisposing);
}
}
}