Skip to content

sclown/PathNavigation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PathNavigation

Navigation framework for SwiftUI applications. It handles most of navigation operations in one common way similar to NavigationStack. Presentation of sheets, alerts, submodules could be done through the path.

Originally published at xymatic-public

Features

  • Declarative Navigation: Define navigation paths and flows in a clear, declarative way.
  • Decoupled: Separates navigation logic from your views, improving modularity and testability.
  • Child modules: Navigation could show another navigation module with the different route type.

Installation

You can add PathNavigation to your project using Swift Package Manager. In your Package.swift file, add the following dependency:

dependencies: [
    .package(url: "https://github.com/sclown/PathNavigation.git", from: "0.2.0")
]

Usage

Basic Setup

  1. Define your navigation routes:
enum AppRoute: Hashable {
    case root
    case next
    case dismiss
}
  1. Define destinations for routes:
@ViewBuilder
func destination(_ path: AppRoute?) -> some View {
    switch path {
    case .root: HomeView()
    case .next: DetailView()
    default: EmptyView()
    }
}
  1. Define navigation actions:
func route(_ route: AppRoute) {
    switch route {
    case .root: break
    case .next: navigation.present(.next, transition: .sheet)
    case .dismiss: navigation.dismiss()
    }
}
  1. Create a navigation view model:
let navigation = NavigationViewModel(root: AppRoute.root)
  1. Use PathNavigationView:
struct ContentView: View {
    var body: some View {
        PathNavigationView(
            viewModel: navigation,
            destinations: { destination($0) }
        )
    }
}
  1. Apply navigation in views:
Button("Push") { route(.next) }

Check Example project for more details.

Modules

Framework comes with supporting modules for handling navigation cases like alerts or pickers. One shows the screen for some input and need results from it. It very convenient to have it as an async function or Combine's Publisher.

  • PNInputRequestCombine: provides solution with the dependency on Combine
  • PNInputRequest: provides solution without combine, but requires iOS 18

Contributing

Contributions are welcome! Please feel free to open an issue or submit a pull request.

License

This project is licensed under the MIT License.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages