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;
|
|
|
|
|
using osu.Game.Online.API.Requests;
|
|
|
|
|
using osuTK;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Overlays.News.Displays
|
|
|
|
|
{
|
2020-07-25 20:08:06 +08:00
|
|
|
|
public class FrontPageDisplay : CompositeDrawable
|
2020-07-09 09:02:14 +08:00
|
|
|
|
{
|
2021-05-19 20:36:05 +08:00
|
|
|
|
public Action<GetNewsResponse> ResponseReceived;
|
|
|
|
|
|
2020-07-25 20:08:06 +08:00
|
|
|
|
[Resolved]
|
|
|
|
|
private IAPIProvider api { get; set; }
|
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
|
|
|
|
|
2020-07-25 20:08:06 +08:00
|
|
|
|
private GetNewsRequest request;
|
2020-07-09 09:02:14 +08:00
|
|
|
|
private Cursor lastCursor;
|
|
|
|
|
|
2021-05-19 20:36:05 +08:00
|
|
|
|
private readonly int year;
|
|
|
|
|
|
|
|
|
|
public FrontPageDisplay(int year = 0)
|
|
|
|
|
{
|
|
|
|
|
this.year = year;
|
|
|
|
|
}
|
|
|
|
|
|
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,
|
|
|
|
|
Spacing = new Vector2(0, 10)
|
|
|
|
|
},
|
|
|
|
|
showMore = new ShowMoreButton
|
|
|
|
|
{
|
|
|
|
|
Anchor = Anchor.TopCentre,
|
|
|
|
|
Origin = Anchor.TopCentre,
|
|
|
|
|
Margin = new MarginPadding
|
|
|
|
|
{
|
|
|
|
|
Top = 15
|
|
|
|
|
},
|
2020-07-25 20:08:06 +08:00
|
|
|
|
Action = performFetch,
|
2020-07-09 09:02:14 +08:00
|
|
|
|
Alpha = 0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
2020-07-25 20:08:06 +08:00
|
|
|
|
|
|
|
|
|
performFetch();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void performFetch()
|
|
|
|
|
{
|
|
|
|
|
request?.Cancel();
|
|
|
|
|
|
2021-05-19 20:36:05 +08:00
|
|
|
|
request = new GetNewsRequest(year, lastCursor);
|
2020-07-25 20:08:06 +08:00
|
|
|
|
request.Success += response => Schedule(() => onSuccess(response));
|
|
|
|
|
api.PerformAsync(request);
|
2020-07-09 09:02:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private CancellationTokenSource cancellationToken;
|
|
|
|
|
|
2021-05-19 20:36:05 +08:00
|
|
|
|
private bool initialLoad = true;
|
|
|
|
|
|
2020-07-25 20:08:06 +08:00
|
|
|
|
private void onSuccess(GetNewsResponse response)
|
2020-07-09 09:02:14 +08:00
|
|
|
|
{
|
2020-07-25 14:26:29 +08:00
|
|
|
|
cancellationToken?.Cancel();
|
|
|
|
|
|
2020-07-09 09:02:14 +08:00
|
|
|
|
lastCursor = response.Cursor;
|
|
|
|
|
|
|
|
|
|
var flow = new FillFlowContainer<NewsCard>
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
|
AutoSizeAxes = Axes.Y,
|
|
|
|
|
Direction = FillDirection.Vertical,
|
|
|
|
|
Spacing = new Vector2(0, 10),
|
|
|
|
|
Children = response.NewsPosts.Select(p => new NewsCard(p)).ToList()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
LoadComponentAsync(flow, loaded =>
|
|
|
|
|
{
|
|
|
|
|
content.Add(loaded);
|
|
|
|
|
showMore.IsLoading = false;
|
2021-05-09 09:57:24 +08:00
|
|
|
|
showMore.Alpha = lastCursor == null ? 0 : 1;
|
2021-05-19 20:36:05 +08:00
|
|
|
|
|
|
|
|
|
if (initialLoad)
|
|
|
|
|
{
|
|
|
|
|
ResponseReceived?.Invoke(response);
|
|
|
|
|
initialLoad = false;
|
|
|
|
|
}
|
2020-07-09 09:02:14 +08:00
|
|
|
|
}, (cancellationToken = new CancellationTokenSource()).Token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
|
{
|
2020-07-25 20:08:06 +08:00
|
|
|
|
request?.Cancel();
|
2020-07-09 09:02:14 +08:00
|
|
|
|
cancellationToken?.Cancel();
|
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|