Chris Fillmore
11/18/2021, 10:29 PMflowWithLifecycle
, and I’m wondering if it’s wise (some code in thread)Chris Fillmore
11/18/2021, 10:29 PM@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)
}
}
Chris Fillmore
11/18/2021, 10:30 PMcollectAsState
@SuppressLint("FlowOperatorInvokedInComposition")
@Composable
fun <T> Flow<T>.lifecycleCollectAsState(
initial: T,
minActiveLifecycleState: Lifecycle.State = Lifecycle.State.STARTED,
): State<T> {
return lifecycleAware(minActiveLifecycleState).collectAsState(initial)
}
Chris Fillmore
11/18/2021, 10:31 PMFlowOperatorInvokedInComposition
. I’m not sure if this poses a problem or not.Chris Fillmore
11/18/2021, 10:31 PMvalue
as the initial value
@SuppressLint("StateFlowValueCalledInComposition")
@Composable
fun <T> StateFlow<T>.lifecycleCollectAsState(
minActiveLifecycleState: Lifecycle.State = Lifecycle.State.STARTED,
): State<T> {
return lifecycleCollectAsState(value, minActiveLifecycleState)
}
Chris Fillmore
11/18/2021, 10:32 PMStateFlowValueCalledInComposition
hereChris Fillmore
11/18/2021, 10:35 PMChris Fillmore
11/18/2021, 10:36 PMAdam Powell
11/18/2021, 11:10 PMAdam Powell
11/18/2021, 11:11 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 catchingLouis Pullen-Freilich [G]
11/18/2021, 11:24 PMcollectAsState
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