From 9603712aa1a21b930179533f788e36448c9ec8a1 Mon Sep 17 00:00:00 2001 From: Andrei Zavatski Date: Tue, 11 May 2021 15:33:27 +0300 Subject: [PATCH] Cache metadata in NewsSideBar --- .../Visual/Online/TestSceneNewsYearsPanel.cs | 32 ++++++++++++++----- osu.Game/Overlays/News/Sidebar/NewsSideBar.cs | 13 ++------ osu.Game/Overlays/News/Sidebar/YearsPanel.cs | 29 +++++++---------- 3 files changed, 38 insertions(+), 36 deletions(-) diff --git a/osu.Game.Tests/Visual/Online/TestSceneNewsYearsPanel.cs b/osu.Game.Tests/Visual/Online/TestSceneNewsYearsPanel.cs index 446cd06eea..031a98dd8b 100644 --- a/osu.Game.Tests/Visual/Online/TestSceneNewsYearsPanel.cs +++ b/osu.Game.Tests/Visual/Online/TestSceneNewsYearsPanel.cs @@ -5,6 +5,9 @@ using osu.Framework.Graphics; using osu.Game.Overlays.News.Sidebar; using osu.Framework.Allocation; using osu.Game.Overlays; +using osu.Framework.Bindables; +using osu.Game.Online.API.Requests.Responses; +using NUnit.Framework; namespace osu.Game.Tests.Visual.Online { @@ -13,22 +16,35 @@ namespace osu.Game.Tests.Visual.Online [Cached] private readonly OverlayColourProvider colourProvider = new OverlayColourProvider(OverlayColourScheme.Purple); - private readonly YearsPanel panel; + [Cached] + private readonly Bindable metadataBindable = new Bindable(); - public TestSceneNewsYearsPanel() + private YearsPanel panel; + + [SetUp] + public void SetUp() { - Add(panel = new YearsPanel + Child = panel = new YearsPanel { Anchor = Anchor.Centre, Origin = Anchor.Centre - }); + }; } - protected override void LoadComplete() + [Test] + public void TestMetadata() { - base.LoadComplete(); - AddStep("Load years", () => panel.Years = new[] { 1000, 2000, 3000, 4000 }); - AddStep("Load different years", () => panel.Years = new[] { 1001, 2001, 3001, 4001, 5001, 6001, 7001, 8001 }); + AddStep("Change metadata to null", () => metadataBindable.Value = null); + AddAssert("Panel is hidden", () => panel.IsPresent == false); + AddStep("Change metadata", () => metadataBindable.Value = metadata); + AddAssert("Panel is visible", () => panel.IsPresent == true); + AddStep("Change metadata to null", () => metadataBindable.Value = null); + AddAssert("Panel is hidden", () => panel.IsPresent == false); } + + private static readonly APINewsSidebar metadata = new APINewsSidebar + { + Years = new[] { 1001, 2001, 3001, 4001, 5001, 6001, 7001, 8001, 9001 } + }; } } diff --git a/osu.Game/Overlays/News/Sidebar/NewsSideBar.cs b/osu.Game/Overlays/News/Sidebar/NewsSideBar.cs index 4d2d3949bd..75726c5fed 100644 --- a/osu.Game/Overlays/News/Sidebar/NewsSideBar.cs +++ b/osu.Game/Overlays/News/Sidebar/NewsSideBar.cs @@ -14,9 +14,9 @@ namespace osu.Game.Overlays.News.Sidebar { public class NewsSideBar : CompositeDrawable { + [Cached] public readonly Bindable Metadata = new Bindable(); - private YearsPanel yearsPanel; private FillFlowContainer monthsFlow; [BackgroundDependencyLoader] @@ -49,7 +49,7 @@ namespace osu.Game.Overlays.News.Sidebar Spacing = new Vector2(0, 20), Children = new Drawable[] { - yearsPanel = new YearsPanel(), + new YearsPanel(), monthsFlow = new FillFlowContainer { AutoSizeAxes = Axes.Both, @@ -70,15 +70,6 @@ namespace osu.Game.Overlays.News.Sidebar { monthsFlow.Clear(); - if (metadata.NewValue == null) - { - yearsPanel.Hide(); - return; - } - - yearsPanel.Years = metadata.NewValue.Years; - yearsPanel.Show(); - if (metadata.NewValue != null) { var lookup = metadata.NewValue.NewsPosts.ToLookup(post => post.PublishedAt.Month); diff --git a/osu.Game/Overlays/News/Sidebar/YearsPanel.cs b/osu.Game/Overlays/News/Sidebar/YearsPanel.cs index 046e1804bd..23dd8d8a5e 100644 --- a/osu.Game/Overlays/News/Sidebar/YearsPanel.cs +++ b/osu.Game/Overlays/News/Sidebar/YearsPanel.cs @@ -12,28 +12,21 @@ using osu.Game.Graphics.Sprites; using System.Collections.Generic; using osu.Game.Graphics; using osu.Framework.Bindables; -using System.Collections.Specialized; +using osu.Game.Online.API.Requests.Responses; namespace osu.Game.Overlays.News.Sidebar { public class YearsPanel : CompositeDrawable { - public int[] Years - { - set - { - years.Clear(); - years.AddRange(value); - } - } - - private readonly BindableList years = new BindableList(); + private readonly Bindable metadata = new Bindable(); private FillFlowContainer flow; [BackgroundDependencyLoader] - private void load(OverlayColourProvider colourProvider) + private void load(OverlayColourProvider colourProvider, Bindable metadata) { + this.metadata.BindTo(metadata); + Width = 160; AutoSizeAxes = Axes.Y; Masking = true; @@ -64,14 +57,16 @@ namespace osu.Game.Overlays.News.Sidebar { base.LoadComplete(); - years.BindCollectionChanged((u, v) => + metadata.BindValueChanged(m => { - switch (v.Action) + if (m.NewValue == null) { - case NotifyCollectionChangedAction.Add: - flow.Children = years.Select(y => new YearButton(y)).ToArray(); - break; + Hide(); + return; } + + flow.Children = m.NewValue.Years.Select(y => new YearButton(y)).ToArray(); + Show(); }, true); }