I have another question. I have a composable funct...
# compose
a
I have another question. I have a composable function defining some geometries. Is there a way to "remember" it and avoid recomposition?
s
I think this is what the “remember*” functions are for, convenience funs which wrap a remember. You can look at an example here https://cs.android.com/androidx/platform/tools/dokka-devsite-plugin/+/master:testDat[…]ndation/lazy/LazyListState.kt;l=48-59?q=rememberLazyListState. Is this what you’re looking for?
a
remember function does not allow composables inside it
I think I can work arround it so I do not need to store composables in the state, but I wonder if it is possible.
s
Do you need to be inside a composable context in order to get those geometries? Sounds like it may be possible to instead go this route and have that remembered class provide @Composable getters like
currentDestination
does if you need access to locals and such
a
I want to remember a painter for VectorImage. I need a composable context to create it, but if I use composable context, I can't remember it.
I think I can move painter creation to the place, where it is rendered. But I still wonder if it is possible to cache composables.
Ha, it is not that easy. In order to draw image on canvas, I need to create a painter and it requires composable scope. Draw scope is not composable.
s
How are you creating that painter? I’m trying to understand why you can’t remember it. Usually when there are such functions, there are also matching remember* functions for it like here.
a
Yes. I use rememberVectorPainter. The problem is that I can't store painter in the
remember
cache because it requires composable scope and I can't create a painter inside the canvas draw scope because it requires composable scope.
So how I am supposed to pass image as a state?
The only way I found is to catch the state outside of canvas scope, add additional painter cache to it and then pass it to canvas. Looks ugly
s
I’m not gonna lie I’m confused about the situation. Without seeing the code I think I can’t quite follow sorry 😕
a
The code is public. but it is actually pretty easy. I need to draw VectorImage in canvas. For that I need this VectorImage as a state. In order to draw it on canvas I need to create Painter with
rememberVectorPainter
. It could not be created inside canvas drawScope because it is not composable. But I also can't store it in the state because state also can't contain composable.