2019-01-24 16:43:03 +08:00
|
|
|
|
// 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
|
|
|
|
|
2021-12-03 17:16:29 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
2017-11-04 01:25:21 +08:00
|
|
|
|
using osu.Framework.Allocation;
|
2019-02-21 18:04:31 +08:00
|
|
|
|
using osu.Framework.Bindables;
|
2017-11-04 01:25:21 +08:00
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2021-07-17 21:45:17 +08:00
|
|
|
|
using osu.Framework.Localisation;
|
2021-12-03 17:16:29 +08:00
|
|
|
|
using osu.Game.Graphics;
|
|
|
|
|
using osu.Game.Graphics.Sprites;
|
|
|
|
|
using osu.Game.Graphics.UserInterface;
|
|
|
|
|
using osu.Game.Online.API;
|
2021-11-29 08:08:45 +08:00
|
|
|
|
using osu.Game.Graphics.Containers;
|
2022-04-19 07:04:23 +08:00
|
|
|
|
using osu.Game.Online.API.Requests;
|
2021-11-04 17:02:44 +08:00
|
|
|
|
using osu.Game.Online.API.Requests.Responses;
|
2021-12-03 17:16:29 +08:00
|
|
|
|
using osuTK;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-11-04 01:25:21 +08:00
|
|
|
|
namespace osu.Game.Overlays.Profile.Sections
|
|
|
|
|
{
|
2020-11-21 08:18:24 +08:00
|
|
|
|
public abstract partial class PaginatedProfileSubsection<TModel> : ProfileSubsection
|
2017-11-04 01:25:21 +08:00
|
|
|
|
{
|
2022-04-19 04:04:21 +08:00
|
|
|
|
/// <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;
|
|
|
|
|
|
2019-08-27 20:47:37 +08:00
|
|
|
|
[Resolved]
|
2022-12-30 20:17:59 +08:00
|
|
|
|
private IAPIProvider api { get; set; } = null!;
|
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
|
|
|
|
|
2022-12-30 20:17:59 +08:00
|
|
|
|
protected ReverseChildIDFillFlowContainer<Drawable> ItemsContainer { get; private set; } = null!;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2022-12-30 20:17:59 +08:00
|
|
|
|
private APIRequest<List<TModel>>? retrievalRequest;
|
|
|
|
|
private CancellationTokenSource? loadCancellation;
|
2020-09-08 04:08:50 +08:00
|
|
|
|
|
2022-12-30 20:17:59 +08:00
|
|
|
|
private ShowMoreButton moreButton = null!;
|
|
|
|
|
private OsuSpriteText missing = null!;
|
2021-07-17 21:45:17 +08:00
|
|
|
|
private readonly LocalisableString? missingText;
|
2020-09-08 04:08:50 +08:00
|
|
|
|
|
2023-01-10 00:46:08 +08:00
|
|
|
|
protected PaginatedProfileSubsection(Bindable<UserProfileData?> userProfileData, LocalisableString? headerText = null, LocalisableString? missingText = null)
|
|
|
|
|
: base(userProfileData, headerText, CounterVisibilityState.AlwaysVisible)
|
2017-11-04 01:25:21 +08:00
|
|
|
|
{
|
2020-11-22 04:03:54 +08:00
|
|
|
|
this.missingText = missingText;
|
2020-09-08 04:08:50 +08:00
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2020-11-21 08:18:24 +08:00
|
|
|
|
protected override Drawable CreateContent() => new FillFlowContainer
|
2020-09-08 04:08:50 +08:00
|
|
|
|
{
|
2020-11-21 08:18:24 +08:00
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
|
AutoSizeAxes = Axes.Y,
|
|
|
|
|
Direction = FillDirection.Vertical,
|
2020-09-11 01:48:06 +08:00
|
|
|
|
Children = new Drawable[]
|
2017-11-04 01:25:21 +08:00
|
|
|
|
{
|
2021-12-07 04:00:26 +08:00
|
|
|
|
// 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.
|
2021-11-29 08:08:45 +08:00
|
|
|
|
ItemsContainer = new ReverseChildIDFillFlowContainer<Drawable>
|
2017-11-04 01:25:21 +08:00
|
|
|
|
{
|
|
|
|
|
AutoSizeAxes = Axes.Y,
|
|
|
|
|
RelativeSizeAxes = Axes.X,
|
2019-05-28 22:04:05 +08:00
|
|
|
|
Spacing = new Vector2(0, 2),
|
2021-12-07 04:00:26 +08:00
|
|
|
|
// ensure the container and its contents are in front of the "more" button.
|
2021-11-29 08:08:45 +08:00
|
|
|
|
Depth = float.MinValue
|
2017-11-04 01:25:21 +08:00
|
|
|
|
},
|
2020-07-30 09:51:09 +08:00
|
|
|
|
moreButton = new ShowMoreButton
|
2017-11-04 01:25:21 +08:00
|
|
|
|
{
|
2019-05-31 04:07:04 +08:00
|
|
|
|
Anchor = Anchor.TopCentre,
|
|
|
|
|
Origin = Anchor.TopCentre,
|
2017-11-04 01:25:21 +08:00
|
|
|
|
Alpha = 0,
|
2019-05-28 22:04:05 +08:00
|
|
|
|
Margin = new MarginPadding { Top = 10 },
|
2019-08-27 20:47:37 +08:00
|
|
|
|
Action = showMore,
|
2020-11-22 04:03:54 +08:00
|
|
|
|
},
|
|
|
|
|
missing = new OsuSpriteText
|
|
|
|
|
{
|
|
|
|
|
Font = OsuFont.GetFont(size: 15),
|
2021-07-17 21:45:17 +08:00
|
|
|
|
Text = missingText ?? string.Empty,
|
2020-11-22 04:03:54 +08:00
|
|
|
|
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();
|
2023-01-10 01:22:49 +08:00
|
|
|
|
UserProfileData.BindValueChanged(onUserChanged, true);
|
2020-11-22 04:13:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-10 00:37:28 +08:00
|
|
|
|
private void onUserChanged(ValueChangedEvent<UserProfileData?> e)
|
2017-11-04 01:25:21 +08:00
|
|
|
|
{
|
2019-08-27 20:47:37 +08:00
|
|
|
|
loadCancellation?.Cancel();
|
|
|
|
|
retrievalRequest?.Cancel();
|
|
|
|
|
|
2022-04-19 07:04:23 +08:00
|
|
|
|
CurrentPage = null;
|
2017-11-04 01:25:21 +08:00
|
|
|
|
ItemsContainer.Clear();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2022-12-30 21:56:19 +08:00
|
|
|
|
if (e.NewValue?.User != null)
|
2019-12-17 17:50:50 +08:00
|
|
|
|
{
|
2020-09-11 01:48:06 +08:00
|
|
|
|
showMore();
|
2022-12-30 21:56:19 +08:00
|
|
|
|
SetCount(GetCount(e.NewValue.User));
|
2019-12-17 17:50:50 +08:00
|
|
|
|
}
|
2019-08-27 20:47:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void showMore()
|
|
|
|
|
{
|
2023-01-10 01:22:49 +08:00
|
|
|
|
if (UserProfileData.Value?.User == null)
|
2022-12-30 20:17:59 +08:00
|
|
|
|
return;
|
|
|
|
|
|
2019-08-27 20:47:37 +08:00
|
|
|
|
loadCancellation = new CancellationTokenSource();
|
|
|
|
|
|
2022-04-19 07:48:34 +08:00
|
|
|
|
CurrentPage = CurrentPage?.TakeNext(ItemsPerPage) ?? new PaginationParameters(InitialItemsCount);
|
2022-04-19 07:04:23 +08:00
|
|
|
|
|
2023-01-10 01:22:49 +08:00
|
|
|
|
retrievalRequest = CreateRequest(UserProfileData.Value.User, CurrentPage.Value);
|
2022-12-30 20:17:59 +08:00
|
|
|
|
retrievalRequest.Success += items => UpdateItems(items, loadCancellation);
|
2019-08-27 20:47:37 +08:00
|
|
|
|
|
|
|
|
|
api.Queue(retrievalRequest);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-30 20:17:59 +08:00
|
|
|
|
protected virtual void UpdateItems(List<TModel> items, CancellationTokenSource cancellationTokenSource) => Schedule(() =>
|
2019-08-27 20:47:37 +08:00
|
|
|
|
{
|
2020-09-08 04:33:04 +08:00
|
|
|
|
OnItemsReceived(items);
|
|
|
|
|
|
2022-04-19 07:04:23 +08:00
|
|
|
|
if (!items.Any() && CurrentPage?.Offset == 0)
|
2019-08-27 20:47:37 +08:00
|
|
|
|
{
|
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)
|
2020-11-22 04:03:54 +08:00
|
|
|
|
missing.Show();
|
2020-09-08 03:30:43 +08:00
|
|
|
|
|
2019-08-28 12:28:57 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
2019-08-27 20:47:37 +08:00
|
|
|
|
|
2022-12-30 20:17:59 +08:00
|
|
|
|
LoadComponentsAsync(items.Select(CreateDrawableItem).Where(d => d != null).Cast<Drawable>(), drawables =>
|
2019-08-28 12:28:57 +08:00
|
|
|
|
{
|
2020-11-22 04:03:54 +08:00
|
|
|
|
missing.Hide();
|
2022-04-19 04:04:21 +08:00
|
|
|
|
|
2022-04-19 07:04:23 +08:00
|
|
|
|
moreButton.FadeTo(items.Count == CurrentPage?.Limit ? 1 : 0);
|
2019-08-28 12:28:57 +08:00
|
|
|
|
moreButton.IsLoading = false;
|
2019-08-27 20:47:37 +08:00
|
|
|
|
|
2019-08-28 12:28:57 +08:00
|
|
|
|
ItemsContainer.AddRange(drawables);
|
2022-12-30 20:17:59 +08:00
|
|
|
|
}, cancellationTokenSource.Token);
|
2019-08-28 12:28:57 +08:00
|
|
|
|
});
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2021-11-04 17:02:44 +08:00
|
|
|
|
protected virtual int GetCount(APIUser user) => 0;
|
2020-09-11 01:48:06 +08:00
|
|
|
|
|
2020-09-08 04:33:04 +08:00
|
|
|
|
protected virtual void OnItemsReceived(List<TModel> items)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-30 20:17:59 +08:00
|
|
|
|
protected abstract APIRequest<List<TModel>> CreateRequest(APIUser user, PaginationParameters pagination);
|
2019-08-27 20:47:37 +08:00
|
|
|
|
|
2022-12-30 20:17:59 +08:00
|
|
|
|
protected abstract Drawable? CreateDrawableItem(TModel model);
|
2019-08-27 20:47:37 +08:00
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
|
{
|
|
|
|
|
retrievalRequest?.Cancel();
|
2020-11-21 08:18:24 +08:00
|
|
|
|
loadCancellation?.Cancel();
|
|
|
|
|
base.Dispose(isDisposing);
|
2019-08-27 20:47:37 +08:00
|
|
|
|
}
|
2017-11-04 01:25:21 +08:00
|
|
|
|
}
|
|
|
|
|
}
|