Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timestamps in room list inconsistently use absolute and relative formatting #2679

Closed
marwing opened this issue Apr 10, 2024 · 3 comments
Closed
Labels
A-Room-List O-Frequent Affects or can be seen by most users regularly or impacts most users' first experience S-Minor Impairs non-critical functionality or suitable workarounds exist T-Defect

Comments

@marwing
Copy link

marwing commented Apr 10, 2024

Steps to reproduce

  1. Have a room in the room list where the latest event (that's shown in the room list) was yesterday but less than 24 hours ago.
  2. Have another room in the room list where the latest relevant event was today.
  3. Look at the timestamp for these room in the room list

This is very easy to reproduce relatively early in the morning with a medium size account.

Outcome

What did you expect?

Consistently formatted timestamps (always "x hours ago", "yesterday", ... or always "hh:mm", "yesterday", ...)

What happened instead?

Room timestamps mix absolute and relative representation for "recent" events.
In the screenshot, the top three rooms show the absolute time because their latest relevant events were "today" (when I took the screenshot). The three rooms below that show a relative timestamp because their latest relevant events were yesterday but are younger than 24 hours. The remaining rooms show "yesterday", because their latest relevant events were yesterday, and they are older than 24 hours.
IMG_3857-obfuscated

Note that "yesterday" is obviously a relative timestamp as well, however, this feels correct and is also what Apple's apps (at least mail and messages) do. (They do "hh:mm", "yesterday", "day of the week", "date".)

Additional context

I asked this in the matrix room which resulted in a brief discussion with @pixlwave and @stefanceriu. From this discussion I take that this is known but unintentional behavior and I was asked to open an issue.

I was bored, so I looked at the code to see if something jumps out at me, and I think I found the source of this behavior. I never wrote any meaningful amount of Swift, though, let alone SwiftUI, so take this with a grain of salt. Also, I don't know if this is all already known, so please ignore this paragraph if it is.
The function Date.formattedMinimal is used here to format timestamps for the room list.
It delegates all formatting for dates that are "yesterday" to the Foundation framework, which seemingly was intended as a way to localize the string "yesterday". Instead, however, the Date.RelativeFormatStyle formats dates as "x hours ago" if they are less than 24 hours ago. In combination with the first branch in that function that forces "hh:mm" formatting for dates from "today" this produces the described behavior.

Date formatting is also inconsistent across features in the app. Date.formattedMinimal is also used for the reactions, but not for read receipts (I hope I found the right locations).

Your phone model

iPhone 13 mini / iPad Pro

Operating system version

iOS/iPadOS 17.4.1

Application version

1.6.2 (102)

Homeserver

Synapse 1.104.0 / Proxy 0.99.15

Will you send logs?

No

@marwing
Copy link
Author

marwing commented Apr 10, 2024

(Sorry for hacking your issue template)

@marwing
Copy link
Author

marwing commented Apr 10, 2024

After some digging and experimenting, I think this patch should produce what I believe to be the intended behavior of the function. I can't test this in the app itself, as the mac I have access to is too old to build it, but I tested the function in isolation and it seems to work:

diff --git a/ElementX/Sources/Other/Extensions/Date.swift b/ElementX/Sources/Other/Extensions/Date.swift
index 1e292c39..ed25b243 100644
--- a/ElementX/Sources/Other/Extensions/Date.swift
+++ b/ElementX/Sources/Other/Extensions/Date.swift
@@ -26,7 +26,10 @@ extension Date {
             return formatted(date: .omitted, time: .shortened)
         } else if calendar.isDateInYesterday(self) {
             // Simply "Yesterday" if it was yesterday.
-            return formatted(Date.RelativeFormatStyle(presentation: .named, capitalizationContext: .beginningOfSentence))
+            let formatter = RelativeDateTimeFormatter()
+            formatter.dateTimeStyle = .named
+            formatter.formattingContext = .beginningOfSentence
+            return formatter.localizedString(from: DateComponents(day: -1))
         } else if let sixDaysAgo = calendar.date(byAdding: .day, value: -6, to: calendar.startOfDay(for: .now)),
                   sixDaysAgo <= self {
             // The named day if it was in the last 6 days.

@stefanceriu stefanceriu added A-Room-List S-Minor Impairs non-critical functionality or suitable workarounds exist O-Frequent Affects or can be seen by most users regularly or impacts most users' first experience labels Apr 11, 2024
stefanceriu added a commit that referenced this issue Jun 18, 2024
@stefanceriu
Copy link
Member

#2937 should take care of it. Thanks for the ticket and the code! I went a slightly different route but the idea is the same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Room-List O-Frequent Affects or can be seen by most users regularly or impacts most users' first experience S-Minor Impairs non-critical functionality or suitable workarounds exist T-Defect
Projects
None yet
Development

No branches or pull requests

2 participants