feat: a chat list clock that says only as much as it has to
The one timestamp format this app had, `toFormattedTimeAndDateString`, writes "14:05 6 Sep 2026". That is right for a message bubble, where it is the only clock on a line the reader has already stopped at. A chat list row is not that. Its job is to place a message relative to now, in whatever width is left after the room's name and the preview of what was said in it -- and a full date on this morning's message spends all of that saying "today" the long way. So the new format gets coarser the further back it goes, and never coarser than the reader can still resolve: - today, the time of day. Anything less cannot order two of today's rooms. - yesterday, named. A date here is a small arithmetic problem to read. - the rest of the last week, an abbreviated weekday. It stops at six days because the seventh is this weekday again, and "Sun" on a message from last Sunday reads as today. - inside this year, day and month. Past a week the weekday has stopped saying anything. - beyond it, the year as well, for the same reason one rung up: day and month repeat. Reading a rung too far is the failure mode and it is silent -- nothing about "Sun" admits which Sunday it means -- so every boundary is a test. Each one is anchored to the local day rather than to a fixed instant, because the boundaries are local midnights and the test would otherwise pass or fail on the machine's zone. A timestamp ahead of `now` deliberately falls through to a date rather than a time. Relay clocks disagree and an event can arrive stamped in the future; rendering that as "14:05" files it under a today it does not belong to. `now` is a parameter defaulting to `Clock.System.now()`, which is the whole reason any of the above is testable without a clock abstraction. It is sampled once per composition, so a list left open across midnight goes on saying "14:05" until something recomposes it -- acceptable for a list that recomposes on every arriving message, and not worth a ticker to fix. Six new tests, one per rung plus the future case. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,11 +1,15 @@
|
||||
package press.mantra.compose.extensions
|
||||
|
||||
import kotlinx.datetime.LocalDate
|
||||
import kotlinx.datetime.LocalDateTime
|
||||
import kotlinx.datetime.TimeZone
|
||||
import kotlinx.datetime.daysUntil
|
||||
import kotlinx.datetime.format
|
||||
import kotlinx.datetime.format.DayOfWeekNames
|
||||
import kotlinx.datetime.format.MonthNames
|
||||
import kotlinx.datetime.format.char
|
||||
import kotlinx.datetime.toLocalDateTime
|
||||
import kotlin.time.Clock
|
||||
import kotlin.time.Instant
|
||||
|
||||
fun Instant.toFormattedTimeAndDateString(): String {
|
||||
@@ -26,4 +30,59 @@ fun Instant.toFormattedTimeAndDateString(): String {
|
||||
year()
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A chat list's clock: the least that still says which line is newer.
|
||||
*
|
||||
* The row already carries the message itself, so the timestamp only has to place it —
|
||||
* and a full date on today's message crowds out the words it belongs to. Coarser the
|
||||
* further back it goes, and never coarser than the reader can resolve: a weekday is
|
||||
* unambiguous for six days and no longer, and a bare day and month for a year.
|
||||
*
|
||||
* A timestamp ahead of [now] gets a date rather than a time. Relay clocks disagree and
|
||||
* an event can arrive stamped in the future; rendering that as "14:05" would put it in
|
||||
* a today it does not belong to.
|
||||
*/
|
||||
fun Instant.toChatListTimestampString(now: Instant = Clock.System.now()): String {
|
||||
val timeZone = TimeZone.currentSystemDefault()
|
||||
val dateTime = toLocalDateTime(timeZone)
|
||||
val today = now.toLocalDateTime(timeZone).date
|
||||
val daysAgo = dateTime.date.daysUntil(today)
|
||||
|
||||
return when {
|
||||
daysAgo == 0 -> dateTime.format(
|
||||
LocalDateTime.Format {
|
||||
hour()
|
||||
char(':')
|
||||
minute()
|
||||
}
|
||||
)
|
||||
|
||||
daysAgo == 1 -> "Yesterday"
|
||||
|
||||
daysAgo in 2..6 -> dateTime.date.format(
|
||||
LocalDate.Format {
|
||||
dayOfWeek(DayOfWeekNames.ENGLISH_ABBREVIATED)
|
||||
}
|
||||
)
|
||||
|
||||
dateTime.year == today.year -> dateTime.date.format(
|
||||
LocalDate.Format {
|
||||
day()
|
||||
char(' ')
|
||||
monthName(MonthNames.ENGLISH_ABBREVIATED)
|
||||
}
|
||||
)
|
||||
|
||||
else -> dateTime.date.format(
|
||||
LocalDate.Format {
|
||||
day()
|
||||
char(' ')
|
||||
monthName(MonthNames.ENGLISH_ABBREVIATED)
|
||||
char(' ')
|
||||
year()
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user