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

@@ -665,6 +665,11 @@ class ConversationFragment :
outState.putBoolean(SAVED_STATE_IS_SEARCH_REQUESTED, isSearchRequested)
}
override fun onStart() {
super.onStart()
recomputeMessageDates(forceUpdate = true)
}
override fun onResume() {
super.onResume()
@@ -1173,12 +1178,7 @@ class ConversationFragment :
getVoiceNoteMediaController().voiceNotePlaybackState.observe(viewLifecycleOwner, inputPanel.playbackStateObserver)
val conversationUpdateTick = ConversationUpdateTick {
disposables += ConversationMessageComputeWorkers.recomputeFormattedDate(
requireContext(),
adapter.currentList.filterIsInstance<ConversationMessageElement>()
).observeOn(AndroidSchedulers.mainThread()).subscribeBy { adapter.updateTimestamps() }
}
val conversationUpdateTick = ConversationUpdateTick { recomputeMessageDates() }
viewLifecycleOwner.lifecycle.addObserver(conversationUpdateTick)
@@ -1188,6 +1188,17 @@ class ConversationFragment :
}
}
private fun recomputeMessageDates(forceUpdate: Boolean = false) {
disposables += ConversationMessageComputeWorkers
.recomputeFormattedDate(
context = requireContext(),
items = adapter.currentList.filterIsInstance<ConversationMessageElement>(),
forceUpdate = forceUpdate
)
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy { adapter.updateTimestamps() }
}
private fun initializeInlineSearch() {
inlineQueryController.onOrientationChange(resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE)

View File

@@ -21,13 +21,14 @@ object ConversationMessageComputeWorkers {
fun recomputeFormattedDate(
context: Context,
items: List<ConversationMessageElement>
items: List<ConversationMessageElement>,
forceUpdate: Boolean = false
): Single<Boolean> {
return Single.fromCallable {
var hasUpdatedProperties = false
for (item in items) {
val oldDate = item.conversationMessage.computedProperties.formattedDate
if (oldDate.isRelative) {
if (oldDate.isRelative || forceUpdate) {
val newDate = ConversationMessage.getFormattedDate(context, item.conversationMessage.messageRecord)
item.conversationMessage.computedProperties.formattedDate = newDate
hasUpdatedProperties = true

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)
}
/**