Neal Sanche
02/09/2020, 7:24 PM// general purpose observe effect. this will likely be provided by LiveData. effect API for
// compose will also simplify soon.
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
}
Ref: https://medium.com/swlh/android-mvi-with-jetpack-compose-b0890f5156acAdam Powell
02/09/2020, 7:28 PM= effectOf<T?>
to, : T?
in the signature (normal return value) and add @Composable
as a function annotation, then remove the `+`s from the effect calls in the function body and add an explicit return
keyword at the end. Change memo
to remember
as it was renamed.Zsolt
02/09/2020, 7:29 PMAdam Powell
02/09/2020, 7:29 PMNeal Sanche
02/09/2020, 7:30 PMAdam Powell
02/09/2020, 7:33 PM@Composable
val <T> LiveData<T>.latestValue: T? get() { ... }
if you'd prefer.Neal Sanche
02/09/2020, 7:56 PMe: java.lang.IllegalStateException: Backend Internal error: Exception during code generation
Element is unknownThe root cause java.util.NoSuchElementException was thrown at: androidx.compose.plugins.kotlin.compiler.lower.ComposableCallTransformer.irComposableExpr(ComposableCallTransformer.kt:1362)
Caused by: java.util.NoSuchElementException: Collection contains no element matching the predicate.
I guess I'll go on a hunt for what might be causing that.composeOptions {
kotlinCompilerVersion "1.3.61-dev-withExperimentalGoogleExtensions-20200129"
kotlinCompilerExtensionVersion "0.1.0-dev04"
}
Into my build.gradle.