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

138 lines
4.2 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
2018-11-20 15:51:59 +08:00
using osuTK;
2018-04-13 17:19:50 +08:00
using osu.Framework.Allocation;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Game.Graphics;
2018-04-13 17:19:50 +08:00
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;
using System.Threading;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Overlays.Profile.Sections
{
2019-08-28 12:28:21 +08:00
public abstract class PaginatedContainer<TModel> : FillFlowContainer
2018-04-13 17:19:50 +08:00
{
private readonly ShowMoreButton moreButton;
private readonly OsuSpriteText missingText;
2019-08-28 12:28:21 +08:00
private APIRequest<List<TModel>> retrievalRequest;
private CancellationTokenSource loadCancellation;
[Resolved]
private IAPIProvider api { get; set; }
2018-04-13 17:19:50 +08:00
protected int VisiblePages;
protected int ItemsPerPage;
protected readonly Bindable<User> User = new Bindable<User>();
protected readonly FillFlowContainer ItemsContainer;
2018-04-13 17:19:50 +08:00
protected RulesetStore Rulesets;
2019-05-29 00:53:00 +08:00
protected PaginatedContainer(Bindable<User> user, string header, string missing)
2018-04-13 17:19:50 +08:00
{
User.BindTo(user);
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
Direction = FillDirection.Vertical;
Children = new Drawable[]
{
new OsuSpriteText
{
Text = header,
2019-05-28 22:04:05 +08:00
Font = OsuFont.GetFont(size: 20, weight: FontWeight.Bold),
2018-04-13 17:19:50 +08:00
Margin = new MarginPadding { Top = 10, Bottom = 10 },
},
ItemsContainer = new FillFlowContainer
{
AutoSizeAxes = Axes.Y,
RelativeSizeAxes = Axes.X,
2019-05-28 22:04:05 +08:00
Spacing = new Vector2(0, 2),
2018-04-13 17:19:50 +08:00
},
moreButton = new ShowMoreButton
2018-04-13 17:19:50 +08:00
{
2019-05-31 04:07:04 +08:00
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
2018-04-13 17:19:50 +08:00
Alpha = 0,
2019-05-28 22:04:05 +08:00
Margin = new MarginPadding { Top = 10 },
Action = showMore,
2018-04-13 17:19:50 +08:00
},
missingText = new OsuSpriteText
2018-04-13 17:19:50 +08:00
{
2019-05-28 22:04:05 +08:00
Font = OsuFont.GetFont(size: 15),
2018-04-13 17:19:50 +08:00
Text = missing,
Alpha = 0,
},
};
}
[BackgroundDependencyLoader]
private void load(RulesetStore rulesets)
2018-04-13 17:19:50 +08:00
{
Rulesets = rulesets;
User.ValueChanged += onUserChanged;
User.TriggerChange();
}
2019-02-21 17:56:34 +08:00
private void onUserChanged(ValueChangedEvent<User> e)
2018-04-13 17:19:50 +08:00
{
loadCancellation?.Cancel();
retrievalRequest?.Cancel();
2018-04-13 17:19:50 +08:00
VisiblePages = 0;
ItemsContainer.Clear();
2019-02-21 17:56:34 +08:00
if (e.NewValue != null)
showMore();
}
private void showMore()
{
loadCancellation = new CancellationTokenSource();
retrievalRequest = CreateRequest();
retrievalRequest.Success += UpdateItems;
api.Queue(retrievalRequest);
}
2019-08-28 12:28:57 +08:00
protected virtual void UpdateItems(List<TModel> items) => Schedule(() =>
{
2019-08-28 12:28:57 +08:00
if (!items.Any() && VisiblePages == 1)
{
2019-08-28 12:28:57 +08:00
moreButton.Hide();
moreButton.IsLoading = false;
missingText.Show();
return;
}
2019-08-28 12:28:57 +08:00
LoadComponentsAsync(items.Select(CreateDrawableItem).Where(d => d != null), drawables =>
{
missingText.Hide();
moreButton.FadeTo(items.Count == ItemsPerPage ? 1 : 0);
moreButton.IsLoading = false;
2019-08-28 12:28:57 +08:00
ItemsContainer.AddRange(drawables);
}, loadCancellation.Token);
});
2018-04-13 17:19:50 +08:00
2019-08-28 12:28:21 +08:00
protected abstract APIRequest<List<TModel>> CreateRequest();
2019-08-28 12:31:12 +08:00
protected abstract Drawable CreateDrawableItem(TModel model);
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
retrievalRequest?.Cancel();
}
2018-04-13 17:19:50 +08:00
}
}