Davide Bertola
11/29/2020, 3:02 PMDavide Bertola
11/29/2020, 3:03 PMliveObject.observeAsState()
returns a State<T>
which is not something that can be subscribed toBrandon Trautmann
11/29/2020, 3:15 PMBrandon Trautmann
11/29/2020, 3:16 PMNote: observeAsState observes a LiveData<T> and returns a State<T> object that is updated whenever the LiveData changes. State<T> is an observable type that Jetpack Compose can use directly.
observeAsState will observe the LiveData only while it is in the Composition.
Davide Bertola
11/29/2020, 3:20 PMDavide Bertola
11/29/2020, 3:20 PMDavide Bertola
11/29/2020, 3:21 PMDavide Bertola
11/29/2020, 3:24 PM@Composable
fun <R, T : R> LiveData<T>.observeAsState(initial: R): State<R> {
val lifecycleOwner = LifecycleOwnerAmbient.current
val observer = remember { DisposableObserver<R, T>(initial, lifecycleOwner) }
observer.source = this
return observer.state
}
Halil Ozercan
11/29/2020, 3:44 PMDavide Bertola
11/29/2020, 3:45 PMAdam Powell
11/29/2020, 3:50 PMDavide Bertola
11/29/2020, 3:53 PMDavide Bertola
11/29/2020, 4:30 PMDavide Bertola
11/29/2020, 4:33 PMliveData.observe(this) { recompose() }
Davide Bertola
11/29/2020, 4:33 PMAdam Powell
11/29/2020, 4:44 PMsnapshotFlow {}
here: https://cs.android.com/androidx/platform/frameworks/support/+/androidx-master-dev:compose/runtime/runtime/src/commonMain/kotlin/androidx/compose/runtime/snapshots/SnapshotFlow.ktAdam Powell
11/29/2020, 4:45 PMAdam Powell
11/29/2020, 4:46 PMval unregisterApplyObserver = Snapshot.registerApplyObserver { changed, _ ->
appliedChanges.offer(changed)
}
Adam Powell
11/29/2020, 4:46 PMchanged
is the set of snapshot-backed objects (e.g. mutableStateOf
) that changed in a single transactionDavide Bertola
11/29/2020, 4:47 PMDavide Bertola
11/29/2020, 4:48 PMAdam Powell
11/29/2020, 4:50 PMAdam Powell
11/29/2020, 4:52 PMDavide Bertola
11/29/2020, 4:53 PMDavide Bertola
11/29/2020, 4:53 PMDavide Bertola
11/29/2020, 4:53 PMDavide Bertola
11/29/2020, 4:56 PMDavide Bertola
11/29/2020, 4:58 PMDavide Bertola
11/29/2020, 4:58 PMDavide Bertola
11/29/2020, 5:16 PM