Improve handling of 12/24 hour timestamps on configuration change.

This fixes an edge case seen on ConversationFragment, where if the
device time format is switched between 12/24 hour format while the app
is running, the old time format will still be displayed when the app
is resumed.

This is due to a design flaw in `DateTimeFormatter.ofLocalizedTime`,
where the time format is statically cached and not updated upon
configuration change. The `LocalTime.formatHours()` extension method
was updated to no longer rely on the misbehaving `ofLocalTime` method.

In addition, `ConversationMessaageComputeWorkers.recomputeFormattedDate`
was designed to skip recomputing non-relative timestamps. This works
in most cases but not this specific edge case. A `force: Boolean` flag
was added to force all items to be updated. And the `force = true` flag
was passed upon `onResume` of the fragment.

Closes #14121
This commit is contained in:
Doug Melton
2025-05-01 21:05:54 -07:00
committed by Michelle Tang
parent 918b792d83
commit c865ed0cdc
3 changed files with 25 additions and 16 deletions

View File

@@ -1,7 +1,6 @@
package org.thoughtcrime.securesms.util
import android.content.Context
import android.os.Build
import android.text.format.DateFormat
import java.time.DayOfWeek
import java.time.Instant
@@ -13,7 +12,6 @@ import java.time.ZoneId
import java.time.ZoneOffset
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle
import java.time.temporal.WeekFields
import java.util.Locale
import java.util.concurrent.TimeUnit
@@ -91,14 +89,13 @@ fun Long.toLocalTime(zoneId: ZoneId = ZoneId.systemDefault()): LocalTime {
}
/**
* Formats [LocalTime] as localized time. For example, "8:00 AM"
* Formats [LocalTime] as localized time. For example, "1:45 PM" or "13:45"
*/
fun LocalTime.formatHours(context: Context): String {
return if (Build.VERSION.SDK_INT >= 26 || !DateFormat.is24HourFormat(context)) {
DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT).format(this)
} else {
DateTimeFormatter.ofPattern("HH:mm", Locale.getDefault()).format(this)
}
// We have to create our own pattern here, since the formatter instance returned by DateTimeFormatter.ofLocalizedTime() is looked up lazily, is immutable,
// and is not updated when the system's 24-hour time setting changes.
val pattern = if (DateFormat.is24HourFormat(context)) "HH:mm" else "h:mm a"
return DateTimeFormatter.ofPattern(pattern, Locale.getDefault()).format(this)
}
/**