I’m trying to display dates in either 24 or 12 hou...
# compose
a
I’m trying to display dates in either 24 or 12 hour format based on user’s locale and preferences, but I’m noticing that changing date format in settings doesn’t cause a recomposition like I would expect… I tried reading
LocalConfiguration.current
like
resources()
does (https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/ui/ui/src/androidMain/kotlin/androidx/compose/ui/res/Resources.android.kt;l=31-34?q=Resources.android) and I have
configChanges
added to manifest.
Copy code
@Composable
fun LocalDateTime.localized(): String {
    LocalConfiguration.current // was hoping this would recompose when it changes, like resources() call
    LocalContext.current // same here
    val pattern = if (DateFormat.is24HourFormat(LocalContext.current)) {
        "H:mm"
    } else {
        "h:mm a"
    }
    // format the date:
anyone have any ideas?
a
The time format is a system setting, not a part of configuration.
If you really want to you can check here for how to convert a system setting into a flow. I'm not sure it'll also work for the time format setting, though.
a
Thanks, Albert! That makes sense… I don’t know if it matters enough to go through the trouble of converting to a flow… the correct time format is used after any other recomposition, so it should be hardly noticeable and also barely annoying. quick follow up to:
The time format is a system setting, not a part of configuration.
How did you know this? Is there somewhere in source I can look this up?
a
From the source of
DateFormat
it looks like it depends on the system setting, and the locale as a fallback. I think another option here is listening for the ACTION_TIME_CHANGED broadcast as a signal for when this might change
a
Thanks, Alex 😄 ! that makes sense. when I originally looked at the source, I didn’t realize
Settings.System.getStringForUser
wasn’t something that was part of
Configuration
I just confirmed that when I change the locale of the device (for example from English (United States), which uses 12 hour format, to Español (España), which uses 24 hour format) recomposition is triggered, because
LocalConfiguration.current
changes. Changing just the setting for “use 24 hour format” doesn’t cause config to change, so no recomposition 👍
221 Views