Refactor NewsHeader

This commit is contained in:
Andrei Zavatski 2020-07-12 15:45:48 +03:00
parent 3601a2d93f
commit 598e48678e
2 changed files with 41 additions and 32 deletions

View File

@ -1,6 +1,7 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using System;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Graphics; using osu.Framework.Graphics;
@ -8,41 +9,42 @@ namespace osu.Game.Overlays.News
{ {
public class NewsHeader : BreadcrumbControlOverlayHeader public class NewsHeader : BreadcrumbControlOverlayHeader
{ {
public const string FRONT_PAGE_STRING = "frontpage"; private const string front_page_string = "frontpage";
public readonly Bindable<string> Post = new Bindable<string>(FRONT_PAGE_STRING); public Action ShowFrontPage;
private readonly Bindable<string> article = new Bindable<string>(null);
public NewsHeader() public NewsHeader()
{ {
TabControl.AddItem(FRONT_PAGE_STRING); TabControl.AddItem(front_page_string);
Current.Value = FRONT_PAGE_STRING;
Current.BindValueChanged(onCurrentChanged);
Post.BindValueChanged(onPostChanged, true);
}
public void SetFrontPage() => Post.Value = FRONT_PAGE_STRING; Current.BindValueChanged(e =>
public void SetArticle(string slug) => Post.Value = slug;
private void onCurrentChanged(ValueChangedEvent<string> current)
{
if (current.NewValue == FRONT_PAGE_STRING)
Post.Value = FRONT_PAGE_STRING;
}
private void onPostChanged(ValueChangedEvent<string> post)
{
if (post.OldValue != FRONT_PAGE_STRING)
TabControl.RemoveItem(post.OldValue);
if (post.NewValue != FRONT_PAGE_STRING)
{ {
TabControl.AddItem(post.NewValue); if (e.NewValue == front_page_string)
Current.Value = post.NewValue; ShowFrontPage?.Invoke();
});
article.BindValueChanged(onArticleChanged, true);
}
public void SetFrontPage() => article.Value = null;
public void SetArticle(string slug) => article.Value = slug;
private void onArticleChanged(ValueChangedEvent<string> e)
{
if (e.OldValue != null)
TabControl.RemoveItem(e.OldValue);
if (e.NewValue != null)
{
TabControl.AddItem(e.NewValue);
Current.Value = e.NewValue;
} }
else else
{ {
Current.Value = FRONT_PAGE_STRING; Current.Value = front_page_string;
} }
} }

View File

@ -15,6 +15,8 @@ namespace osu.Game.Overlays
{ {
public class NewsOverlay : FullscreenOverlay public class NewsOverlay : FullscreenOverlay
{ {
private readonly Bindable<string> article = new Bindable<string>(null);
private Container content; private Container content;
private LoadingLayer loading; private LoadingLayer loading;
private NewsHeader header; private NewsHeader header;
@ -46,7 +48,10 @@ namespace osu.Game.Overlays
Direction = FillDirection.Vertical, Direction = FillDirection.Vertical,
Children = new Drawable[] Children = new Drawable[]
{ {
header = new NewsHeader(), header = new NewsHeader
{
ShowFrontPage = ShowFrontPage
},
content = new Container content = new Container
{ {
RelativeSizeAxes = Axes.X, RelativeSizeAxes = Axes.X,
@ -62,34 +67,36 @@ namespace osu.Game.Overlays
protected override void LoadComplete() protected override void LoadComplete()
{ {
base.LoadComplete(); base.LoadComplete();
header.Post.BindValueChanged(onPostChanged, true); article.BindValueChanged(onArticleChanged, true);
} }
public void ShowFrontPage() public void ShowFrontPage()
{ {
header.SetFrontPage(); article.Value = null;
Show(); Show();
} }
public void ShowArticle(string slug) public void ShowArticle(string slug)
{ {
header.SetArticle(slug); article.Value = slug;
Show(); Show();
} }
private CancellationTokenSource cancellationToken; private CancellationTokenSource cancellationToken;
private void onPostChanged(ValueChangedEvent<string> post) private void onArticleChanged(ValueChangedEvent<string> e)
{ {
cancellationToken?.Cancel(); cancellationToken?.Cancel();
loading.Show(); loading.Show();
if (post.NewValue == NewsHeader.FRONT_PAGE_STRING) if (e.NewValue == null)
{ {
header.SetFrontPage();
LoadDisplay(new FrontPageDisplay()); LoadDisplay(new FrontPageDisplay());
return; return;
} }
header.SetArticle(e.NewValue);
LoadDisplay(Empty()); LoadDisplay(Empty());
} }