StateFlow.kt
package com.louisfn.somovie.ui.common.extension
import android.annotation.SuppressLint
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.flowWithLifecycle
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.StateFlow
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
@SuppressLint("StateFlowValueCalledInComposition")
@Composable
fun <T : R, R> StateFlow<T>.collectAsStateLifecycleAware(
context: CoroutineContext = EmptyCoroutineContext,
minActiveState: Lifecycle.State = Lifecycle.State.STARTED,
): State<R> =
rememberFlowWithLifecycle(flow = this, minActiveState = minActiveState)
.collectAsState(context = context, initial = value)
@Composable
private fun <T> rememberFlowWithLifecycle(
flow: Flow<T>,
lifecycle: Lifecycle = LocalLifecycleOwner.current.lifecycle,
minActiveState: Lifecycle.State = Lifecycle.State.STARTED,
): Flow<T> = remember(flow, lifecycle) {
flow.flowWithLifecycle(
lifecycle = lifecycle,
minActiveState = minActiveState,
)
}