JamesSedlacek / Routing

SwiftUI library for abstracting navigation logic from views

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Decoupling Routing Mechanism

hmlongco opened this issue · comments

From my perspective, one of the primary functions of a router is to decouple the presentation methodology.

IOW, the code says "I want to display a payment screen." That code shouldn't care if the payment screen is added to the stack, or presented in a modal sheet or In a full-screen cover.

Whereas this code is explicitly defining the process from within the view.

Button("Go to Detail View") {
  router.push(.detail)
}

What if you had...

Button("Go to Detail View") {
  router.route(.detail)
}

And then add some additional mechanism for the Route to determine the proper course of action?

(Added this as Issue since Discussions not enabled for the project.)

You could definitely create a router object that handles navigation the way you're suggesting.
Or my preferred way is by injecting the router object into the viewModel and the viewModel specifies how to handle navigation.
I provided an MVVM example in the README for how to do this.