1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-21 23:27:25 +08:00

Implement FrontPageDisplay

This commit is contained in:
Andrei Zavatski 2020-07-08 20:07:29 +03:00
parent 49d998c8db
commit 0b4213f330
3 changed files with 137 additions and 1 deletions

View File

@ -12,6 +12,8 @@ namespace osu.Game.Tests.Visual.Online
{
private TestNewsOverlay news;
protected override bool UseOnlineAPI => true;
protected override void LoadComplete()
{
base.LoadComplete();

View File

@ -0,0 +1,133 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System.Threading;
using osu.Framework.Allocation;
using osu.Framework.Extensions.IEnumerableExtensions;
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 osuTK;
namespace osu.Game.Overlays.News.Displays
{
public class FrontpageDisplay : CompositeDrawable
{
[Resolved]
private IAPIProvider api { get; set; }
private readonly FillFlowContainer content;
private readonly FrontpageShowMoreButton showMore;
private GetNewsRequest request;
private Cursor lastCursor;
public FrontpageDisplay()
{
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
Padding = new MarginPadding
{
Top = 20,
Bottom = 10,
Left = 35,
Right = 55
};
InternalChild = new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
Spacing = new Vector2(0, 10),
Children = new Drawable[]
{
content = new FillFlowContainer
{
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
Spacing = new Vector2(0, 10)
},
showMore = new FrontpageShowMoreButton
{
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Margin = new MarginPadding
{
Vertical = 15
},
Action = fetchPage,
Alpha = 0
}
}
};
}
[BackgroundDependencyLoader]
private void load()
{
fetchPage();
}
private void fetchPage()
{
request = new GetNewsRequest(lastCursor);
request.Success += response => Schedule(() => createContent(response));
api.PerformAsync(request);
}
private CancellationTokenSource cancellationToken;
private void createContent(GetNewsResponse response)
{
lastCursor = response.Cursor;
FillFlowContainer<NewsCard> flow;
flow = new FillFlowContainer<NewsCard>
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
Spacing = new Vector2(0, 10)
};
response.NewsPosts.ForEach(p =>
{
flow.Add(new NewsCard(p));
});
LoadComponentAsync(flow, loaded =>
{
content.Add(loaded);
showMore.IsLoading = false;
showMore.Show();
}, (cancellationToken = new CancellationTokenSource()).Token);
}
protected override void Dispose(bool isDisposing)
{
request?.Cancel();
cancellationToken?.Cancel();
base.Dispose(isDisposing);
}
private class FrontpageShowMoreButton : ShowMoreButton
{
[BackgroundDependencyLoader]
private void load(OverlayColourProvider colourProvider)
{
Height = 20;
IdleColour = colourProvider.Background3;
HoverColour = colourProvider.Background2;
ChevronIconColour = colourProvider.Foreground1;
}
}
}
}

View File

@ -9,6 +9,7 @@ using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Game.Graphics.UserInterface;
using osu.Game.Overlays.News;
using osu.Game.Overlays.News.Displays;
namespace osu.Game.Overlays
{
@ -87,7 +88,7 @@ namespace osu.Game.Overlays
if (current.NewValue == null)
{
LoadDisplay(Empty());
LoadDisplay(new FrontpageDisplay());
return;
}