ChildAware API¶
The ChildAware
interface allows you to scope communication with (or between) dynamically available child nodes easily.
Baseline¶
In the next examples:
- Let's assume that
SomeNode
can host multiple child nodes:Child1
,Child2
, etc. SomeInteractor
belongs toSomeNode
and is passed as a Plugin to itSomeInteractor
extends theInteractor
helper class from the framework:- It implements
NodeLifecycleAware
, which makes sure it will receive theonCreate
callback from the framework - It implements
ChildAware
, which unlocks the DSL we'll see in the following snippets
- It implements
Single child scenario¶
import com.bumble.appyx.navigation.lifecycle.Lifecycle
import com.bumble.appyx.core.children.whenChildAttached
import com.bumble.appyx.core.children.whenChildrenAttached
import com.bumble.appyx.core.clienthelper.interactor.Interactor
class SomeInteractor : Interactor<SomeNode>() {
override fun onCreate(lifecycle: Lifecycle) {
lifecycle.subscribe(onCreate = {
// This lambda is executed every time a node of type Child1Node is attached:
whenChildAttached { commonLifecycle: Lifecycle, child1: Child1Node ->
// TODO:
// - establish communication with child1
// - use commonLifecycle for scoping
// - it will be capped by the lifecycles of child1 and the parent
}
})
}
}
Note: Lifecycle is multiplatform.
Multiple children¶
import androidx.lifecycle.Lifecycle
import com.bumble.appyx.core.children.whenChildAttached
import com.bumble.appyx.core.children.whenChildrenAttached
import com.bumble.appyx.core.clienthelper.interactor.Interactor
class SomeInteractor : Interactor<SomeNode>() {
override fun onCreate(lifecycle: Lifecycle) {
lifecycle.subscribe(onCreate = {
// This lambda is executed every time these two nodes are attached at the same time:
whenChildrenAttached { commonLifecycle: Lifecycle, child1: Child1Node, child2: Child2Node ->
// TODO
// - establish communication between child1 & child2
// - use commonLifecycle for scoping
// - it will be capped by the lifecycles of child1, child2 and the parent
}
})
}
}