Refactor NewsOverlay to use displays logic

This commit is contained in:
Andrei Zavatski
2020-07-08 18:24:13 +03:00
parent de4c22c709
commit 49d998c8db
3 changed files with 53 additions and 29 deletions

View File

@ -7,37 +7,40 @@ using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Game.Graphics;
using osu.Game.Graphics.UserInterface;
using osu.Game.Overlays.News;
namespace osu.Game.Overlays
{
public class NewsOverlay : FullscreenOverlay
{
private NewsHeader header;
private Container<NewsContent> content;
public readonly Bindable<string> Current = new Bindable<string>(null);
private Container content;
private LoadingLayer loading;
private OverlayScrollContainer scrollFlow;
public NewsOverlay()
: base(OverlayColourScheme.Purple)
{
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
private void load()
{
NewsHeader header;
Children = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = colours.PurpleDarkAlternative
Colour = ColourProvider.Background5,
},
new OverlayScrollContainer
scrollFlow = new OverlayScrollContainer
{
RelativeSizeAxes = Axes.Both,
ScrollbarVisible = false,
Child = new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
@ -49,7 +52,7 @@ namespace osu.Game.Overlays
{
ShowFrontPage = ShowFrontPage
},
content = new Container<NewsContent>
content = new Container
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
@ -57,25 +60,16 @@ namespace osu.Game.Overlays
},
},
},
loading = new LoadingLayer(content),
};
header.Post.BindTo(Current);
Current.TriggerChange();
}
private CancellationTokenSource loadContentCancellation;
protected void LoadAndShowContent(NewsContent newContent)
protected override void LoadComplete()
{
content.FadeTo(0.2f, 300, Easing.OutQuint);
loadContentCancellation?.Cancel();
LoadComponentAsync(newContent, c =>
{
content.Child = c;
content.FadeIn(300, Easing.OutQuint);
}, (loadContentCancellation = new CancellationTokenSource()).Token);
base.LoadComplete();
Current.BindValueChanged(onCurrentChanged, true);
}
public void ShowFrontPage()
@ -83,5 +77,37 @@ namespace osu.Game.Overlays
Current.Value = null;
Show();
}
private CancellationTokenSource cancellationToken;
private void onCurrentChanged(ValueChangedEvent<string> current)
{
cancellationToken?.Cancel();
loading.Show();
if (current.NewValue == null)
{
LoadDisplay(Empty());
return;
}
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)
{
cancellationToken?.Cancel();
base.Dispose(isDisposing);
}
}
}