osu/osu.Game/Overlays/News/Sidebar/NewsSideBar.cs

104 lines
3.3 KiB
C#
Raw Normal View History

2021-05-10 06:53:52 +00:00
// 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.Linq;
2021-05-10 06:53:52 +00:00
namespace osu.Game.Overlays.News.Sidebar
{
2021-05-17 07:00:36 +00:00
public class NewsSidebar : CompositeDrawable
2021-05-10 06:53:52 +00:00
{
2021-05-11 12:33:27 +00:00
[Cached]
2021-05-10 06:53:52 +00:00
public readonly Bindable<APINewsSidebar> Metadata = new Bindable<APINewsSidebar>();
2021-05-17 07:02:21 +00:00
private FillFlowContainer<MonthSection> monthsFlow;
2021-05-10 06:53:52 +00:00
[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
{
Vertical = 20,
2021-05-10 06:53:52 +00:00
Left = 50,
Right = 30
},
Child = new FillFlowContainer
{
Direction = FillDirection.Vertical,
2021-05-11 13:51:59 +00:00
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
2021-05-10 06:53:52 +00:00
Spacing = new Vector2(0, 20),
Children = new Drawable[]
{
2021-05-11 12:33:27 +00:00
new YearsPanel(),
2021-05-17 07:02:21 +00:00
monthsFlow = new FillFlowContainer<MonthSection>
2021-05-10 06:53:52 +00:00
{
2021-05-11 13:51:59 +00:00
AutoSizeAxes = Axes.Y,
RelativeSizeAxes = Axes.X,
2021-05-10 06:53:52 +00:00
Direction = FillDirection.Vertical,
Spacing = new Vector2(0, 10)
}
}
}
}
};
}
protected override void LoadComplete()
{
base.LoadComplete();
Metadata.BindValueChanged(onMetadataChanged, true);
}
2021-05-10 06:53:52 +00:00
private void onMetadataChanged(ValueChangedEvent<APINewsSidebar> metadata)
{
monthsFlow.Clear();
2021-05-10 06:53:52 +00:00
if (metadata.NewValue == null)
return;
2021-05-10 06:53:52 +00:00
var allPosts = metadata.NewValue.NewsPosts;
2021-05-15 16:14:58 +00:00
if (allPosts?.Any() != true)
return;
var lookup = metadata.NewValue.NewsPosts.ToLookup(post => post.PublishedAt.Month);
var keys = lookup.Select(kvp => kvp.Key);
var sortedKeys = keys.OrderByDescending(k => k).ToList();
var year = metadata.NewValue.CurrentYear;
for (int i = 0; i < sortedKeys.Count; i++)
{
var month = sortedKeys[i];
var posts = lookup[month];
2021-05-17 07:02:21 +00:00
monthsFlow.Add(new MonthSection(month, year, posts)
{
IsOpen = { Value = i == 0 }
});
}
2021-05-10 06:53:52 +00:00
}
}
}