Replies: 1 comment 2 replies
-
Hi @lucamegh, the library actually already comes with the tools to accomplish this, and a bit more simply. Both the There are just a few changes you must make. First you must mark your @CasePathable
enum LoadingState<Success, Failure: Error> {
…
} Also, navigation tools tend to work better with optional state because it gives you a natural way to clear state when a feature is dismissed. So, for that reason I think it's best to drop the @CasePathable
enum LoadingState<Success, Failure: Error> {
case inProgress
case success(Success)
case failure(Failure)
} And hold onto optional state in your model: var state: LoadingState<String, Error>? Then Next you can "install" a view controller into the root that represents the "idle" case: install(
…
) And finally you can use destination(item: $model.state.inProgress) { _ in
LoadingViewController()
} present: { [weak self] controller, _ in
self?.install(controller)
} dismiss: { controller, _ in
controller.uninstall()
} If you do that for each controller it should work identical to what you have now, but without any additional wrapper controllers. It also unlocks new super powers, such as being able to use And if you aren't happy with this specific API design, then that is ok, because you can build your own special API on top of these tools. We are trying to keep the number of tools offered by the library to a bare minimum while still making it possible for people to build their own specialized controllers on top. |
Beta Was this translation helpful? Give feedback.
-
As discussed many times in episodes, navigation can take many shapes and forms. One of my favorite types of navigation takes the form of Dave DeLong's ContainerViewController. The view controller's sole purpose is to display other view controllers, one at a time. Think it as a dumber
UITabBarController
. By assigning a view controller to ContainerViewController.content
, the container view controller simply displays it. This concept is particularly useful in apps that make extensive use of view controller containment.The introduction of UIKitNavigation inspired me to think about how this pattern could be enhanced with observation tools. I created a demo to illustrate what an observation-enabled
ContainerViewController
(provisionally namedConditionalViewController
) might look like.Here's the result:
Here's a basic implementation:
This view controller could be even more powerful with custom transition support. It would also address another significant use case: when you initialize a
NavigationStackController
, the root view controller cannot be changed. By usingConditionalViewController
as the root of the navigation stack controller, you can dynamically "change" the root based on the model's state.I strongly believe that
ConditionalViewController
would be a valuable addition to the library, as it offers a straightforward and elegant solution to address numerous navigation problems effectively.Demo
Beta Was this translation helpful? Give feedback.
All reactions