Colton Idle
05/11/2022, 2:24 AMremember{}
is needed? Code in threadColton Idle
05/11/2022, 2:28 AMBox(
modifier =
Modifier.fillMaxWidth(1f)
.background(
brush =
when (status) {
Status.ABC,
Status.DEF -> {
Brush.verticalGradient(listOf(bunch of hex values))
}
<http://Status.XYZ|Status.XYZ> -> {
Brush.verticalGradient(listOf(bunch of hex values))
}
...
I'd like to extract the "when" statement into just a function so it doesn't have to pollute my composable.
So I basically did this
val colorsForStatus = colorsForStatus(status)
Box(
modifier =
Modifier.fillMaxWidth(1f)
.background(brush = Brush.verticalGradient(colorsForStatus)))
and the function itself (which is not a composable function)
fun colorsForStatus(status: String): List<Color> {
return when (status) {
Status.ABC,
Status.DEF -> {
Brush.verticalGradient(listOf(bunch of hex values))
}
<http://Status.XYZ|Status.XYZ> -> {
Brush.verticalGradient(listOf(bunch of hex values))
}
}
}
Colton Idle
05/11/2022, 2:28 AMval colorsForStatus = colorsForStatus(status)
in a remember {}?natario1
05/11/2022, 2:36 AMAdam Powell
05/11/2022, 2:47 AMColton Idle
05/11/2022, 2:48 AMnatario1
05/11/2022, 2:57 AMColton Idle
05/11/2022, 2:58 AMval myLookuptable = mapOf(Pair(Status.ABC, listOf(a bunch of hex values)))
if so, then I would just use it like this right?
myLookuptable.getOrDefault(status, listOf(default list of hex values))
Colton Idle
05/11/2022, 2:58 AMColton Idle
05/11/2022, 2:59 AMColton Idle
05/11/2022, 3:04 AMAdam Powell
05/11/2022, 3:14 AMfun brushFor(status: Status): Brush = myLookupTable[status] ?: defaultBrush
- the point is you only have one allocated brush per status for the life of the process and you reuse it. You don't need to remember a global 🙃Adam Powell
05/11/2022, 3:17 AMremember
something you're trading composition slot table space for computation time (and that computation time might come in the form of gc pressure if you're allocating big/complex object graphs) and for keyed remembers you're spending time comparing keys for equality to see if you need to do workColton Idle
05/11/2022, 3:18 AMAdam Powell
05/11/2022, 3:19 AMColton Idle
05/11/2022, 3:21 AMAdam Powell
05/11/2022, 3:39 AMjames
05/11/2022, 4:10 AMremember
took me some time to get my head around, but that little bit of trivia would’ve helped it “click” more quickly for me
petition to add that “fun fact” to the official docs 😁Colton Idle
05/11/2022, 1:27 PMAdam Powell
05/11/2022, 2:24 PM