https://kotlinlang.org logo
#compose
Title
# compose
j

Justin Breitfeller

01/28/2022, 2:39 PM
Is there a Modifier that lets me wrap the content that is about to be created in a CompositionLocalProvider. I.E. If i apply a modifier to a composable, I’d like the child content to be able to access that set CompositionLocal.
a

Adam Powell

01/28/2022, 3:09 PM
Nope. Modifiers are part of the conceptual nesting structure of layout nodes but not of the composition that builds them. What are you trying to do? There might be another mechanism to achieve the same API result. ModifierLocals were recently added to support some of these use cases, but they're specifically available to the later layout/draw phases after composition.
j

Justin Breitfeller

01/28/2022, 3:19 PM
Thanks Adam. So what I’m trying to do is implement an analytics modifier that will receive and event object. Then, when a view is on the screen it would perform an action with that event object. Where the local comes in is that I’d like to be able to have a stack of any tracked view above the currently tracked view so i have context where it is. I was looking at ModifierLocals but I was having trouble understanding exactly how I would use them in my case. I can have each modifier provide the current stack, but the consumer felt funny to me since it operates with a callback mechanism and I’d have to link that into some other modifier (when detecting the shown state)
a

Adam Powell

01/28/2022, 4:20 PM
yes, it's quite low level and intended for building other abstractions on top of (e.g. compose's focus handling) but it sounds like you have the basic data flow right. Since you'll be dealing with layout visibility you shouldn't have any data dependency loops.
j

Justin Breitfeller

01/28/2022, 5:12 PM
So your opinion is to give ModifierLocals a go? I’ll have to play with them and see how it works. It sounds like I have to chain the modifier local consumer into a layout listener modifier?
6 Views