https://kotlinlang.org logo
#compose
Title
# compose
j

Jan Skrasek

08/29/2020, 8:21 PM
Material's Colors has all its fields as a state - documented as an optimization to when the color changes not to recompose everything, but only composables depending on the particular color. But I can't find any place that would modify any color. So is this a preoptimized for some user-land use-cases? And what they could be?
a

Adam Powell

08/29/2020, 8:23 PM
Animating theme colors
j

Jan Skrasek

08/29/2020, 8:32 PM
Is such animation used in compose? The only one I can think of is going from light to dark. Or any other reason?
l

Louis Pullen-Freilich [G]

08/29/2020, 8:33 PM
Jetcaster animates the
primary
color of the app depending on the podcast artwork, for a real-life example https://github.com/android/compose-samples/tree/master/Jetcaster
👍 1
j

Jan Skrasek

08/29/2020, 8:51 PM
Thanks, great example!
I've taken a look and it doesn't seems that Jetcaster would modify the state, instead it copy() the colors & provides new ambient value, isn't it? https://github.com/android/compose-samples/blob/dcc400da3fb8e09b99b5fb21fced77c60e04393d/Jetcaster/app/src/main/java/com/example/jetcaster/util/DynamicTheming.kt#L63-L67
l

Louis Pullen-Freilich [G]

08/29/2020, 9:15 PM
Exactly,
MaterialTheme
internally modifies the state, it's a performance optimization for the internals, but isn't consumer facing https://cs.android.com/androidx/platform/frameworks/support/+/androidx-master-dev:compose/material/material/src/commonMain/kotlin/androidx/compose/material/MaterialTheme.kt;l=63 Internally we just update the initial theme provided, with colors from the new object, instead of replacing the object in the ambient value
👍 1
j

Jan Skrasek

08/29/2020, 9:16 PM
Oh, I see it. I think the trick is here:
Copy code
val rememberedColors = remember {
    // TODO: b/162450508 remove the unnecessary .copy() here when it isn't needed to ensure that
    // we don't skip the updateColorsFrom call
    colors.copy()
}.apply { updateColorsFrom(colors) }