https://kotlinlang.org logo
Title
m

Michael Paus

05/29/2021, 5:14 AM
Does Compose have a concept to explicitly ask for a recomposition? I am currently using some dummy state for that but that looks awkward.
z

Zach Klippenstein (he/him) [MOD]

05/29/2021, 5:27 AM
Why do you need to trigger recomposition? Typically if this comes up there’s something else that’s probably wrong
4
a

Adam Powell

05/29/2021, 5:48 AM
What @Zach Klippenstein (he/him) [MOD] said. That said, if you still think you need it,
currentRecomposeScope()
will give you an object with an invalidate method on it
Recomposition is not an event handler
m

Michael Paus

05/29/2021, 12:47 PM
Well, I think I still need it. You have to consider that in the real world not all code is written from scratch and is not always written with Compose in mind. I have some code which encapsulates a lot of canvas drawing in a generic way and maintains its own state internally. I have however a callback which I can use to be notified about repaint requests and that leads me to the question of how to best hook this up with Compose.
a

Adam Powell

05/29/2021, 1:52 PM
True invalidation is fine, some people tend to use recomposition as a replacement for setting up a flow collector or other event-driven actor
If you're only invalidating drawing you'll want to avoid a full recomposition like the currentRecomposeScope mentioned above will trigger, I don't recall if there's an analog in the drawing APIs or not. If not you could use something like assigning Unit to a mutableStateOf with neverEqualPolicy or something similar
1
m

Michael Paus

05/29/2021, 7:24 PM
That sounds a bit like what I am doing already right now and what looks ugly. If it does not exist yet, wouldn’t you think that introducing an explicit API for that purpose would make sense? (hint)