How can I reset certain states when some other sta...
# compose
m
How can I reset certain states when some other states change? I don't know if that's the best way to state my problem, so let me elaborate. I have a composable function that displays an image (well a box with an image inside), that supports double tap, pinch-zoom, panning and glide. All of the states like current scale level and offset are internal to the composable. It takes a ImageBitmap as its input. Now, in the main activity, there's the option to display different images. Now whenever I display a different image, I want to reset current zoom level and pan. Of course, when the ImageBitmap passed to my composable, only the displayed image is changed and any previous zoom and pan are retained. How do I reset these internal zoom and pan states, whenever ImageBitmap is changed, without exposing the internal states to the main activity?
m
I think that might work, but wouldn't it look inelegant? I don't really need to use the changed value of ImageBitmap, only that its value has changed.
Copy code
var scale by remember {
        derivedStateOf { 
              imageBitmap
              0f
        }
}
Would the code look something like this? Would it reset to 0 whenever imageBitmap changes?
o
m
No, I didn't know
remember
had a
key
argument. I think that's what I am looking for. So the code should look this, right?
Copy code
var scale by remember(imageBitmap) { 0f }
One other thing, it's a minor question but why are all the key arguments named
key1
?
key
doesn't seem to be a keyword in Kotlin.
o
I think you want
scale
to be a mutable state, so:
Copy code
var scale by remember(imageBitmap) { mutableStateOf(0f) }
There are also
key2
and
key3
, so that’s why they choose
key1
for name of the first key 🤷
👍 1
m
Oh right, I forgot to use
mutableStateOf
Thanks for all your help! 😊
👍 1