GithubHelp home page GithubHelp logo

omit2c / routing Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jamessedlacek/routing

0.0 0.0 0.0 2.38 MB

SwiftUI library for abstracting navigation logic from views

License: MIT License

Swift 100.00%

routing's Introduction

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

Description

Routing is a lightweight SwiftUI navigation library.

  • Leverages 1st-party APIs NavigationStack & NavigationDestination.
  • Never be confused about NavigationLink or NavigationPath again! (You don't need them)
  • Unit Tested protocol implementations.
  • Zero 3rd party dependencies.

Note - This library is for NavigationStack only.
If you need to abstract sheets, alerts, etc. then use my other library Presenting


Table of Contents

  1. Requirements
  2. Installation
  3. Getting Started
  4. Passing Data Example
  5. Under the hood
  6. Author

Requirements

Platform Minimum Version
iOS 16.0
macOS 13.0
tvOS 16.0
watchOS 9.0

Installation

You can install Routing using the Swift Package Manager.

  1. In Xcode, select File > Add Package Dependencies.


  1. 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 a Route enum that conforms to the Routable protocol.
import Routing
import SwiftUI

enum ExampleRoute: Routable {
    case detail
    case settings
    
    var body: some View {
        switch self {
        case .detail:
            DetailView()
        case .settings:
            SettingsView()
        }
    }
}
  1. Create a Router object and wrap your RootView with a RoutingView.
import SwiftUI
import Routing

struct ContentView: View {
    @StateObject private var router: Router<ExampleRoute> = .init()

    var body: some View {
        RoutingView(router) {
            Button("Go to Settings") {
                router.navigate(to: .settings)
            }
        }
    }
}
  1. Handle navigation using the Router functions
/// Navigate back in the stack by a specified count.
func navigateBack(_ count: Int)

/// Navigate back to a specific destination in the stack.
func navigateBack(to destination: Destination)

/// Navigate to the root of the stack by emptying it.
func navigateToRoot()

/// Navigate to a specific destination by appending it to the stack.
func navigate(to destination: Destination)

/// Navigate to multiple destinations by appending them to the stack.
func navigate(to destinations: [Destination])

/// Replace the current stack with new destinations.
func replace(with destinations: [Destination])
  1. Child Views have access to the Router object through the environment.
import SwiftUI
import Routing

struct SettingsView: View {
    @EnvironmentObject
    private var router: Router<ExampleRoute>
    
    var body: some View {
        Button("Go Back") {
            router.navigateBack()
        }
    }
}

Passing Data Example

import Routing
import SwiftUI

enum ContentRoute: Routable {
    case detail(Color)
    case settings

    var body: some View {
        switch self {
        case .detail(let color):
            ColorDetail(color: color)
        case .settings:
            SettingsView()
        }
    }
}

struct ContentView: View {
    @StateObject private var router: Router<ContentRoute> = .init()
    private let colors: [Color] = [.red, .green, .blue]

    var body: some View {
        RoutingView(router) {
            List(colors, id: \.self) { color in
                color
                    .onTapGesture {
                        router.navigate(to: .detail(color))
                    }
            }
        }
    }
}

struct ColorDetail: View {
    @EnvironmentObject
    private var router: Router<ContentRoute>
    private let color: Color

    init(color: Color) {
        self.color = color
    }

    var body: some View {
        color
            .frame(maxWidth: .infinity, maxHeight: .infinity)
            .toolbar { toolbarContentView }
    }

    private var toolbarContentView: some ToolbarContent {
        ToolbarItem(placement: .topBarTrailing) {
            Button {
                router.navigate(to: .settings)
            } label: {
                Image(systemName: "gearshape")
            }
        }
    }
}

struct SettingsView: View {
    @EnvironmentObject
    private var router: Router<ContentRoute>

    var body: some View {
        Button("Go back to Root") {
            router.navigateToRoot()
        }
    }
}

Under the hood

The RoutingView essentially is just wrapping your view with a NavigationStack & navigationDestination.

NavigationStack(path: $router.stack) {
    rootView(router)
        .navigationDestination(for: Router<Routes>.Destination.self) { route in
            route.body.environmentObject(router)
        }
}

Author

James Sedlacek, find me on X/Twitter or LinkedIn

routing's People

Contributors

jamessedlacek avatar ahmed-yamany avatar achi113s avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.