osu/osu.Game/Overlays/News/Displays/FrontPageDisplay.cs

114 lines
3.6 KiB
C#
Raw Normal View History

2020-07-09 01:02:14 +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.
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 06:26:29 +00:00
public class FrontPageDisplay : OverlayView<GetNewsResponse>
2020-07-09 01:02:14 +00:00
{
protected override bool PerformFetchOnApiStateChange => false;
2020-07-25 06:26:29 +00:00
protected override APIRequest<GetNewsResponse> CreateRequest() => new GetNewsRequest();
2020-07-09 01:02:14 +00:00
2020-07-25 06:26:29 +00:00
private FillFlowContainer content;
private ShowMoreButton showMore;
2020-07-09 01:02:14 +00:00
2020-07-25 06:26:29 +00:00
private GetNewsRequest olderPostsRequest;
2020-07-09 01:02:14 +00:00
private Cursor lastCursor;
2020-07-25 06:26:29 +00:00
[BackgroundDependencyLoader]
private void load()
2020-07-09 01:02:14 +00: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 06:26:29 +00:00
Action = fetchOlderPosts,
2020-07-09 01:02:14 +00:00
Alpha = 0
}
}
};
}
private CancellationTokenSource cancellationToken;
2020-07-25 06:26:29 +00:00
protected override void OnSuccess(GetNewsResponse response)
2020-07-09 01:02:14 +00:00
{
2020-07-25 06:26:29 +00:00
cancellationToken?.Cancel();
2020-07-09 01:02:14 +00: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;
showMore.Show();
}, (cancellationToken = new CancellationTokenSource()).Token);
}
2020-07-25 06:26:29 +00:00
private void fetchOlderPosts()
{
olderPostsRequest?.Cancel();
olderPostsRequest = new GetNewsRequest(lastCursor);
olderPostsRequest.Success += response => Schedule(() => OnSuccess(response));
API.PerformAsync(olderPostsRequest);
}
2020-07-09 01:02:14 +00:00
protected override void Dispose(bool isDisposing)
{
2020-07-25 06:26:29 +00:00
olderPostsRequest?.Cancel();
2020-07-09 01:02:14 +00:00
cancellationToken?.Cancel();
base.Dispose(isDisposing);
}
}
}