Berkeli Alashov
06/25/2021, 12:17 AM.copy()
in `remember { colors.copy() }`:
@Composable
fun ProvideAppColors(
colors: AppColors,
content: @Composable () -> Unit
) {
val appColors = remember { colors.copy() }.apply { update(colors) }
CompositionLocalProvider(
LocalAppColors provides appColors,
content = content)
}
Without .copy()
, it only works for the first change in state.
Full code: https://gist.github.com/alashow/e67a75d662a13ce5529317bde3e241c2Sean McQuillan [G]
06/25/2021, 12:20 AMBerkeli Alashov
06/25/2021, 12:21 AMSean McQuillan [G]
06/25/2021, 12:22 AMSean McQuillan [G]
06/25/2021, 12:22 AMAppTheme
is the source of truth about the AppColorsSean McQuillan [G]
06/25/2021, 12:23 AMSean McQuillan [G]
06/25/2021, 12:23 AMdarkTheme: Boolean
into a AppColors
every time it recomposesBerkeli Alashov
06/25/2021, 12:24 AMLocalAppColors.current
doesn't get updatedSean McQuillan [G]
06/25/2021, 12:25 AMSean McQuillan [G]
06/25/2021, 12:27 AMAlbert Chang
06/25/2021, 12:33 AMSean McQuillan [G]
06/25/2021, 12:34 AMSean McQuillan [G]
06/25/2021, 12:34 AMSean McQuillan [G]
06/25/2021, 12:38 AMremember
without a key, there will only ever be one AppColors
object referenced lower in the tree, and the colors which are represented by MutableState
will be independently observedSean McQuillan [G]
06/25/2021, 12:39 AMSean McQuillan [G]
06/25/2021, 12:40 AM.copy()
in there, then you're mutating the globals, so when you try to update it again it fails.
e.g.
1. Provide DarkAppColors
2. Update DarkAppColors
to LightAppColors
3. Update DarkAppColors
to DarkAppColors
(which now has the same values as LightAppColors
)Sean McQuillan [G]
06/25/2021, 12:46 AM