```@OptIn(ExperimentalMaterialApi::class) private ...
# compose
u
Copy code
@OptIn(ExperimentalMaterialApi::class)
private val MyRippleConfiguration =
    RippleConfiguration(color = Color.Red, rippleAlpha = MyRippleAlpha)
In the docs the ripple config instance is cached (in a top level val) what is the preferred way to cache this if the given color comes from
Material.colors
which is
@Composable
? I cannot just access
Color.x
statically like they do The color is effectivelly static all time, so having a single instance for all is reasonable, but
remember { .. }
probably doesn' make much sense, since that would scope it to composition, and I have multiple components that need this at the same time
l
This seems more like you are trying to change ripple behavior across your application,
LocalRippleConfiguration
is more for specific per component overrides, it isn’t intended to be used to define complex theming logic. Material ripples use
LocalContentColor
- if this doesn’t work for you at an app-wide level, then you probably would be better off building your own ripple / components that use your ripple
u
No no, I'm creating a
SelectableCard
which is a wrapper around
Card
which adds border when selected - and the border is blue, so I want the ripple to be blue as well Only for this one component
Copy code
@OptIn(ExperimentalMaterialApi::class)
@Composable
fun SelectableCard(
    modifier: Modifier = Modifier,
    selected: Boolean,
    enabled: Boolean = true,
    backgroundColor: Color = MaterialTheme.colors.surface,
    onClick: () -> Unit,
    content: @Composable () -> Unit
) {
    val strokeColor = if (selected) {
        MaterialTheme.colors.secondary
    } else {
        MaterialTheme.colors.whatever
    }
    CompositionLocalProvider(
        LocalRippleConfiguration provides RippleConfiguration(
            color = MaterialTheme.colors.secondary
        ),
    ) {
        Card(
            onClick = onClick,
            modifier = modifier,
            border = BorderStroke(1.dp, SolidColor(value = strokeColor)),
            backgroundColor = backgroundColor,
            elevation = 0.dp,
            content = content
        )
    }
}
all there is to it and the question is now how should I cache the
RippleConfiguration
instance, or if I should at all?
l
I see, I don’t think there’s much value in caching it then
u
(I simplified the snippet now)
l
It’s just a simple class, so it should be ok to create a new one when the theme colors change
u
okay so some sort of lazy-init caching is not work much?
okay great thank you
l
given that the ripple configuration object is just a simple class with two properties, any caching you try and do is probably going to be more expensive than just creating a new one