1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-16 23:42:54 +08:00
osu-lazer/osu.Game/Overlays/NewsOverlay.cs

168 lines
4.7 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;
using System.Threading;
2019-08-10 23:06:52 +08:00
using osu.Framework.Bindables;
2019-08-10 18:22:45 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
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;
using osu.Game.Overlays.News.Sidebar;
2019-08-10 18:22:45 +08:00
namespace osu.Game.Overlays
{
2021-01-18 16:13:38 +08:00
public class NewsOverlay : OnlineOverlay<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 readonly Container sidebarContainer;
private readonly NewsSidebar sidebar;
private readonly Container content;
2021-05-20 13:11:42 +08:00
private CancellationTokenSource cancellationToken;
2021-05-20 13:12:34 +08:00
private bool displayUpdateRequired = true;
2020-01-24 17:24:35 +08:00
public NewsOverlay()
: base(OverlayColourScheme.Purple, false)
2020-01-24 17:24:35 +08:00
{
Child = new GridContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
RowDimensions = new[]
{
new Dimension(GridSizeMode.AutoSize)
},
ColumnDimensions = new[]
{
new Dimension(GridSizeMode.AutoSize),
new Dimension()
},
Content = new[]
{
new Drawable[]
{
sidebarContainer = new Container
{
AutoSizeAxes = Axes.X,
Child = sidebar = new NewsSidebar()
},
content = new Container
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y
}
}
}
};
2020-01-24 17:24:35 +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);
}
2021-05-20 14:15:46 +08:00
protected override NewsHeader CreateHeader() => new NewsHeader { ShowFrontPage = ShowFrontPage };
2021-01-18 15:48:12 +08:00
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 ShowYear(int year)
{
2021-05-20 13:11:42 +08:00
loadFrontPage(year);
Show();
}
public void ShowArticle(string slug)
{
2020-07-12 20:45:48 +08:00
article.Value = slug;
Show();
}
2021-05-20 13:12:34 +08:00
protected void LoadDisplay(Drawable display)
{
ScrollFlow.ScrollToStart();
LoadComponentAsync(display, loaded => content.Child = loaded, (cancellationToken = new CancellationTokenSource()).Token);
2021-05-20 13:12:34 +08:00
}
protected override void UpdateAfterChildren()
{
base.UpdateAfterChildren();
sidebarContainer.Height = DrawHeight;
sidebarContainer.Y = Math.Clamp(ScrollFlow.Current - Header.DrawHeight, 0, Math.Max(ScrollFlow.ScrollContent.DrawHeight - DrawHeight - Header.DrawHeight, 0));
}
2021-05-20 14:15:46 +08:00
private void onArticleChanged(ValueChangedEvent<string> article)
{
2021-05-20 14:15:46 +08:00
if (article.NewValue == null)
loadFrontPage();
2021-05-20 13:11:42 +08:00
else
2021-05-20 14:15:46 +08:00
loadArticle(article.NewValue);
}
private void loadFrontPage(int? year = null)
{
2021-05-20 13:11:42 +08:00
beginLoading();
Header.SetFrontPage();
var page = new ArticleListing(year);
page.SidebarMetadataUpdated += metadata => Schedule(() =>
{
sidebar.Metadata.Value = metadata;
Loading.Hide();
});
LoadDisplay(page);
}
private void loadArticle(string article)
{
2021-05-20 13:11:42 +08:00
beginLoading();
Header.SetArticle(article);
// Temporary, should be handled by ArticleDisplay later
LoadDisplay(Empty());
Loading.Hide();
}
2021-05-20 13:11:42 +08:00
private void beginLoading()
{
cancellationToken?.Cancel();
Loading.Show();
}
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
}
}
}