2.3 KiB
2.3 KiB
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.
!!! info "Relevant methods"
- `ParentNode.onChildFinished(child: Node)` can be overridden by client code to handle a child finishing
- `Node.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
O3calls itsfinish()methodOnboardingnoticesO3finished; if it had more children, it could switch to another; now it callsfinish()tooLogged innoticesOnboardingfinished, and switches its navigation toMainMainis initialised, and loads its default navigation target (based on product requirements) to beMessagesMessagesis initialised, and loads its default navigation target to beList
!!! success "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
Rooteither implements alogoutcallback, 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 dependencyProfileinvokes the callback or alogoutmethod on the repositoryRootnotices the state change, and switches its navigation to theLogged outscopeLogged outloads its initial navigation target,Login
!!! success "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