Merge pull request #16526 from bdach/news-sidebar-wrong-headers

Fix news sidebar assuming returned posts are always from given year
This commit is contained in:
Dan Balasescu 2022-01-20 13:47:21 +09:00 committed by GitHub
commit e9939199e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 88 additions and 9 deletions

View File

@ -38,6 +38,14 @@ namespace osu.Game.Tests.Visual.Online
AddUntilStep("No month sections were created", () => !sidebar.ChildrenOfType<MonthSection>().Any());
}
[Test]
public void TestMetadataWithMultipleYears()
{
AddStep("Add data spanning multiple years", () => sidebar.Metadata.Value = metadata_with_multiple_years);
AddUntilStep("2022 month sections exist", () => sidebar.ChildrenOfType<MonthSection>().Any(s => s.Year == 2022));
AddUntilStep("2021 month sections exist", () => sidebar.ChildrenOfType<MonthSection>().Any(s => s.Year == 2021));
}
[Test]
public void TestYearsPanelVisibility()
{
@ -133,6 +141,74 @@ namespace osu.Game.Tests.Visual.Online
NewsPosts = Array.Empty<APINewsPost>()
};
// see https://osu.ppy.sh/docs/index.html#get-news-listing:
// "NewsPost collections queried by year will also include posts published in November and December of the previous year if the current date is the same year and before April."
private static readonly APINewsSidebar metadata_with_multiple_years = new APINewsSidebar
{
CurrentYear = 2022,
Years = new[]
{
2022,
2021,
2020,
2019,
2018,
2017,
2016,
2015,
2014,
2013
},
NewsPosts = new List<APINewsPost>
{
new APINewsPost
{
Title = "(Mar 2022) Short title",
PublishedAt = new DateTime(2022, 3, 1)
},
new APINewsPost
{
Title = "(Mar 2022) Oh boy that's a long post title I wonder if it will break anything",
PublishedAt = new DateTime(2022, 3, 1)
},
new APINewsPost
{
Title = "(Feb 2022) Medium title, nothing to see here",
PublishedAt = new DateTime(2022, 2, 1)
},
new APINewsPost
{
Title = "(Feb 2022) Short title",
PublishedAt = new DateTime(2022, 2, 1)
},
new APINewsPost
{
Title = "(Jan 2022) Oh boy that's a long post title I wonder if it will break anything",
PublishedAt = new DateTime(2022, 1, 1)
},
new APINewsPost
{
Title = "(Jan 2022) Medium title, nothing to see here",
PublishedAt = new DateTime(2022, 1, 1)
},
new APINewsPost
{
Title = "(Jan 2022) Short title",
PublishedAt = new DateTime(2022, 1, 1)
},
new APINewsPost
{
Title = "(Dec 2021) Surprise, the last year's not gone yet",
PublishedAt = new DateTime(2021, 12, 1)
},
new APINewsPost
{
Title = "(Nov 2021) Same goes for November",
PublishedAt = new DateTime(2021, 11, 1)
}
}
};
private class TestNewsSidebar : NewsSidebar
{
public Action<int> YearChanged;

View File

@ -24,16 +24,21 @@ namespace osu.Game.Overlays.News.Sidebar
{
public class MonthSection : CompositeDrawable
{
public int Year { get; private set; }
public int Month { get; private set; }
public readonly BindableBool Expanded = new BindableBool();
private const int animation_duration = 250;
private Sample sampleOpen;
private Sample sampleClose;
public readonly BindableBool Expanded = new BindableBool();
public MonthSection(int month, int year, IEnumerable<APINewsPost> posts)
{
Debug.Assert(posts.All(p => p.PublishedAt.Month == month && p.PublishedAt.Year == year));
Year = year;
Month = month;
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
Masking = true;

View File

@ -56,19 +56,17 @@ namespace osu.Game.Overlays.News.Sidebar
if (allPosts?.Any() != true)
return;
var lookup = metadata.NewValue.NewsPosts.ToLookup(post => post.PublishedAt.Month);
var lookup = metadata.NewValue.NewsPosts.ToLookup(post => (post.PublishedAt.Month, post.PublishedAt.Year));
var keys = lookup.Select(kvp => kvp.Key);
var sortedKeys = keys.OrderByDescending(k => k).ToList();
int year = metadata.NewValue.CurrentYear;
var sortedKeys = keys.OrderByDescending(k => k.Year).ThenByDescending(k => k.Month).ToList();
for (int i = 0; i < sortedKeys.Count; i++)
{
int month = sortedKeys[i];
var posts = lookup[month];
var key = sortedKeys[i];
var posts = lookup[key];
monthsFlow.Add(new MonthSection(month, year, posts)
monthsFlow.Add(new MonthSection(key.Month, key.Year, posts)
{
Expanded = { Value = i == 0 }
});