1
0
mirror of https://github.com/ppy/osu.git synced 2025-03-06 18:27:18 +08:00
osu-lazer/osu.Game/Overlays/NewsOverlay.cs

100 lines
2.5 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;
2019-08-10 17:06:52 +02:00
using osu.Framework.Bindables;
2019-08-10 12:22:45 +02:00
using osu.Framework.Graphics;
2019-08-10 12:53:34 +02:00
using osu.Game.Overlays.News;
2020-07-08 20:07:29 +03:00
using osu.Game.Overlays.News.Displays;
2019-08-10 12:22:45 +02:00
namespace osu.Game.Overlays
{
2021-01-18 11:13:38 +03:00
public class NewsOverlay : OnlineOverlay<NewsHeader>
2019-08-10 12:22:45 +02:00
{
2020-07-12 15:45:48 +03:00
private readonly Bindable<string> article = new Bindable<string>(null);
2020-01-24 12:24:35 +03:00
public NewsOverlay()
: base(OverlayColourScheme.Purple, false)
2020-01-24 12:24:35 +03:00
{
}
protected override void LoadComplete()
{
base.LoadComplete();
2020-08-05 18:50:37 +09:00
// should not be run until first pop-in to avoid requesting data before user views.
article.BindValueChanged(onArticleChanged);
}
2021-01-18 10:48:12 +03:00
protected override NewsHeader CreateHeader() => new NewsHeader
{
ShowFrontPage = ShowFrontPage
};
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 15:45:48 +03:00
article.Value = null;
Show();
}
public void ShowArticle(string slug)
{
2020-07-12 15:45:48 +03:00
article.Value = slug;
Show();
}
private CancellationTokenSource cancellationToken;
2020-07-12 15:45:48 +03:00
private void onArticleChanged(ValueChangedEvent<string> e)
{
cancellationToken?.Cancel();
2021-01-18 10:48:12 +03:00
Loading.Show();
2020-07-12 15:45:48 +03:00
if (e.NewValue == null)
{
Header.SetFrontPage();
2020-07-09 01:26:56 +03:00
LoadDisplay(new FrontPageDisplay());
return;
}
Header.SetArticle(e.NewValue);
LoadDisplay(Empty());
}
protected void LoadDisplay(Drawable display)
{
2021-01-18 10:48:12 +03:00
ScrollFlow.ScrollToStart();
LoadComponentAsync(display, loaded =>
{
2021-01-18 10:48:12 +03:00
Child = loaded;
Loading.Hide();
}, (cancellationToken = new CancellationTokenSource()).Token);
}
protected override void Dispose(bool isDisposing)
2019-08-10 17:06:52 +02:00
{
cancellationToken?.Cancel();
base.Dispose(isDisposing);
2019-08-10 12:22:45 +02:00
}
}
}