1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-18 02:22:59 +08:00
osu-lazer/osu.Game/Overlays/NewsOverlay.cs

140 lines
4.0 KiB
C#
Raw Normal View History

// 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;
2019-08-10 23:06:52 +08:00
using osu.Framework.Bindables;
2019-08-10 18:22:45 +08:00
using osu.Framework.Graphics;
2019-08-10 18:53:34 +08:00
using osu.Framework.Graphics.Containers;
2019-08-10 18:22:45 +08:00
using osu.Framework.Graphics.Shapes;
using osu.Game.Graphics.UserInterface;
2019-08-10 18:53:34 +08:00
using osu.Game.Overlays.News;
2020-07-09 01:07:29 +08:00
using osu.Game.Overlays.News.Displays;
2019-08-10 18:22:45 +08:00
namespace osu.Game.Overlays
{
public class NewsOverlay : FullscreenOverlay<NewsHeader>
2019-08-10 18:22:45 +08:00
{
2020-07-12 20:45:48 +08:00
private readonly Bindable<string> article = new Bindable<string>(null);
private Container content;
private LoadingLayer loading;
private OverlayScrollContainer scrollFlow;
2020-01-24 17:24:35 +08:00
public NewsOverlay()
: base(OverlayColourScheme.Purple, new NewsHeader())
2020-01-24 17:24:35 +08:00
{
}
2019-08-10 18:22:45 +08:00
[BackgroundDependencyLoader]
private void load()
2019-08-10 18:22:45 +08:00
{
Children = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = ColourProvider.Background5,
2019-08-10 18:53:34 +08:00
},
scrollFlow = new OverlayScrollContainer
2019-08-10 18:53:34 +08:00
{
RelativeSizeAxes = Axes.Both,
ScrollbarVisible = false,
2019-08-10 18:53:34 +08:00
Child = new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
Children = new Drawable[]
{
Header.With(h =>
2020-07-12 20:45:48 +08:00
{
h.ShowFrontPage = ShowFrontPage;
}),
content = new Container
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
}
2019-08-10 18:53:34 +08:00
},
},
},
loading = new LoadingLayer(content),
2019-08-10 18:22:45 +08:00
};
2019-08-10 23:06:52 +08:00
}
protected override void LoadComplete()
{
base.LoadComplete();
2020-08-05 17:50:37 +08:00
// should not be run until first pop-in to avoid requesting data before user views.
article.BindValueChanged(onArticleChanged);
}
private bool displayUpdateRequired = true;
protected override void PopIn()
{
base.PopIn();
if (displayUpdateRequired)
{
article.TriggerChange();
displayUpdateRequired = false;
}
}
protected override void PopOutComplete()
{
base.PopOutComplete();
displayUpdateRequired = true;
}
public void ShowFrontPage()
{
2020-07-12 20:45:48 +08:00
article.Value = null;
Show();
}
public void ShowArticle(string slug)
{
2020-07-12 20:45:48 +08:00
article.Value = slug;
Show();
}
private CancellationTokenSource cancellationToken;
2020-07-12 20:45:48 +08:00
private void onArticleChanged(ValueChangedEvent<string> e)
{
cancellationToken?.Cancel();
loading.Show();
2020-07-12 20:45:48 +08:00
if (e.NewValue == null)
{
Header.SetFrontPage();
2020-07-09 06:26:56 +08:00
LoadDisplay(new FrontPageDisplay());
return;
}
Header.SetArticle(e.NewValue);
LoadDisplay(Empty());
}
protected void LoadDisplay(Drawable display)
{
scrollFlow.ScrollToStart();
LoadComponentAsync(display, loaded =>
{
content.Child = loaded;
loading.Hide();
}, (cancellationToken = new CancellationTokenSource()).Token);
}
protected override void Dispose(bool isDisposing)
2019-08-10 23:06:52 +08:00
{
cancellationToken?.Cancel();
base.Dispose(isDisposing);
2019-08-10 18:22:45 +08:00
}
}
}