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

Merge pull request #12925 from EVAST9919/news-request

Move GetNewsRequest from ArticleListing to NewsOverlay
This commit is contained in:
Dean Herbert 2021-05-26 23:26:59 +09:00 committed by GitHub
commit fffa355097
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 76 additions and 78 deletions

View File

@ -2,14 +2,13 @@
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using osu.Framework.Allocation; 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.Responses; using osu.Game.Online.API.Requests.Responses;
using osuTK; using osuTK;
@ -20,26 +19,16 @@ namespace osu.Game.Overlays.News.Displays
/// </summary> /// </summary>
public class ArticleListing : CompositeDrawable public class ArticleListing : CompositeDrawable
{ {
public Action<APINewsSidebar> SidebarMetadataUpdated; private readonly Action fetchMorePosts;
[Resolved]
private IAPIProvider api { get; set; }
private FillFlowContainer content; private FillFlowContainer content;
private ShowMoreButton showMore; private ShowMoreButton showMore;
private GetNewsRequest request; private CancellationTokenSource cancellationToken;
private Cursor lastCursor;
private readonly int? year; public ArticleListing(Action fetchMorePosts)
/// <summary>
/// Instantiate a listing for the specified year.
/// </summary>
/// <param name="year">The year to load articles from. If null, will show the most recent articles.</param>
public ArticleListing(int? year = null)
{ {
this.year = year; this.fetchMorePosts = fetchMorePosts;
} }
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
@ -47,6 +36,7 @@ namespace osu.Game.Overlays.News.Displays
{ {
RelativeSizeAxes = Axes.X; RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y; AutoSizeAxes = Axes.Y;
Padding = new MarginPadding Padding = new MarginPadding
{ {
Vertical = 20, Vertical = 20,
@ -75,53 +65,25 @@ namespace osu.Game.Overlays.News.Displays
{ {
Anchor = Anchor.TopCentre, Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre, Origin = Anchor.TopCentre,
Margin = new MarginPadding Margin = new MarginPadding { Top = 15 },
{ Action = fetchMorePosts,
Top = 15
},
Action = performFetch,
Alpha = 0 Alpha = 0
} }
} }
}; };
performFetch();
} }
private void performFetch() public void AddPosts(IEnumerable<APINewsPost> posts, bool morePostsAvailable) => Schedule(() =>
{ LoadComponentsAsync(posts.Select(p => new NewsCard(p)).ToList(), loaded =>
request?.Cancel();
request = new GetNewsRequest(year, lastCursor);
request.Success += response => Schedule(() => onSuccess(response));
api.PerformAsync(request);
}
private CancellationTokenSource cancellationToken;
private void onSuccess(GetNewsResponse response)
{
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 =>
{ {
content.AddRange(loaded); content.AddRange(loaded);
showMore.IsLoading = false; showMore.IsLoading = false;
showMore.Alpha = response.Cursor != null ? 1 : 0; showMore.Alpha = morePostsAvailable ? 1 : 0;
}, (cancellationToken = new CancellationTokenSource()).Token); }, (cancellationToken = new CancellationTokenSource()).Token)
} );
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,7 @@ 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.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;
@ -14,13 +15,21 @@ namespace osu.Game.Overlays
{ {
public class NewsOverlay : OnlineOverlay<NewsHeader> public class NewsOverlay : OnlineOverlay<NewsHeader>
{ {
private readonly Bindable<string> article = new Bindable<string>(null); private readonly Bindable<string> article = new Bindable<string>();
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 GetNewsRequest request;
private Cursor lastCursor;
/// <summary>
/// The year currently being displayed. If null, the main listing is being displayed.
/// </summary>
private int? displayedYear;
private CancellationTokenSource cancellationToken; private CancellationTokenSource cancellationToken;
private bool displayUpdateRequired = true; private bool displayUpdateRequired = true;
@ -65,7 +74,13 @@ namespace osu.Game.Overlays
base.LoadComplete(); base.LoadComplete();
// should not be run until first pop-in to avoid requesting data before user views. // should not be run until first pop-in to avoid requesting data before user views.
article.BindValueChanged(onArticleChanged); article.BindValueChanged(a =>
{
if (a.NewValue == null)
loadListing();
else
loadArticle(a.NewValue);
});
} }
protected override NewsHeader CreateHeader() => new NewsHeader { ShowFrontPage = ShowFrontPage }; protected override NewsHeader CreateHeader() => new NewsHeader { ShowFrontPage = ShowFrontPage };
@ -95,7 +110,7 @@ namespace osu.Game.Overlays
public void ShowYear(int year) public void ShowYear(int year)
{ {
loadFrontPage(year); loadListing(year);
Show(); Show();
} }
@ -108,7 +123,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()
@ -118,48 +137,65 @@ namespace osu.Game.Overlays
sidebarContainer.Y = Math.Clamp(ScrollFlow.Current - Header.DrawHeight, 0, Math.Max(ScrollFlow.ScrollContent.DrawHeight - DrawHeight - Header.DrawHeight, 0)); sidebarContainer.Y = Math.Clamp(ScrollFlow.Current - Header.DrawHeight, 0, Math.Max(ScrollFlow.ScrollContent.DrawHeight - DrawHeight - Header.DrawHeight, 0));
} }
private void onArticleChanged(ValueChangedEvent<string> article) private void loadListing(int? year = null)
{ {
if (article.NewValue == null)
loadFrontPage();
else
loadArticle(article.NewValue);
}
private void loadFrontPage(int? year = null)
{
beginLoading();
Header.SetFrontPage(); Header.SetFrontPage();
var page = new ArticleListing(year); displayedYear = year;
page.SidebarMetadataUpdated += metadata => Schedule(() => lastCursor = null;
beginLoading(true);
request = new GetNewsRequest(displayedYear);
request.Success += response => Schedule(() =>
{ {
sidebar.Metadata.Value = metadata; lastCursor = response.Cursor;
Loading.Hide(); sidebar.Metadata.Value = response.SidebarMetadata;
var listing = new ArticleListing(getMorePosts);
listing.AddPosts(response.NewsPosts, response.Cursor != null);
LoadDisplay(listing);
}); });
LoadDisplay(page);
API.PerformAsync(request);
}
private void getMorePosts()
{
beginLoading(false);
request = new GetNewsRequest(displayedYear, lastCursor);
request.Success += response => Schedule(() =>
{
lastCursor = response.Cursor;
if (content.Child is ArticleListing listing)
listing.AddPosts(response.NewsPosts, response.Cursor != null);
});
API.PerformAsync(request);
} }
private void loadArticle(string article) private void loadArticle(string article)
{ {
beginLoading(); // This is not yet implemented nor called from anywhere.
beginLoading(true);
Header.SetArticle(article); Header.SetArticle(article);
// Temporary, should be handled by ArticleDisplay later
LoadDisplay(Empty()); LoadDisplay(Empty());
Loading.Hide();
} }
private void beginLoading() private void beginLoading(bool showLoadingOverlay)
{ {
request?.Cancel();
cancellationToken?.Cancel(); cancellationToken?.Cancel();
if (showLoadingOverlay)
Loading.Show(); Loading.Show();
} }
protected override void Dispose(bool isDisposing) protected override void Dispose(bool isDisposing)
{ {
request?.Cancel();
cancellationToken?.Cancel(); cancellationToken?.Cancel();
base.Dispose(isDisposing); base.Dispose(isDisposing);
} }