androidMain.net.humans.kmm.mvi.FragmentExt.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mvi-core Show documentation
Show all versions of mvi-core Show documentation
Simple and concise implementation of Redux/MVI approach by Humans.
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)
}
}
}