Use lookup instead of dictionary to distribute posts

This commit is contained in:
Andrei Zavatski 2021-05-11 15:12:04 +03:00
parent 711e7ba860
commit e736240a06
2 changed files with 10 additions and 23 deletions

View File

@ -26,7 +26,7 @@ namespace osu.Game.Overlays.News.Sidebar
private readonly FillFlowContainer postsFlow; private readonly FillFlowContainer postsFlow;
public MonthPanel(List<APINewsPost> posts) public MonthPanel(IEnumerable<APINewsPost> posts)
{ {
Width = 160; Width = 160;
Masking = true; Masking = true;
@ -38,7 +38,7 @@ namespace osu.Game.Overlays.News.Sidebar
Spacing = new Vector2(0, 5), Spacing = new Vector2(0, 5),
Children = new Drawable[] Children = new Drawable[]
{ {
new DropdownButton(posts[0].PublishedAt) new DropdownButton(posts.ElementAt(0).PublishedAt)
{ {
IsOpen = { BindTarget = IsOpen } IsOpen = { BindTarget = IsOpen }
}, },

View File

@ -8,7 +8,7 @@ using osu.Framework.Graphics;
using osu.Game.Online.API.Requests.Responses; using osu.Game.Online.API.Requests.Responses;
using osu.Framework.Graphics.Shapes; using osu.Framework.Graphics.Shapes;
using osuTK; using osuTK;
using System.Collections.Generic; using System.Linq;
namespace osu.Game.Overlays.News.Sidebar namespace osu.Game.Overlays.News.Sidebar
{ {
@ -81,30 +81,17 @@ namespace osu.Game.Overlays.News.Sidebar
if (metadata.NewValue != null) if (metadata.NewValue != null)
{ {
var dict = new Dictionary<int, List<APINewsPost>>(); var lookup = metadata.NewValue.NewsPosts.ToLookup(post => post.PublishedAt.Month);
foreach (var p in metadata.NewValue.NewsPosts) var keys = lookup.Select(kvp => kvp.Key);
var sortedKeys = keys.OrderByDescending(k => k).ToList();
for (int i = 0; i < sortedKeys.Count; i++)
{ {
var month = p.PublishedAt.Month; monthsFlow.Add(new MonthPanel(lookup[sortedKeys[i]])
if (dict.ContainsKey(month))
dict[month].Add(p);
else
{ {
dict.Add(month, new List<APINewsPost>(new[] { p })); IsOpen = { Value = i == 0 }
}
}
bool isFirst = true;
foreach (var keyValuePair in dict)
{
monthsFlow.Add(new MonthPanel(keyValuePair.Value)
{
IsOpen = { Value = isFirst }
}); });
isFirst = false;
} }
} }
}, true); }, true);