Skip to content

Commit 5af9bb7

Browse files
authored
Merge pull request #35495 from Joehuu/fix-drawable-date-update
Fix `DrawableDate` not updating
2 parents 3c6fb14 + 4c60df2 commit 5af9bb7

File tree

2 files changed

+43
-22
lines changed

2 files changed

+43
-22
lines changed

osu.Game.Tests/Visual/UserInterface/TestSceneDrawableDate.cs

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
// See the LICENCE file in the repository root for full licence text.
33

44
using System;
5+
using System.Linq;
6+
using NUnit.Framework;
57
using osu.Framework.Graphics;
68
using osu.Framework.Graphics.Containers;
79
using osu.Framework.Graphics.Shapes;
10+
using osu.Framework.Testing;
811
using osu.Game.Graphics;
912
using osuTK;
1013
using osuTK.Graphics;
@@ -13,25 +16,35 @@ namespace osu.Game.Tests.Visual.UserInterface
1316
{
1417
public partial class TestSceneDrawableDate : OsuTestScene
1518
{
16-
public TestSceneDrawableDate()
19+
[SetUpSteps]
20+
public void SetUpSteps()
1721
{
18-
Child = new FillFlowContainer
22+
AddStep("Create 7 dates", () =>
1923
{
20-
Direction = FillDirection.Vertical,
21-
AutoSizeAxes = Axes.Both,
22-
Origin = Anchor.Centre,
23-
Anchor = Anchor.Centre,
24-
Children = new Drawable[]
24+
Child = new FillFlowContainer
2525
{
26-
new PokeyDrawableDate(DateTimeOffset.Now.Subtract(TimeSpan.FromSeconds(60))),
27-
new PokeyDrawableDate(DateTimeOffset.Now.Subtract(TimeSpan.FromSeconds(55))),
28-
new PokeyDrawableDate(DateTimeOffset.Now.Subtract(TimeSpan.FromSeconds(50))),
29-
new PokeyDrawableDate(DateTimeOffset.Now),
30-
new PokeyDrawableDate(DateTimeOffset.Now.Add(TimeSpan.FromSeconds(60))),
31-
new PokeyDrawableDate(DateTimeOffset.Now.Add(TimeSpan.FromSeconds(65))),
32-
new PokeyDrawableDate(DateTimeOffset.Now.Add(TimeSpan.FromSeconds(70))),
33-
}
34-
};
26+
Direction = FillDirection.Vertical,
27+
AutoSizeAxes = Axes.Both,
28+
Origin = Anchor.Centre,
29+
Anchor = Anchor.Centre,
30+
Children = new Drawable[]
31+
{
32+
new PokeyDrawableDate(DateTimeOffset.Now.Subtract(TimeSpan.FromSeconds(60))),
33+
new PokeyDrawableDate(DateTimeOffset.Now.Subtract(TimeSpan.FromSeconds(55))),
34+
new PokeyDrawableDate(DateTimeOffset.Now.Subtract(TimeSpan.FromSeconds(50))),
35+
new PokeyDrawableDate(DateTimeOffset.Now),
36+
new PokeyDrawableDate(DateTimeOffset.Now.Add(TimeSpan.FromSeconds(60))),
37+
new PokeyDrawableDate(DateTimeOffset.Now.Add(TimeSpan.FromSeconds(65))),
38+
new PokeyDrawableDate(DateTimeOffset.Now.Add(TimeSpan.FromSeconds(70))),
39+
}
40+
};
41+
});
42+
}
43+
44+
[Test]
45+
public void TestSecondsUpdate()
46+
{
47+
AddUntilStep("4th date says \"2 seconds ago\"", () => this.ChildrenOfType<DrawableDate>().ElementAt(3).Current.Value == "2 seconds ago");
3548
}
3649

3750
private partial class PokeyDrawableDate : CompositeDrawable

osu.Game/Graphics/DrawableDate.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using osu.Framework.Allocation;
66
using osu.Framework.Graphics;
77
using osu.Framework.Graphics.Cursor;
8+
using osu.Framework.Graphics.Sprites;
89
using osu.Framework.Localisation;
910
using osu.Game.Graphics.Sprites;
1011
using osu.Game.Utils;
@@ -80,7 +81,7 @@ private void updateTimeWithReschedule()
8081

8182
public DateTimeOffset TooltipContent => Date;
8283

83-
private class HumanisedDate : IEquatable<HumanisedDate>, ILocalisableStringData
84+
private class HumanisedDate : ILocalisableStringData
8485
{
8586
public readonly DateTimeOffset Date;
8687

@@ -89,11 +90,18 @@ public HumanisedDate(DateTimeOffset date)
8990
Date = date;
9091
}
9192

92-
public bool Equals(HumanisedDate? other)
93-
=> other?.Date != null && Date.Equals(other.Date);
94-
95-
public bool Equals(ILocalisableStringData? other)
96-
=> other is HumanisedDate otherDate && Equals(otherDate);
93+
/// <remarks>
94+
/// Humanizer formats the <see cref="Date"/> relative to the local computer time.
95+
/// Therefore, replacing a <see cref="HumanisedDate"/> instance with another instance of the class with the same <see cref="Date"/>
96+
/// should have the effect of replacing and re-formatting the text.
97+
/// Including <see cref="Date"/> in equality members would stop this from happening, as <see cref="SpriteText.Text"/>
98+
/// has equality-based early guards to prevent redundant text replaces.
99+
/// Thus, instances of these class just compare <see langword="false"/> to any <see cref="ILocalisableStringData"/> to ensure re-formatting happens correctly.
100+
/// There are "technically" more "correct" ways to do this (like also including the current time into equality checks),
101+
/// but they are simultaneously functionally equivalent to this and overly convoluted.
102+
/// This is a private hack-job of a wrapper around humanizer anyway.
103+
/// </remarks>
104+
public bool Equals(ILocalisableStringData? other) => false;
97105

98106
public string GetLocalised(LocalisationParameters parameters) => HumanizerUtils.Humanize(Date);
99107

0 commit comments

Comments
 (0)