2020-07-09 09:02:14 +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.
|
|
|
|
|
|
2021-05-19 20:36:05 +08:00
|
|
|
|
using System;
|
2020-07-09 09:02:14 +08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
|
using osu.Game.Graphics.UserInterface;
|
|
|
|
|
using osu.Game.Online.API.Requests;
|
|
|
|
|
using osuTK;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Overlays.News.Displays
|
|
|
|
|
{
|
2021-05-20 14:24:01 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Lists articles in a vertical flow for a specified year.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class ArticleListing : CompositeDrawable
|
2020-07-09 09:02:14 +08:00
|
|
|
|
{
|
2021-05-24 13:26:44 +08:00
|
|
|
|
public Action RequestMorePosts;
|
2020-07-09 09:02:14 +08:00
|
|
|
|
|
2020-07-25 14:26:29 +08:00
|
|
|
|
private FillFlowContainer content;
|
|
|
|
|
private ShowMoreButton showMore;
|
2020-07-09 09:02:14 +08:00
|
|
|
|
|
2021-05-24 13:26:44 +08:00
|
|
|
|
private readonly GetNewsResponse initialResponse;
|
2021-05-19 20:36:05 +08:00
|
|
|
|
|
2021-05-20 14:24:01 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Instantiate a listing for the specified year.
|
|
|
|
|
/// </summary>
|
2021-05-24 13:26:44 +08:00
|
|
|
|
/// <param name="initialResponse">Initial response to create articles from.</param>
|
|
|
|
|
public ArticleListing(GetNewsResponse initialResponse)
|
2021-05-19 20:36:05 +08:00
|
|
|
|
{
|
2021-05-24 13:26:44 +08:00
|
|
|
|
this.initialResponse = initialResponse;
|
2021-05-19 20:36:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-07-25 14:26:29 +08:00
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load()
|
2020-07-09 09:02:14 +08:00
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.X;
|
|
|
|
|
AutoSizeAxes = Axes.Y;
|
|
|
|
|
Padding = new MarginPadding
|
|
|
|
|
{
|
|
|
|
|
Vertical = 20,
|
|
|
|
|
Left = 30,
|
|
|
|
|
Right = 50
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
InternalChild = new FillFlowContainer
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
|
AutoSizeAxes = Axes.Y,
|
|
|
|
|
Direction = FillDirection.Vertical,
|
|
|
|
|
Spacing = new Vector2(0, 10),
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
|
|
|
|
content = new FillFlowContainer
|
|
|
|
|
{
|
|
|
|
|
Anchor = Anchor.TopCentre,
|
|
|
|
|
Origin = Anchor.TopCentre,
|
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
|
AutoSizeAxes = Axes.Y,
|
|
|
|
|
Direction = FillDirection.Vertical,
|
2021-05-24 13:26:44 +08:00
|
|
|
|
Spacing = new Vector2(0, 10),
|
|
|
|
|
Children = initialResponse.NewsPosts.Select(p => new NewsCard(p)).ToList()
|
2020-07-09 09:02:14 +08:00
|
|
|
|
},
|
|
|
|
|
showMore = new ShowMoreButton
|
|
|
|
|
{
|
|
|
|
|
Anchor = Anchor.TopCentre,
|
|
|
|
|
Origin = Anchor.TopCentre,
|
|
|
|
|
Margin = new MarginPadding
|
|
|
|
|
{
|
|
|
|
|
Top = 15
|
|
|
|
|
},
|
2021-05-24 13:26:44 +08:00
|
|
|
|
Action = RequestMorePosts,
|
|
|
|
|
Alpha = initialResponse.Cursor != null ? 1 : 0
|
2020-07-09 09:02:14 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private CancellationTokenSource cancellationToken;
|
|
|
|
|
|
2021-05-24 13:26:44 +08:00
|
|
|
|
public void AddPosts(GetNewsResponse response)
|
2020-07-09 09:02:14 +08:00
|
|
|
|
{
|
2020-07-25 14:26:29 +08:00
|
|
|
|
cancellationToken?.Cancel();
|
|
|
|
|
|
2021-05-21 14:41:31 +08:00
|
|
|
|
LoadComponentsAsync(response.NewsPosts.Select(p => new NewsCard(p)).ToList(), loaded =>
|
2020-07-09 09:02:14 +08:00
|
|
|
|
{
|
2021-05-21 14:41:31 +08:00
|
|
|
|
content.AddRange(loaded);
|
2020-07-09 09:02:14 +08:00
|
|
|
|
|
|
|
|
|
showMore.IsLoading = false;
|
2021-05-21 14:41:31 +08:00
|
|
|
|
showMore.Alpha = response.Cursor != null ? 1 : 0;
|
2020-07-09 09:02:14 +08:00
|
|
|
|
}, (cancellationToken = new CancellationTokenSource()).Token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
|
{
|
|
|
|
|
cancellationToken?.Cancel();
|
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|