Masoud Karimi
09/13/2023, 4:49 PMMaterialTheme
object and also its composable function. Something caught my attention. I don't understand why they are creating a copy
of the colorScheme
and then call the updateColorSchemeFrom
extension function again. I think they are the same. Am I missing something here?
https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/[…]ialTheme.kt;drc=bcaacc85477f959cac171b60258679b6244f512b;l=69
@Composable
fun MaterialTheme(
colorScheme: ColorScheme = MaterialTheme.colorScheme,
shapes: Shapes = MaterialTheme.shapes,
typography: Typography = MaterialTheme.typography,
content: @Composable () -> Unit
) {
val rememberedColorScheme = remember {
colorScheme.copy()
}.apply {
updateColorSchemeFrom(colorScheme) // This function also copy the values from colorScheme just like the copy function called inside the remember block.
}
val rippleIndication = rememberRipple()
val selectionColors = rememberTextSelectionColors(rememberedColorScheme)
CompositionLocalProvider(
LocalColorScheme provides rememberedColorScheme,
LocalIndication provides rippleIndication,
LocalRippleTheme provides MaterialRippleTheme,
LocalShapes provides shapes,
LocalTextSelectionColors provides selectionColors,
LocalTypography provides typography,
) {
ProvideTextStyle(value = typography.bodyLarge, content = content)
}
}
Stylianos Gakis
09/13/2023, 4:51 PMcolorScheme
changes, then the resulting colorScheme will have all of its internal values (which are MutableState objects) will be updated accordingly, and since they are state as I said before, all the composable functions that read them will recompose appropriately.
So I think tl;dr of this is that it’s an optimization, not for correctness. That’s my understandingMasoud Karimi
09/13/2023, 5:02 PMcolorScheme
? First, inside the remember block, and then inside the apply block. 🤔Stylianos Gakis
09/13/2023, 5:07 PMupdateColorSchemeFrom
when provided with the exact same colors (which it always is the case on first composition) will be a no-op, so it doesn’t matter.Stylianos Gakis
09/13/2023, 5:08 PMrememberUpdatedState
https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/[…]berUpdatedState&ss=androidx%2Fplatform%2Fframeworks%2FsupportMasoud Karimi
09/13/2023, 5:16 PMColorScheme
in the first composition. Then, on each recomposition, they just update the properties. Am I right?Stylianos Gakis
09/13/2023, 5:29 PMupdateColorSchemeFrom
is called again, since it’s just part of the composable itself, not inside a lambda or some other side-effect composable. It just happens every single recomposition. It’s more or less the same as if they wrote this
SideEffect {
rememberedColorScheme.updateColorSchemeFrom(colorScheme)
}
And they must be relying on the fact that updateColorSchemeFrom
shouldn’t be expensive if the same colorScheme is passed in there over and over again.Stylianos Gakis
09/13/2023, 5:32 PMMasoud Karimi
09/13/2023, 5:41 PMremember
and rememberUpdatedState
and I finally got the point. Thank u very much 👍
https://stackoverflow.com/a/70223293
https://proandroiddev.com/jetpack-compose-side-effects-iii-rememberupdatedstate-c8df7b90a01d