Skip to content

Instantly share code, notes, and snippets.

@Aidanvii7
Created August 19, 2021 14:47
Show Gist options
  • Save Aidanvii7/0fd64644751848d201f35f68d5aa6862 to your computer and use it in GitHub Desktop.
Save Aidanvii7/0fd64644751848d201f35f68d5aa6862 to your computer and use it in GitHub Desktop.
Lifecycle bound collectAsState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.Lifecycle.State.CREATED
import androidx.lifecycle.Lifecycle.State.STARTED
import androidx.lifecycle.flowWithLifecycle
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.StateFlow
@Composable
fun <R> StateFlow<R>.collectAsStateWhileCreated(): State<R> =
collectAsStateWhile(minActiveState = CREATED)
@Composable
fun <R> StateFlow<R>.collectAsStateWhileStarted(): State<R> =
collectAsStateWhile(minActiveState = STARTED)
@Composable
fun <R> StateFlow<R>.collectAsStateWhile(
minActiveState: Lifecycle.State,
): State<R> = collectAsStateWhile(
minActiveState = minActiveState,
default = remember { value },
)
@Composable
fun <R> Flow<R>.collectAsStateWhileCreated(
default: R,
): State<R> = collectAsStateWhile(
minActiveState = CREATED,
default = default,
)
@Composable
fun <R> Flow<R>.collectAsStateWhileStarted(
default: R,
): State<R> = collectAsStateWhile(
minActiveState = STARTED,
default = default,
)
@Composable
fun <R> Flow<R>.collectAsStateWhile(
minActiveState: Lifecycle.State,
default: R,
): State<R> {
@Suppress("NON_EXHAUSTIVE_WHEN")
when (minActiveState) {
Lifecycle.State.INITIALIZED,
Lifecycle.State.DESTROYED -> {
throw IllegalArgumentException("Only supports lifecycle states CREATED, STARTED and RESUMED")
}
}
val lifecycleOwner = LocalLifecycleOwner.current
return remember(default, lifecycleOwner) {
flowWithLifecycle(
lifecycle = lifecycleOwner.lifecycle,
minActiveState = minActiveState,
)
}.collectAsState(initial = default)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment