Skip to content

Framework for abstracting navigation logic from views in SwiftUI

Notifications You must be signed in to change notification settings

ferencIOS/Routing

 
 

Repository files navigation

Swift Package Manager GitHub stars GitHub forks GitHub contributors Pull Requests Badge Issues Badge

Description

Routing is a framework for abstracting navigation logic from views in SwiftUI.

  • Simplifies code by removing navigation responsibilities from views.
  • Leads to cleaner, more manageable code.
  • Promotes better separation of concerns.
  • Ridiculously lightweight.
  • Type-safe routing using enums and associated values.
  • Unit Tested protocol implementations.
  • Zero 3rd party dependencies.

Requirements

  • iOS: Requires iOS 17.0 or later.
  • macOS: Requires macOS 14.0 or later.

Installation

You can install Routing using the Swift Package Manager.

  1. In Xcode, select "File" > "Add Package Dependencies".
  2. Copy & paste the following into the "Search or Enter Package URL" search bar.
https://github.com/JamesSedlacek/Routing.git
  1. Xcode will fetch the repository & the "Routing" library will be added to your project.

Getting Started

  1. Create the "Route" enum where views will navigate to.
import SwiftUI
import Routing

enum Route: ViewDisplayable {
    case detail
    case settings
    
    @ViewBuilder
    var viewToDisplay: some View {
        switch self {
        case .detail:
            DetailView()
        case .settings:
            SettingsView()
        }
    }
}
  1. The RoutingView will be used instead of a NavigationStack. Pass in the Route enumeration, so that the RouterView can use them.
import SwiftUI
import Routing

struct ContentView: View {
    var body: some View {
        RoutingView(Route.self) { router in
            // Main Content goes here
        }
    }
}
  1. Handle navigation using the Router functions
// NavigationPath
func pop(_ count: Int)
func popToRoot()
func push(_ destination: Destination)
func push(_ destinations: [Destination])

// Sheet
func presentSheet(_ destination: Destination)
func dismissSheet()

// FullScreenCover
func presentFullScreenCover(_ destination: Destination)
func dismissFullScreenCover()

// Alert
func presentAlert(_ alert: Alert)
func dismissAlert()

// Toast (not implemented yet)
func presentToast(_ toast: Toast)
func dismissToast()

Router In View Example

import SwiftUI
import Routing

enum ExampleRoute: ViewDisplayable {
    case detail
    case settings
    
    @ViewBuilder
    var viewToDisplay: some View {
        switch self {
        case .detail:
            DetailView()
        case .settings:
            SettingsView()
        }
    }
}

struct ExampleView: View {
    var body: some View {
        RoutingView(ExampleRoute.self) { router in
    
            // Example of using `push`
            Button("Go to Detail View") {
                router.push(.detail)
            }
    
            // Example of using `pop`
            Button("Go back") {
                router.pop()
            }
        
            // Example of using `popToRoot`
            Button("Go back to Root") {
                router.popToRoot()
            }
            
            // Example of using `presentSheet`
            Button("Present Settings View") {
                router.presentSheet(.settings)
            }
            
            // Example of using `presentFullScreenCover`
            Button("Present Settings View") {
                router.presentFullScreenCover(.settings)
            }

            // Example of using `presentAlert`
            Button("Show alert") {
                router.presentAlert(.init(title: Text("Testing alerts"),
                                          primaryButton: .default(Text("OK")),
                                          secondaryButton: .cancel(Text("Cancel"))))
            }
        }
    }
}

ViewModel Example

import SwiftUI
import Routing

enum ExampleRoute: ViewDisplayable {
    case detail
    case settings
    
    @ViewBuilder
    var viewToDisplay: some View {
        switch self {
        case .detail:
            DetailView()
        case .settings:
            SettingsView()
        }
    }
}

class ExampleViewModel: ObservableObject {
    private let router: Router<ExampleRoute>

    init(router: Router<ExampleRoute>) {
        self.router = router
    }

    func goToDetailView() {
        router.push(.detail)
    }

    func goBack() {
        router.pop()
    }

    func goBackToRoot() {
        router.popToRoot()
    }
}

struct ExampleView: View {
    @StateObject var viewModel: ExampleViewModel

    var body: some View {
        VStack {
            Button("Go to Detail View") {
                viewModel.goToDetailView()
            }

            Button("Go back") {
                viewModel.goBack()
            }

            Button("Go back to Root") {
                viewModel.goBackToRoot()
            }
        }
    }
}

struct ContentView: View {
    var body: some View {
        RoutingView(ExampleRoute.self) { router in
            ExampleView(viewModel: .init(router: router))
        }
    }
}

Author

James Sedlacek, find me on X/Twitter or LinkedIn

License

Routing is available under the MIT license.

Copyright (c) 2023 James Sedlacek

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

Framework for abstracting navigation logic from views in SwiftUI

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Swift 100.0%