1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-14 20:25:39 +08:00

Refactor ArticleListing

This commit is contained in:
Andrei Zavatski 2021-05-26 15:35:38 +03:00
parent 82d977f80a
commit 1bde11a07e
2 changed files with 40 additions and 27 deletions

View File

@ -2,13 +2,16 @@
// 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.Collections.Specialized;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using osu.Framework.Allocation; using osu.Framework.Allocation;
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.Graphics.UserInterface; using osu.Game.Graphics.UserInterface;
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,20 +23,12 @@ namespace osu.Game.Overlays.News.Displays
{ {
public Action RequestMorePosts; public Action RequestMorePosts;
private readonly BindableList<APINewsPost> posts = new BindableList<APINewsPost>();
private bool showMoreButtonIsVisible;
private FillFlowContainer content; private FillFlowContainer content;
private ShowMoreButton showMore; private ShowMoreButton showMore;
private readonly GetNewsResponse initialResponse;
/// <summary>
/// Instantiate a listing for the specified year.
/// </summary>
/// <param name="initialResponse">Initial response to create articles from.</param>
public ArticleListing(GetNewsResponse initialResponse)
{
this.initialResponse = initialResponse;
}
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load() private void load()
{ {
@ -45,7 +40,6 @@ namespace osu.Game.Overlays.News.Displays
Left = 30, Left = 30,
Right = 50 Right = 50
}; };
InternalChild = new FillFlowContainer InternalChild = new FillFlowContainer
{ {
RelativeSizeAxes = Axes.X, RelativeSizeAxes = Axes.X,
@ -61,8 +55,7 @@ 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
{ {
@ -73,24 +66,45 @@ namespace osu.Game.Overlays.News.Displays
Top = 15 Top = 15
}, },
Action = RequestMorePosts, Action = RequestMorePosts,
Alpha = initialResponse.Cursor != null ? 1 : 0 Alpha = 0
} }
} }
}; };
} }
protected override void LoadComplete()
{
base.LoadComplete();
posts.BindCollectionChanged((sender, args) =>
{
switch (args.Action)
{
case NotifyCollectionChangedAction.Add:
addPosts(args.NewItems.Cast<APINewsPost>());
break;
default:
throw new NotSupportedException(@"You can only add items to this list. Other actions are not supported.");
}
}, true);
}
public void AddPosts(IEnumerable<APINewsPost> posts, bool showMoreButtonIsVisible)
{
this.showMoreButtonIsVisible = showMoreButtonIsVisible;
this.posts.AddRange(posts);
}
private CancellationTokenSource cancellationToken; private CancellationTokenSource cancellationToken;
public void AddPosts(GetNewsResponse response) private void addPosts(IEnumerable<APINewsPost> posts)
{ {
cancellationToken?.Cancel(); LoadComponentsAsync(posts.Select(p => new NewsCard(p)).ToList(), loaded =>
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 = showMoreButtonIsVisible ? 1 : 0;
}, (cancellationToken = new CancellationTokenSource()).Token); }, (cancellationToken = new CancellationTokenSource()).Token);
} }

View File

@ -152,10 +152,9 @@ namespace osu.Game.Overlays
lastCursor = response.Cursor; lastCursor = response.Cursor;
sidebar.Metadata.Value = response.SidebarMetadata; sidebar.Metadata.Value = response.SidebarMetadata;
LoadDisplay(new ArticleListing(response) var listing = new ArticleListing { RequestMorePosts = getMorePosts };
{ listing.AddPosts(response.NewsPosts, response.Cursor != null);
RequestMorePosts = getMorePosts LoadDisplay(listing);
});
}); });
API.PerformAsync(request); API.PerformAsync(request);
@ -170,7 +169,7 @@ namespace osu.Game.Overlays
{ {
lastCursor = response.Cursor; lastCursor = response.Cursor;
if (content.Child is ArticleListing listing) if (content.Child is ArticleListing listing)
listing.AddPosts(response); listing.AddPosts(response.NewsPosts, response.Cursor != null);
}); });
API.PerformAsync(request); API.PerformAsync(request);