Skip to content

iletai/BaseNavigationStack

Repository files navigation

BaseNavigationStack

Build a navigation controller with Navigation Stack SwiftUI

Swift Version Platform

BaseNavigationStack is a Swift package that provides a flexible and reusable base class for managing navigation stacks in SwiftUI applications.

Overview

BaseNavigationStack is designed to simplify the management of navigation stacks in SwiftUI applications. It introduces a generic approach to handle different types for views and presentation targets. This allows you to build navigation flows with ease, supporting various types of views and presentation styles.

Example:

Simulator.Screen.Recording.-.iPhone.15.Pro.-.2023-12-14.at.17.52.24.mp4

Installation

Swift Package Manager

You can use the Swift Package Manager to install BaseNavigationStack by adding it to your Package.swift file:

dependencies: [
    .package(url: "https://github.com/iletai/BaseNavigationStack.git", from: "1.0.0"),
],
targets: [
    .target(name: "YourTarget", dependencies: ["BaseNavigationStack"]),
]

Usage

  1. Import the BaseNavigationStack module into your Swift file:
import BaseNavigationStack
  1. Build In View:
struct ContentView: View {
    @State 
    var navigationRouter = BaseNavigationStack<
        ViewNavigationTarget,
        SheetViewPresentTarget
    >(
        isPresented: .constant(.splash)
    )
    
    var body: some View {
        NavigationStack(path: navigationRouter.navigationPath) {
            BaseView()
                .withNavigationRouter()
                .withSheetRouter(sheetDestination: navigationRouter.presentingSheet)
        }
        .environment(navigationRouter)
    }
}
  1. Quick To Push/Present View
struct ListView: View {
    @Environment(BaseNavigationStack<ViewNavigationTarget, SheetViewPresentTarget>.self)
    var navigationRouter
    
    var body: some View {
        VStack {
            Text("List View")
                .font(.title)
                .fontWeight(.bold)
            HStack {
                Button {
                    navigationRouter.pushToView(.splash)
                } label: {
                    Text("Splash")
                }
                Button {
                    navigationRouter.popBack()
                } label: {
                    Text("Pop Back")
                }
                Button {
                    navigationRouter.navigateToRoot()
                } label: {
                    Text("Pop To Root")
                }
            }
            .buttonStyle(.bordered)
            Spacer()
        }
    }
}