1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 15:47:26 +08:00

Implement NewsSideBar component

This commit is contained in:
Andrei Zavatski 2021-05-10 09:53:52 +03:00
parent 7971a2ef48
commit 4b97224932
4 changed files with 251 additions and 0 deletions

View File

@ -0,0 +1,114 @@
// 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.Collections.Generic;
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Overlays;
using osu.Game.Overlays.News.Sidebar;
namespace osu.Game.Tests.Visual.Online
{
public class TestSceneNewsSideBar : OsuTestScene
{
[Cached]
private readonly OverlayColourProvider colourProvider = new OverlayColourProvider(OverlayColourScheme.Purple);
private NewsSideBar sidebar;
[Test]
public void TestCreateEmpty()
{
createSidebar(null);
}
[Test]
public void TestCreateWithData()
{
createSidebar(metadata);
}
[Test]
public void TestDataChange()
{
createSidebar(null);
AddStep("Add data", () =>
{
if (sidebar != null)
sidebar.Metadata.Value = metadata;
});
}
private void createSidebar(APINewsSidebar metadata) => AddStep("Create", () => Child = sidebar = new NewsSideBar
{
Metadata = { Value = metadata }
});
private static APINewsSidebar metadata = new APINewsSidebar
{
CurrentYear = 2021,
Years = new[]
{
2021,
2020,
2019,
2018,
2017,
2016,
2015,
2014,
2013
},
NewsPosts = new List<APINewsPost>
{
new APINewsPost
{
Title = "(Mar) Short title",
PublishedAt = new DateTime(2021, 3, 1)
},
new APINewsPost
{
Title = "(Mar) Oh boy that's a long post title I wonder if it will break anything",
PublishedAt = new DateTime(2021, 3, 1)
},
new APINewsPost
{
Title = "(Mar) Medium title, nothing to see here",
PublishedAt = new DateTime(2021, 3, 1)
},
new APINewsPost
{
Title = "(Feb) Short title",
PublishedAt = new DateTime(2021, 2, 1)
},
new APINewsPost
{
Title = "(Feb) Oh boy that's a long post title I wonder if it will break anything",
PublishedAt = new DateTime(2021, 2, 1)
},
new APINewsPost
{
Title = "(Feb) Medium title, nothing to see here",
PublishedAt = new DateTime(2021, 2, 1)
},
new APINewsPost
{
Title = "Short title",
PublishedAt = new DateTime(2021, 1, 1)
},
new APINewsPost
{
Title = "Oh boy that's a long post title I wonder if it will break anything",
PublishedAt = new DateTime(2021, 1, 1)
},
new APINewsPost
{
Title = "Medium title, nothing to see here",
PublishedAt = new DateTime(2021, 1, 1)
}
}
};
}
}

View File

@ -11,5 +11,8 @@ namespace osu.Game.Online.API.Requests
{
[JsonProperty("news_posts")]
public IEnumerable<APINewsPost> NewsPosts;
[JsonProperty("news_sidebar")]
public APINewsSidebar SidebarMetadata;
}
}

View File

@ -0,0 +1,20 @@
// 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 Newtonsoft.Json;
using System.Collections.Generic;
namespace osu.Game.Online.API.Requests.Responses
{
public class APINewsSidebar
{
[JsonProperty("current_year")]
public int CurrentYear { get; set; }
[JsonProperty("news_posts")]
public IEnumerable<APINewsPost> NewsPosts { get; set; }
[JsonProperty("years")]
public int[] Years { get; set; }
}
}

View File

@ -0,0 +1,114 @@
// 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 osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics;
using osu.Game.Online.API.Requests.Responses;
using osu.Framework.Graphics.Shapes;
using osuTK;
using System.Collections.Generic;
using System;
namespace osu.Game.Overlays.News.Sidebar
{
public class NewsSideBar : CompositeDrawable
{
public readonly Bindable<APINewsSidebar> Metadata = new Bindable<APINewsSidebar>();
private YearsPanel yearsPanel;
private FillFlowContainer<MonthPanel> monthsFlow;
[BackgroundDependencyLoader]
private void load(OverlayColourProvider colourProvider)
{
RelativeSizeAxes = Axes.Y;
Width = 250;
InternalChildren = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = colourProvider.Background4
},
new Container
{
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding
{
Top = 20,
Left = 50,
Right = 30
},
Child = new FillFlowContainer
{
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Direction = FillDirection.Vertical,
AutoSizeAxes = Axes.Both,
Spacing = new Vector2(0, 20),
Children = new Drawable[]
{
yearsPanel = new YearsPanel(),
monthsFlow = new FillFlowContainer<MonthPanel>
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Vertical,
Spacing = new Vector2(0, 10)
}
}
}
}
};
}
protected override void LoadComplete()
{
base.LoadComplete();
Metadata.BindValueChanged(metadata =>
{
monthsFlow.Clear();
if (metadata.NewValue == null)
{
yearsPanel.Hide();
return;
}
yearsPanel.Years = metadata.NewValue.Years;
yearsPanel.Show();
if (metadata.NewValue != null)
{
var dict = new Dictionary<int, List<APINewsPost>>();
foreach (var p in metadata.NewValue.NewsPosts)
{
var month = p.PublishedAt.Month;
if (dict.ContainsKey(month))
dict[month].Add(p);
else
{
dict.Add(month, new List<APINewsPost>(new[] { p }));
}
}
bool isFirst = true;
foreach (var keyValuePair in dict)
{
monthsFlow.Add(new MonthPanel(new DateTime(metadata.NewValue.CurrentYear, keyValuePair.Key, 1), keyValuePair.Value.ToArray())
{
IsOpen = { Value = isFirst }
});
isFirst = false;
}
}
}, true);
}
}
}