mirror of
https://github.com/ppy/osu.git
synced 2025-01-13 22:22:55 +08:00
Move GetNewsRequest from ArticleListing to NewsOverlay
This commit is contained in:
parent
babe24ff5d
commit
06fe0563d3
@ -8,9 +8,7 @@ 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 osu.Game.Online.API.Requests.Responses;
|
||||
using osuTK;
|
||||
|
||||
namespace osu.Game.Overlays.News.Displays
|
||||
@ -20,26 +18,20 @@ namespace osu.Game.Overlays.News.Displays
|
||||
/// </summary>
|
||||
public class ArticleListing : CompositeDrawable
|
||||
{
|
||||
public Action<APINewsSidebar> SidebarMetadataUpdated;
|
||||
|
||||
[Resolved]
|
||||
private IAPIProvider api { get; set; }
|
||||
public Action RequestMorePosts;
|
||||
|
||||
private FillFlowContainer content;
|
||||
private ShowMoreButton showMore;
|
||||
|
||||
private GetNewsRequest request;
|
||||
private Cursor lastCursor;
|
||||
|
||||
private readonly int? year;
|
||||
private readonly GetNewsResponse initialResponse;
|
||||
|
||||
/// <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)
|
||||
/// <param name="initialResponse">Initial response to create articles from.</param>
|
||||
public ArticleListing(GetNewsResponse initialResponse)
|
||||
{
|
||||
this.year = year;
|
||||
this.initialResponse = initialResponse;
|
||||
}
|
||||
|
||||
[BackgroundDependencyLoader]
|
||||
@ -69,7 +61,8 @@ namespace osu.Game.Overlays.News.Displays
|
||||
RelativeSizeAxes = Axes.X,
|
||||
AutoSizeAxes = Axes.Y,
|
||||
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
|
||||
{
|
||||
@ -79,37 +72,19 @@ namespace osu.Game.Overlays.News.Displays
|
||||
{
|
||||
Top = 15
|
||||
},
|
||||
Action = performFetch,
|
||||
Alpha = 0
|
||||
Action = RequestMorePosts,
|
||||
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 void onSuccess(GetNewsResponse response)
|
||||
public void AddPosts(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);
|
||||
@ -121,7 +96,6 @@ namespace osu.Game.Overlays.News.Displays
|
||||
|
||||
protected override void Dispose(bool isDisposing)
|
||||
{
|
||||
request?.Cancel();
|
||||
cancellationToken?.Cancel();
|
||||
base.Dispose(isDisposing);
|
||||
}
|
||||
|
@ -6,6 +6,8 @@ using System.Threading;
|
||||
using osu.Framework.Bindables;
|
||||
using osu.Framework.Graphics;
|
||||
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.Displays;
|
||||
using osu.Game.Overlays.News.Sidebar;
|
||||
@ -18,9 +20,12 @@ namespace osu.Game.Overlays
|
||||
|
||||
private readonly Container sidebarContainer;
|
||||
private readonly NewsSidebar sidebar;
|
||||
|
||||
private readonly Container content;
|
||||
|
||||
private APIRequest lastRequest;
|
||||
private Cursor lastCursor;
|
||||
private int? year;
|
||||
|
||||
private CancellationTokenSource cancellationToken;
|
||||
|
||||
private bool displayUpdateRequired = true;
|
||||
@ -108,7 +113,11 @@ namespace osu.Game.Overlays
|
||||
protected void LoadDisplay(Drawable display)
|
||||
{
|
||||
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()
|
||||
@ -132,13 +141,41 @@ namespace osu.Game.Overlays
|
||||
|
||||
Header.SetFrontPage();
|
||||
|
||||
var page = new ArticleListing(year);
|
||||
page.SidebarMetadataUpdated += metadata => Schedule(() =>
|
||||
this.year = year;
|
||||
lastCursor = null;
|
||||
|
||||
performListingRequest(response =>
|
||||
{
|
||||
sidebar.Metadata.Value = metadata;
|
||||
Loading.Hide();
|
||||
sidebar.Metadata.Value = response.SidebarMetadata;
|
||||
|
||||
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)
|
||||
@ -149,17 +186,18 @@ namespace osu.Game.Overlays
|
||||
|
||||
// Temporary, should be handled by ArticleDisplay later
|
||||
LoadDisplay(Empty());
|
||||
Loading.Hide();
|
||||
}
|
||||
|
||||
private void beginLoading()
|
||||
{
|
||||
lastRequest?.Cancel();
|
||||
cancellationToken?.Cancel();
|
||||
Loading.Show();
|
||||
}
|
||||
|
||||
protected override void Dispose(bool isDisposing)
|
||||
{
|
||||
lastRequest?.Cancel();
|
||||
cancellationToken?.Cancel();
|
||||
base.Dispose(isDisposing);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user