Move GetNewsRequest from ArticleListing to NewsOverlay

This commit is contained in:
Andrei Zavatski
2021-05-24 08:26:44 +03:00
parent babe24ff5d
commit 06fe0563d3
2 changed files with 56 additions and 44 deletions

View File

@ -8,9 +8,7 @@ using osu.Framework.Allocation;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; 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.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
@ -20,26 +18,20 @@ namespace osu.Game.Overlays.News.Displays
/// </summary> /// </summary>
public class ArticleListing : CompositeDrawable public class ArticleListing : CompositeDrawable
{ {
public Action<APINewsSidebar> SidebarMetadataUpdated; public Action RequestMorePosts;
[Resolved]
private IAPIProvider api { get; set; }
private FillFlowContainer content; private FillFlowContainer content;
private ShowMoreButton showMore; private ShowMoreButton showMore;
private GetNewsRequest request; private readonly GetNewsResponse initialResponse;
private Cursor lastCursor;
private readonly int? year;
/// <summary> /// <summary>
/// Instantiate a listing for the specified year. /// Instantiate a listing for the specified year.
/// </summary> /// </summary>
/// <param name="year">The year to load articles from. If null, will show the most recent articles.</param> /// <param name="initialResponse">Initial response to create articles from.</param>
public ArticleListing(int? year = null) public ArticleListing(GetNewsResponse initialResponse)
{ {
this.year = year; this.initialResponse = initialResponse;
} }
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
@ -69,7 +61,8 @@ namespace osu.Game.Overlays.News.Displays
RelativeSizeAxes = Axes.X, RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y, AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical, Direction = FillDirection.Vertical,
Spacing = new Vector2(0, 10) Spacing = new Vector2(0, 10),
Children = initialResponse.NewsPosts.Select(p => new NewsCard(p)).ToList()
}, },
showMore = new ShowMoreButton showMore = new ShowMoreButton
{ {
@ -79,37 +72,19 @@ namespace osu.Game.Overlays.News.Displays
{ {
Top = 15 Top = 15
}, },
Action = performFetch, Action = RequestMorePosts,
Alpha = 0 Alpha = initialResponse.Cursor != null ? 1 : 0
} }
} }
}; };
performFetch();
}
private void performFetch()
{
request?.Cancel();
request = new GetNewsRequest(year, lastCursor);
request.Success += response => Schedule(() => onSuccess(response));
api.PerformAsync(request);
} }
private CancellationTokenSource cancellationToken; private CancellationTokenSource cancellationToken;
private void onSuccess(GetNewsResponse response) public void AddPosts(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;
LoadComponentsAsync(response.NewsPosts.Select(p => new NewsCard(p)).ToList(), loaded => LoadComponentsAsync(response.NewsPosts.Select(p => new NewsCard(p)).ToList(), loaded =>
{ {
content.AddRange(loaded); content.AddRange(loaded);
@ -121,7 +96,6 @@ namespace osu.Game.Overlays.News.Displays
protected override void Dispose(bool isDisposing) protected override void Dispose(bool isDisposing)
{ {
request?.Cancel();
cancellationToken?.Cancel(); cancellationToken?.Cancel();
base.Dispose(isDisposing); base.Dispose(isDisposing);
} }

View File

@ -6,6 +6,8 @@ using System.Threading;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Overlays.News; using osu.Game.Overlays.News;
using osu.Game.Overlays.News.Displays; using osu.Game.Overlays.News.Displays;
using osu.Game.Overlays.News.Sidebar; using osu.Game.Overlays.News.Sidebar;
@ -18,9 +20,12 @@ namespace osu.Game.Overlays
private readonly Container sidebarContainer; private readonly Container sidebarContainer;
private readonly NewsSidebar sidebar; private readonly NewsSidebar sidebar;
private readonly Container content; private readonly Container content;
private APIRequest lastRequest;
private Cursor lastCursor;
private int? year;
private CancellationTokenSource cancellationToken; private CancellationTokenSource cancellationToken;
private bool displayUpdateRequired = true; private bool displayUpdateRequired = true;
@ -108,7 +113,11 @@ namespace osu.Game.Overlays
protected void LoadDisplay(Drawable display) protected void LoadDisplay(Drawable display)
{ {
ScrollFlow.ScrollToStart(); ScrollFlow.ScrollToStart();
LoadComponentAsync(display, loaded => content.Child = loaded, (cancellationToken = new CancellationTokenSource()).Token); LoadComponentAsync(display, loaded =>
{
content.Child = loaded;
Loading.Hide();
}, (cancellationToken = new CancellationTokenSource()).Token);
} }
protected override void UpdateAfterChildren() protected override void UpdateAfterChildren()
@ -132,13 +141,41 @@ namespace osu.Game.Overlays
Header.SetFrontPage(); Header.SetFrontPage();
var page = new ArticleListing(year); this.year = year;
page.SidebarMetadataUpdated += metadata => Schedule(() => lastCursor = null;
performListingRequest(response =>
{ {
sidebar.Metadata.Value = metadata; sidebar.Metadata.Value = response.SidebarMetadata;
Loading.Hide();
var listing = new ArticleListing(response);
listing.RequestMorePosts += getMorePosts;
LoadDisplay(listing);
}); });
LoadDisplay(page); }
private void getMorePosts()
{
lastRequest?.Cancel();
performListingRequest(response =>
{
if (content.Child is ArticleListing listing)
listing.AddPosts(response);
});
}
private void performListingRequest(Action<GetNewsResponse> onSuccess)
{
lastRequest = new GetNewsRequest(year, lastCursor);
((GetNewsRequest)lastRequest).Success += response => Schedule(() =>
{
lastCursor = response.Cursor;
onSuccess?.Invoke(response);
});
API.PerformAsync(lastRequest);
} }
private void loadArticle(string article) private void loadArticle(string article)
@ -149,17 +186,18 @@ namespace osu.Game.Overlays
// Temporary, should be handled by ArticleDisplay later // Temporary, should be handled by ArticleDisplay later
LoadDisplay(Empty()); LoadDisplay(Empty());
Loading.Hide();
} }
private void beginLoading() private void beginLoading()
{ {
lastRequest?.Cancel();
cancellationToken?.Cancel(); cancellationToken?.Cancel();
Loading.Show(); Loading.Show();
} }
protected override void Dispose(bool isDisposing) protected override void Dispose(bool isDisposing)
{ {
lastRequest?.Cancel();
cancellationToken?.Cancel(); cancellationToken?.Cancel();
base.Dispose(isDisposing); base.Dispose(isDisposing);
} }