Implicit navigation¶
How can we go from one part of the tree to another? In almost all cases navigation can be implicit instead of explicit. We don't need to specify the target – navigation will happen as a consequence of individual pieces of the puzzle.
Relevant methods
ParentNode.onChildFinished(child: Node)
can be overridden by client code to handle a child finishingNode.finish()
invokes the above method on its parent
Use-case 1¶
Requirement¶
After onboarding finishes, the user should land in the message list screen.
Solution¶
O3
calls itsfinish()
methodOnboarding
noticesO3
finished; if it had more children, it could switch to another; now it callsfinish()
tooLogged in
noticesOnboarding
finished, and switches its navigation toMain
Main
is initialised, and loads its default navigation target (based on product requirements) to beMessages
Messages
is initialised, and loads its default navigation target to beList
Bonus
Every Node
in the above sequence only needed to care about its own local concern.
Use-case 2¶
Requirement¶
Pressing the logout button on the profile screen should land us back to the login screen.
Solution¶
Root
either implements alogout
callback, or subscribes to the changes of a user repository; in both cases, either the callback or the repository is passed down the tree as a dependencyProfile
invokes the callback or alogout
method on the repositoryRoot
notices the state change, and switches its navigation to theLogged out
scopeLogged out
loads its initial navigation target,Login
Bonus
Note how the entire Logged in
scope is destroyed without any extra effort. The next time a login happens, all state is created anew.
Summary¶
Implicit navigation allows you to implement navigation without introducing unnecessary coupling in the tree, and successfully covers the majority of navigation scenarios.
In case it's not enough to meet your needs, see the next chapter, Explicit navigation