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

Arjan van Wieringen

04/29/2022, 9:52 AM
Where can I find some information about the Composable annotation and the corresponding compiler plugin? I am interested in using it for an own tree structure. I found two blog articles (https://arunkumar.dev/jetpack-compose-for-non-ui-tree-construction-and-code-generation/ and http://intelligiblebabble.com/compose-from-first-principles/). But I was hoping to find some more low-level information. Is there any available?
a

Adam Powell

04/29/2022, 1:52 PM
What are you looking for, more specifically?
a

Arjan van Wieringen

04/29/2022, 2:39 PM
How I could use the composable architecture and compiler for different things than UI. I read about the Applier for instance. But is there some broader API documentation apart from the source code and comments?
a

Adam Powell

04/29/2022, 2:43 PM
I think @Chris Arriola is working on some material based on the recent Google Maps library for Compose that uses its own composition type, but the API reference is the primary source for the moment. The Maps example is based on subcomposition rather than a standalone Recomposer; depending on which case you want the approach will be a little different
Are you interested out of general curiosity or do you have a specific use case in mind? (The answer will help us direct what kinds of documentation to add that the community is interested in)
a

Arjan van Wieringen

04/29/2022, 5:13 PM
General curiosity. I think it is an amazing library with a very well thought out architecture. A general high level architecture overview with some pointers to code samples would be great. Or some links to some repos out there that do some stuff with compose. However, I can imagine that it would take a lot of work, so I guess I’ll just deep dive into the API myself. It looked pretty well documented. And maybe I can write down some of my explorations myself.
a

Adam Powell

04/29/2022, 5:37 PM
ok, a couple pointers to get you started:
Start by writing an applier that can build and maintain your output. Write some matching
@Composable
functions that emit the node type for that applier; these will be the composables you build everything else out of
Construct a
Composition
using an instance of your applier plus a
CompositionContext
the
CompositionContext
can be obtained from
rememberCompositionContext()
to create a subcomposition of an existing composition, or you can construct your own
Recomposer
to drive compositions yourself
(
Recomposer
is a
CompositionContext
)
a

Arjan van Wieringen

04/29/2022, 5:41 PM
Thanks!
a

Adam Powell

04/29/2022, 5:43 PM
Calling
Recomposer.runRecomposeAndApplyChanges
is the main loop of recomposition, it listens for invalidations to the compositions registered with it and then awaits a frame from the calling `CoroutineContext`'s
MonotonicFrameClock
- you can see
BroadcastMonotonicFrameClock
for an example/one you can use in some situations. It will give you a callback once something wants a frame from it and then you can choose when to signal the frame to perform recomposition. Usually you'll want to do this right before you intend to do something with the recomposed output tree, when you want to get your applier to apply all potential changes
and that's about it
there's a
withRunningRecomposer
api that makes the setup of the
runRecomposeAndApplyChanges
part a bit more turnkey
a

Arjan van Wieringen

04/29/2022, 7:21 PM
Wow! Thanks! This is a great start
👍 2
a

Adam Powell

04/29/2022, 7:33 PM
Oh, and if you aren't also using something else like Compose UI that sets up periodic notifications of global snapshot changes, you might end up wanting to have something that does. Example: https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/[…]GlobalSnapshotManager.android.kt;l=38?q=GlobalSnapshotManager
this is what causes compositions (or any other snapshot observers) to be notified of batch snapshot changes that happen outside of an explicit use of a snapshot, e.g.
Snapshot.withMutableSnapshot { doStuff() }
c

Chris Arriola

05/03/2022, 8:40 PM
a bit late to the convo here but if you need some more code samples of using a custom applier Maps Compose has some examples. see: https://github.com/googlemaps/android-maps-compose/blob/main/maps-compose/src/main/java/com/google/maps/android/compose/MapApplier.kt#L33
👍🏼 1
a

Arjan van Wieringen

05/04/2022, 6:28 AM
@Chris Arriola Awesome, thanks
i

Icyrockton

05/05/2022, 3:53 PM
useful information thanksjetpack compose
5 Views