2019-01-24 08:43:03 +00: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 09:19:50 +00:00
|
|
|
|
|
2018-11-20 07:51:59 +00:00
|
|
|
|
using osuTK;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
using osu.Framework.Allocation;
|
2019-02-21 10:04:31 +00:00
|
|
|
|
using osu.Framework.Bindables;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2019-02-12 04:04:46 +00:00
|
|
|
|
using osu.Game.Graphics;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
using osu.Game.Graphics.Sprites;
|
|
|
|
|
using osu.Game.Online.API;
|
|
|
|
|
using osu.Game.Rulesets;
|
|
|
|
|
using osu.Game.Users;
|
2019-08-27 12:47:37 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
2020-07-30 01:51:09 +00:00
|
|
|
|
using osu.Game.Graphics.UserInterface;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Overlays.Profile.Sections
|
|
|
|
|
{
|
2019-08-28 04:28:21 +00:00
|
|
|
|
public abstract class PaginatedContainer<TModel> : FillFlowContainer
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
2019-08-27 12:47:37 +00:00
|
|
|
|
[Resolved]
|
|
|
|
|
private IAPIProvider api { get; set; }
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
|
|
|
|
protected int VisiblePages;
|
|
|
|
|
protected int ItemsPerPage;
|
|
|
|
|
|
|
|
|
|
protected readonly Bindable<User> User = new Bindable<User>();
|
2020-09-07 20:08:50 +00:00
|
|
|
|
protected FillFlowContainer ItemsContainer;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
protected RulesetStore Rulesets;
|
|
|
|
|
|
2020-09-07 20:08:50 +00:00
|
|
|
|
private APIRequest<List<TModel>> retrievalRequest;
|
|
|
|
|
private CancellationTokenSource loadCancellation;
|
|
|
|
|
|
2020-09-11 03:19:26 +00:00
|
|
|
|
private readonly string missingText;
|
2020-09-07 20:08:50 +00:00
|
|
|
|
private ShowMoreButton moreButton;
|
2020-09-11 03:19:26 +00:00
|
|
|
|
private OsuSpriteText missing;
|
2020-09-10 17:48:06 +00:00
|
|
|
|
private PaginatedContainerHeader header;
|
2020-09-07 20:08:50 +00:00
|
|
|
|
|
2020-09-10 17:48:06 +00:00
|
|
|
|
private readonly string headerText;
|
|
|
|
|
private readonly CounterVisibilityState counterVisibilityState;
|
|
|
|
|
|
2020-09-11 03:19:26 +00:00
|
|
|
|
protected PaginatedContainer(Bindable<User> user, string headerText = "", string missingText = "", CounterVisibilityState counterVisibilityState = CounterVisibilityState.AlwaysHidden)
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
2020-09-10 17:48:06 +00:00
|
|
|
|
this.headerText = headerText;
|
2020-09-11 03:19:26 +00:00
|
|
|
|
this.missingText = missingText;
|
2020-09-10 17:48:06 +00:00
|
|
|
|
this.counterVisibilityState = counterVisibilityState;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
User.BindTo(user);
|
2020-09-07 20:08:50 +00:00
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2020-09-07 20:08:50 +00:00
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load(RulesetStore rulesets)
|
|
|
|
|
{
|
2018-04-13 09:19:50 +00:00
|
|
|
|
RelativeSizeAxes = Axes.X;
|
|
|
|
|
AutoSizeAxes = Axes.Y;
|
|
|
|
|
Direction = FillDirection.Vertical;
|
|
|
|
|
|
2020-09-10 17:48:06 +00:00
|
|
|
|
Children = new Drawable[]
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
2020-09-10 17:48:06 +00:00
|
|
|
|
header = new PaginatedContainerHeader(headerText, counterVisibilityState)
|
|
|
|
|
{
|
|
|
|
|
Alpha = string.IsNullOrEmpty(headerText) ? 0 : 1
|
|
|
|
|
},
|
2018-04-13 09:19:50 +00:00
|
|
|
|
ItemsContainer = new FillFlowContainer
|
|
|
|
|
{
|
|
|
|
|
AutoSizeAxes = Axes.Y,
|
|
|
|
|
RelativeSizeAxes = Axes.X,
|
2019-05-28 14:04:05 +00:00
|
|
|
|
Spacing = new Vector2(0, 2),
|
2018-04-13 09:19:50 +00:00
|
|
|
|
},
|
2020-07-30 01:51:09 +00:00
|
|
|
|
moreButton = new ShowMoreButton
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
2019-05-30 20:07:04 +00:00
|
|
|
|
Anchor = Anchor.TopCentre,
|
|
|
|
|
Origin = Anchor.TopCentre,
|
2018-04-13 09:19:50 +00:00
|
|
|
|
Alpha = 0,
|
2019-05-28 14:04:05 +00:00
|
|
|
|
Margin = new MarginPadding { Top = 10 },
|
2019-08-27 12:47:37 +00:00
|
|
|
|
Action = showMore,
|
2018-04-13 09:19:50 +00:00
|
|
|
|
},
|
2020-09-11 03:19:26 +00:00
|
|
|
|
missing = new OsuSpriteText
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
2019-05-28 14:04:05 +00:00
|
|
|
|
Font = OsuFont.GetFont(size: 15),
|
2020-09-11 03:19:26 +00:00
|
|
|
|
Text = missingText,
|
2018-04-13 09:19:50 +00:00
|
|
|
|
Alpha = 0,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Rulesets = rulesets;
|
|
|
|
|
|
|
|
|
|
User.ValueChanged += onUserChanged;
|
|
|
|
|
User.TriggerChange();
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-21 09:56:34 +00:00
|
|
|
|
private void onUserChanged(ValueChangedEvent<User> e)
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
2019-08-27 12:47:37 +00:00
|
|
|
|
loadCancellation?.Cancel();
|
|
|
|
|
retrievalRequest?.Cancel();
|
|
|
|
|
|
2018-04-13 09:19:50 +00:00
|
|
|
|
VisiblePages = 0;
|
|
|
|
|
ItemsContainer.Clear();
|
|
|
|
|
|
2019-02-21 09:56:34 +00:00
|
|
|
|
if (e.NewValue != null)
|
2019-12-17 09:50:50 +00:00
|
|
|
|
{
|
2020-09-10 17:48:06 +00:00
|
|
|
|
showMore();
|
|
|
|
|
SetCount(GetCount(e.NewValue));
|
2019-12-17 09:50:50 +00:00
|
|
|
|
}
|
2019-08-27 12:47:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void showMore()
|
|
|
|
|
{
|
|
|
|
|
loadCancellation = new CancellationTokenSource();
|
|
|
|
|
|
|
|
|
|
retrievalRequest = CreateRequest();
|
|
|
|
|
retrievalRequest.Success += UpdateItems;
|
|
|
|
|
|
|
|
|
|
api.Queue(retrievalRequest);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-28 04:28:57 +00:00
|
|
|
|
protected virtual void UpdateItems(List<TModel> items) => Schedule(() =>
|
2019-08-27 12:47:37 +00:00
|
|
|
|
{
|
2020-09-07 20:33:04 +00:00
|
|
|
|
OnItemsReceived(items);
|
|
|
|
|
|
2019-08-28 04:28:57 +00:00
|
|
|
|
if (!items.Any() && VisiblePages == 1)
|
2019-08-27 12:47:37 +00:00
|
|
|
|
{
|
2019-08-28 04:28:57 +00:00
|
|
|
|
moreButton.Hide();
|
|
|
|
|
moreButton.IsLoading = false;
|
2020-09-07 19:30:43 +00:00
|
|
|
|
|
2020-09-11 03:19:26 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(missing.Text))
|
|
|
|
|
missing.Show();
|
2020-09-07 19:30:43 +00:00
|
|
|
|
|
2019-08-28 04:28:57 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2019-08-27 12:47:37 +00:00
|
|
|
|
|
2019-08-28 04:28:57 +00:00
|
|
|
|
LoadComponentsAsync(items.Select(CreateDrawableItem).Where(d => d != null), drawables =>
|
|
|
|
|
{
|
2020-09-11 03:19:26 +00:00
|
|
|
|
missing.Hide();
|
2019-08-28 04:28:57 +00:00
|
|
|
|
moreButton.FadeTo(items.Count == ItemsPerPage ? 1 : 0);
|
|
|
|
|
moreButton.IsLoading = false;
|
2019-08-27 12:47:37 +00:00
|
|
|
|
|
2019-08-28 04:28:57 +00:00
|
|
|
|
ItemsContainer.AddRange(drawables);
|
|
|
|
|
}, loadCancellation.Token);
|
|
|
|
|
});
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2020-09-10 17:48:06 +00:00
|
|
|
|
protected virtual int GetCount(User user) => 0;
|
|
|
|
|
|
|
|
|
|
protected void SetCount(int value) => header.Current.Value = value;
|
2020-09-07 20:08:50 +00:00
|
|
|
|
|
2020-09-07 20:33:04 +00:00
|
|
|
|
protected virtual void OnItemsReceived(List<TModel> items)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-28 04:28:21 +00:00
|
|
|
|
protected abstract APIRequest<List<TModel>> CreateRequest();
|
2019-08-27 12:47:37 +00:00
|
|
|
|
|
2019-08-28 04:31:12 +00:00
|
|
|
|
protected abstract Drawable CreateDrawableItem(TModel model);
|
2019-08-27 12:47:37 +00:00
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
|
{
|
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
retrievalRequest?.Cancel();
|
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|