Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9ebfba9a authored by Chaohui Wang's avatar Chaohui Wang
Browse files

Return Job in collectLatestWithLifecycle

To enable canceling the flow collection.

Bug: 339226604
Test: manual - Choose network
Change-Id: Id10f7abaaf67a67e242a9d1d99b465613d4b7b01
parent a8d4e7e2
Loading
Loading
Loading
Loading
+4 −5
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.combine
@@ -61,10 +62,8 @@ fun <T> Flow<T>.collectLatestWithLifecycle(
    lifecycleOwner: LifecycleOwner,
    minActiveState: Lifecycle.State = Lifecycle.State.STARTED,
    action: suspend (value: T) -> Unit,
) {
    lifecycleOwner.lifecycleScope.launch {
): Job = lifecycleOwner.lifecycleScope.launch {
    lifecycleOwner.repeatOnLifecycle(minActiveState) {
        collectLatest(action)
    }
}
}