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

Roar Gronmo

12/08/2019, 12:48 PM
When will it be possible to combine @Composable and Room ? I think its crucial that those two features can coexist. RG
a

Adam Powell

12/08/2019, 2:50 PM
Yes. Room can give you back both `Flow`s and `LiveData`s of query results, and compose can subscribe to those and render the results as they change.
You can do it today with some integration glue
Flow
is a bit of a challenge at the moment due to some incompatibilities between the version of the IR compiler the latest compose plugin is using and generating suspending code, but some clever module separation can work around it for now. We're pretty anxious to see these bugs get fixed soon
LiveData
is a little bit easier, let me see if I can dig up the snippet to use as an
observe()
effect for it
there it is:
Copy code
fun <T> observe(data: LiveData<T>) = effectOf<T?> {
    val result = +state<T?> { data.value }
    val observer = +memo { Observer<T> { result.value = it } }

    +onCommit(data) {
        data.observeForever(observer)
        onDispose { data.removeObserver(observer) }
    }

    result.value
}
usage is:
Copy code
val currentValue = +observe(myLiveData)
you'll get a recomposition each time the
LiveData
value changes, and the
LiveData
will have an active observer for the duration of that composition
which is to say, the `LiveData`s returned by room queries will continue updating until that part of your compose UI goes away
r

Roar Gronmo

12/08/2019, 3:01 PM
Thanx alot !!! @Adam Powell, I'll test this on Monday...
👍 1
a

Ash

12/08/2019, 5:34 PM
Just what I needed! Have been working on this over the weekend 🙏🏽
👍 1
a

Andrew Kelly

12/09/2019, 7:25 AM
@Roar Gronmo I used the +observe snippet of code successfully in a few talks I gave on Compose recently, just to show something more heavyweight compared to the existing @Model observable examples.
a

Ash

12/10/2019, 5:21 AM
@Andrew Kelly Do you have a link to the talks?
a

Andrew Kelly

12/10/2019, 5:22 AM
I did them at DevFest Melbourne, DevFest Sydney and DevFest Cape Town. I haven’t seen any uploads yet, I imagine they’re still processing them all.
2 Views