1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 15:47:26 +08:00

Improve code quality around cursor and upwards passing of response data

This commit is contained in:
Dean Herbert 2021-05-21 15:41:31 +09:00
parent 40ca94cd7b
commit fbe4d7e03c
2 changed files with 13 additions and 23 deletions

View File

@ -10,6 +10,7 @@ using osu.Framework.Graphics.Containers;
using osu.Game.Graphics.UserInterface; using osu.Game.Graphics.UserInterface;
using osu.Game.Online.API; using osu.Game.Online.API;
using osu.Game.Online.API.Requests; using osu.Game.Online.API.Requests;
using osu.Game.Online.API.Requests.Responses;
using osuTK; using osuTK;
namespace osu.Game.Overlays.News.Displays namespace osu.Game.Overlays.News.Displays
@ -19,7 +20,7 @@ namespace osu.Game.Overlays.News.Displays
/// </summary> /// </summary>
public class ArticleListing : CompositeDrawable public class ArticleListing : CompositeDrawable
{ {
public Action<GetNewsResponse> ResponseReceived; public Action<APINewsSidebar> SidebarMetadataUpdated;
[Resolved] [Resolved]
private IAPIProvider api { get; set; } private IAPIProvider api { get; set; }
@ -98,34 +99,23 @@ namespace osu.Game.Overlays.News.Displays
private CancellationTokenSource cancellationToken; private CancellationTokenSource cancellationToken;
private bool initialLoad = true;
private void onSuccess(GetNewsResponse response) private void onSuccess(GetNewsResponse response)
{ {
cancellationToken?.Cancel(); cancellationToken?.Cancel();
// only needs to be updated on the initial load, as the content won't change during pagination.
if (lastCursor == null)
SidebarMetadataUpdated?.Invoke(response.SidebarMetadata);
// store cursor for next pagination request.
lastCursor = response.Cursor; lastCursor = response.Cursor;
var flow = new FillFlowContainer<NewsCard> LoadComponentsAsync(response.NewsPosts.Select(p => new NewsCard(p)).ToList(), loaded =>
{ {
RelativeSizeAxes = Axes.X, content.AddRange(loaded);
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.IsLoading = false;
showMore.Alpha = lastCursor == null ? 0 : 1; showMore.Alpha = response.Cursor != null ? 1 : 0;
if (initialLoad)
{
ResponseReceived?.Invoke(response);
initialLoad = false;
}
}, (cancellationToken = new CancellationTokenSource()).Token); }, (cancellationToken = new CancellationTokenSource()).Token);
} }

View File

@ -133,11 +133,11 @@ namespace osu.Game.Overlays
Header.SetFrontPage(); Header.SetFrontPage();
var page = new ArticleListing(year); var page = new ArticleListing(year);
page.ResponseReceived += r => page.SidebarMetadataUpdated += metadata => Schedule(() =>
{ {
sidebar.Metadata.Value = r.SidebarMetadata; sidebar.Metadata.Value = metadata;
Loading.Hide(); Loading.Hide();
}; });
LoadDisplay(page); LoadDisplay(page);
} }