Don't set null value to show front page

This commit is contained in:
Andrei Zavatski 2020-07-09 03:40:14 +03:00
parent dfa22b1e4c
commit 3ba8ec0fd7
3 changed files with 45 additions and 43 deletions

View File

@ -7,19 +7,21 @@ namespace osu.Game.Tests.Visual.Online
{ {
public class TestSceneNewsOverlay : OsuTestScene public class TestSceneNewsOverlay : OsuTestScene
{ {
private NewsOverlay news;
protected override bool UseOnlineAPI => true; protected override bool UseOnlineAPI => true;
protected override void LoadComplete() protected override void LoadComplete()
{ {
base.LoadComplete(); base.LoadComplete();
Add(news = new NewsOverlay());
AddStep(@"Show", news.Show);
AddStep(@"Hide", news.Hide);
AddStep(@"Show front page", () => news.ShowFrontPage()); NewsOverlay news;
AddStep(@"Custom article", () => news.Current.Value = "Test Article 101"); Add(news = new NewsOverlay());
AddStep("Show", news.Show);
AddStep("Hide", news.Hide);
AddStep("Show front page", () => news.ShowFrontPage());
AddStep("Custom article", () => news.ShowArticle("Test Article 101"));
AddStep("Custom article", () => news.ShowArticle("Test Article 102"));
} }
} }
} }

View File

@ -3,44 +3,46 @@
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using System;
namespace osu.Game.Overlays.News namespace osu.Game.Overlays.News
{ {
public class NewsHeader : BreadcrumbControlOverlayHeader public class NewsHeader : BreadcrumbControlOverlayHeader
{ {
private const string front_page_string = "frontpage"; public const string FRONT_PAGE_STRING = "frontpage";
public readonly Bindable<string> Post = new Bindable<string>(null); public readonly Bindable<string> Post = new Bindable<string>(FRONT_PAGE_STRING);
public Action ShowFrontPage;
public NewsHeader() public NewsHeader()
{ {
TabControl.AddItem(front_page_string); TabControl.AddItem(FRONT_PAGE_STRING);
Current.Value = FRONT_PAGE_STRING;
Current.ValueChanged += e => Current.BindValueChanged(onCurrentChanged);
{ Post.BindValueChanged(onPostChanged, true);
if (e.NewValue == front_page_string)
ShowFrontPage?.Invoke();
};
Post.ValueChanged += showPost;
} }
private void showPost(ValueChangedEvent<string> e) public void SetFrontPage() => Post.Value = FRONT_PAGE_STRING;
{
if (e.OldValue != null)
TabControl.RemoveItem(e.OldValue);
if (e.NewValue != null) public void SetArticle(string slug) => Post.Value = slug;
private void onCurrentChanged(ValueChangedEvent<string> current)
{ {
TabControl.AddItem(e.NewValue); if (current.NewValue == FRONT_PAGE_STRING)
Current.Value = e.NewValue; 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);
Current.Value = post.NewValue;
} }
else else
{ {
Current.Value = front_page_string; Current.Value = FRONT_PAGE_STRING;
} }
} }

View File

@ -15,10 +15,9 @@ namespace osu.Game.Overlays
{ {
public class NewsOverlay : FullscreenOverlay public class NewsOverlay : FullscreenOverlay
{ {
public readonly Bindable<string> Current = new Bindable<string>(null);
private Container content; private Container content;
private LoadingLayer loading; private LoadingLayer loading;
private NewsHeader header;
private OverlayScrollContainer scrollFlow; private OverlayScrollContainer scrollFlow;
public NewsOverlay() public NewsOverlay()
@ -29,8 +28,6 @@ namespace osu.Game.Overlays
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load() private void load()
{ {
NewsHeader header;
Children = new Drawable[] Children = new Drawable[]
{ {
new Box new Box
@ -49,10 +46,7 @@ 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,
@ -63,30 +57,34 @@ namespace osu.Game.Overlays
}, },
loading = new LoadingLayer(content), loading = new LoadingLayer(content),
}; };
header.Post.BindTo(Current);
} }
protected override void LoadComplete() protected override void LoadComplete()
{ {
base.LoadComplete(); base.LoadComplete();
Current.BindValueChanged(onCurrentChanged, true); header.Post.BindValueChanged(onPostChanged, true);
} }
public void ShowFrontPage() public void ShowFrontPage()
{ {
Current.Value = null; header.SetFrontPage();
Show();
}
public void ShowArticle(string slug)
{
header.SetArticle(slug);
Show(); Show();
} }
private CancellationTokenSource cancellationToken; private CancellationTokenSource cancellationToken;
private void onCurrentChanged(ValueChangedEvent<string> current) private void onPostChanged(ValueChangedEvent<string> post)
{ {
cancellationToken?.Cancel(); cancellationToken?.Cancel();
loading.Show(); loading.Show();
if (current.NewValue == null) if (post.NewValue == NewsHeader.FRONT_PAGE_STRING)
{ {
LoadDisplay(new FrontPageDisplay()); LoadDisplay(new FrontPageDisplay());
return; return;