Stylianos Gakis
07/05/2023, 9:08 AM@Composable
fun customDateTimeFormatter(): DateTimeFormatter {
val locale = getLocale()
return remember(locale) {
DateTimeFormatterBuilder().stuff().toFormatter(locale)
}
}
But I wonder is this the right place do do just this instead (without the remember)
@Composable
@ReadOnlyComposable
fun customDateTimeFormatter(): DateTimeFormatter {
val locale = getLocale()
return DateTimeFormatterBuilder.stuff().toFormatter(locale)
}
}
Would this give me the same guarantees that
• I only need to evaluate this once and it gets remembered properly, so I don’t call the formatter builder on each recomposition
• If Locale changes for some reason, it will in fact recompose and give me the updated valueStylianos Gakis
07/05/2023, 9:09 AMgetLocale()
is also a @ReadOnlyComposable
. It’s this
@Composable
@ReadOnlyComposable
fun getLocale(): Locale {
val configuration = LocalConfiguration.current
return ConfigurationCompat.getLocales(configuration).get(0) ?: LocaleListCompat.getAdjustedDefault()[0]!!
}
Albert Chang
07/05/2023, 2:47 PMI only need to evaluate this once and it gets remembered properly, so I don’t call the formatter builder on each recompositionI don't know where the expectation comes from, but I don't think Compose will automatically remember the return value for you and
@ReadOnlyComposable
doesn't change this.Stylianos Gakis
07/05/2023, 3:09 PMgetLocale()
return value does not change either.
But you are right, it’s a wrong assumption to make. I think composable functions that return values aren’t skipped in the same way that other composable functions are right? I’ve tried to find some source to read about how value returning composables are different but haven’t been able to yet.
What I am really looking for is just the rememberX
style of functions. So something like this:
@Composable
fun rememberCustomDateTimeFormatter(): DateTimeFormatter {
val locale = getLocale()
return remember(locale) { DateTimeFormatterBuilder().stuff().toFormatter(locale) }
}
At least that’s what I think I want here 😄