All Downloads are FREE. Search and download functionalities are using the official Maven repository.

androidMain.net.humans.kmm.mvi.FragmentExt.kt Maven / Gradle / Ivy

The newest version!
package net.humans.kmm.mvi

import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch

fun  Fragment.observeOnLifecycle(
    flow: StateFlow,
    lifecycleState: Lifecycle.State = Lifecycle.State.STARTED,
    onChanged: suspend (prev: T?, curr: T) -> Unit
) {
    viewLifecycleOwner.lifecycleScope.launch {
        viewLifecycleOwner.lifecycle.repeatOnLifecycle(lifecycleState) {
            flow.scanCollect(onChanged)
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy