Chris Fillmore
11/18/2021, 10:29 PMflowWithLifecycle
, and I’m wondering if it’s wise (some code in thread)@Composable
fun <T> Flow<T>.lifecycleAware(
minActiveLifecycleState: Lifecycle.State = Lifecycle.State.STARTED,
): Flow<T> {
val lifecycleOwner = LocalLifecycleOwner.current
return remember(this, lifecycleOwner) {
flowWithLifecycle(lifecycleOwner.lifecycle, minActiveLifecycleState)
}
}
collectAsState
@SuppressLint("FlowOperatorInvokedInComposition")
@Composable
fun <T> Flow<T>.lifecycleCollectAsState(
initial: T,
minActiveLifecycleState: Lifecycle.State = Lifecycle.State.STARTED,
): State<T> {
return lifecycleAware(minActiveLifecycleState).collectAsState(initial)
}
FlowOperatorInvokedInComposition
. I’m not sure if this poses a problem or not.value
as the initial value
@SuppressLint("StateFlowValueCalledInComposition")
@Composable
fun <T> StateFlow<T>.lifecycleCollectAsState(
minActiveLifecycleState: Lifecycle.State = Lifecycle.State.STARTED,
): State<T> {
return lifecycleCollectAsState(value, minActiveLifecycleState)
}
StateFlowValueCalledInComposition
hereAdam Powell
11/18/2021, 11:10 PMminActiveLifecycleState
as a remember
key; if that changes, a previously remembered flow won't.Louis Pullen-Freilich [G]
11/18/2021, 11:22 PMcollectAsState
in nature this is a false positive / safe to ignore, I don’t think there’s a way we can avoid warning here short of removing the lint check - and I think basically every other case that it catches is worth catchingcollectAsState
tooAdam Powell
11/18/2021, 11:49 PM@Composable
Louis Pullen-Freilich [G]
11/19/2021, 12:06 AMChris Fillmore
11/22/2021, 5:15 PMminActiveLifecycleState
as a key