Fix navigation issue #2778

This commit is contained in:
Benoit Marty 2024-05-01 13:50:34 +02:00
parent b05fa82dfc
commit 83506e5191
3 changed files with 26 additions and 15 deletions

View file

@ -20,6 +20,7 @@ import androidx.lifecycle.lifecycleScope
import com.bumble.appyx.core.children.nodeOrNull
import com.bumble.appyx.core.node.Node
import com.bumble.appyx.core.node.ParentNode
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlin.coroutines.resume
@ -48,3 +49,23 @@ suspend inline fun <reified N : Node, NavTarget : Any> ParentNode<NavTarget>.wai
continuation.cancel()
}
}
/**
* Wait for a child to be attached to the parent node, only using the NavTarget
*/
suspend inline fun <NavTarget : Any> ParentNode<NavTarget>.waitForNavTargetAttached(crossinline predicate: (NavTarget) -> Boolean) =
suspendCancellableCoroutine { continuation ->
lifecycleScope.launch {
children.collect { childMap ->
val node = childMap.entries
.map { it.key.navTarget }
.lastOrNull(predicate)
if (node != null && !continuation.isCompleted) {
continuation.resume(Unit)
cancel()
}
}
}.invokeOnCompletion {
continuation.cancel()
}
}