Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
362 changes: 301 additions & 61 deletions README.md

Large diffs are not rendered by default.

149 changes: 149 additions & 0 deletions docs/wiki/API-BaseNavigationContainer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
# API — BaseNavigationContainer

Declarative container that wires a `NavigationRouter` into a SwiftUI `NavigationStack`, destination mapping, sheet and full screen cover.

## Signature

```swift
public struct BaseNavigationContainer<
Route: Hashable & Sendable,
Sheet: Identifiable & Hashable & Sendable,
Cover: Identifiable & Hashable & Sendable,
Content: View,
Destination: View,
SheetContent: View,
CoverContent: View
>: View
```

Generic parameters:

| Parameter | Role |
|---|---|
| `Route` | Destination type pushed onto `NavigationStack` |
| `Sheet` | Identifiable model driving `.sheet(item:)` |
| `Cover` | Identifiable model driving `.fullScreenCover(item:)` |
| `Content` | Root view inside the stack |
| `Destination` | View built from a `Route` |
| `SheetContent` | View built from a `Sheet` |
| `CoverContent` | View built from a `Cover` |

SwiftUI infers the five view generics from the closures — you only name the three model types on the router.

## Full initializer

```swift
public init(
router: NavigationRouter<Route, Sheet, Cover>,
@ViewBuilder content: @escaping () -> Content,
@ViewBuilder destination: @escaping (Route) -> Destination,
@ViewBuilder sheet: @escaping (Sheet) -> SheetContent,
@ViewBuilder cover: @escaping (Cover) -> CoverContent
)
```

```swift
@State private var router = AppRouter()

BaseNavigationContainer(
router: router,
content: { HomeView() },
destination: { route in DestinationView(route: route) },
sheet: { sheet in SheetView(sheet: sheet) },
cover: { cover in CoverView(cover: cover) }
)
```

## Default initializer (no modals)

When `SheetContent == EmptyView` and `CoverContent == EmptyView`, this convenience initializer exists:

```swift
public init(
router: NavigationRouter<Route, Sheet, Cover>,
@ViewBuilder content: @escaping () -> Content,
@ViewBuilder destination: @escaping (Route) -> Destination
)
```

```swift
BaseNavigationContainer(
router: router,
content: { HomeView() },
destination: { route in DestinationView(route: route) }
)
```

You still declare `Sheet`/`Cover` types on the router (partial application with `EmptyView` is not possible), but they are never presented. To avoid declaring dummy types, see the `navigationRouter(_:destination:)` modifier in [[API-Modifiers-and-Helpers]] — it also requires the three types, so the practical minimal setup is small empty `Identifiable` structs:

```swift
struct NoSheet: Identifiable, Hashable, Sendable { let id = UUID() }
struct NoCover: Identifiable, Hashable, Sendable { let id = UUID() }

typealias AppRouter = NavigationRouter<AppRoute, NoSheet, NoCover>
```

## Conditional compilation

`fullScreenCover` does not exist on macOS. The container branches at compile time:

```swift
#if os(iOS) || os(tvOS) || os(watchOS) || os(visionOS)
NavigationStack(path: $router.path) {
content()
.navigationDestination(for: Route.self) { destination($0) }
.sheet(item: $router.sheet) { sheetContent($0) }
.fullScreenCover(item: $router.cover) { coverContent($0) }
}
.environment(router)
#else
NavigationStack(path: $router.path) {
content()
.navigationDestination(for: Route.self) { destination($0) }
.sheet(item: $router.sheet) { sheetContent($0) }
}
.environment(router)
#endif
```

On macOS:

- `cover` state is still tracked by the router but never rendered. Guard UI that assumes a cover is visible.
- `.sheet(item:)` exists on macOS and works normally.
- Your `cover:` closure still compiles; keep it platform-neutral.

## Behavior notes

- Router is stored as `@Bindable`, so `$router.path`, `$router.sheet`, `$router.cover` drive SwiftUI directly.
- `.environment(router)` is applied to the stack — every pushed destination, sheet, and cover can read it with `@Environment(AppRouter.self)`.
- Modals are attached **inside** the `NavigationStack`; a sheet presented from a pushed screen still closes when the router's `sheet` becomes `nil`.
- The container does not own the router. Create it with `@State` in the parent so it survives view re-creation.

## Complete example

```swift
struct RootView: View {
@State private var router = AppRouter()

var body: some View {
BaseNavigationContainer(
router: router,
content: { HomeView() },
destination: { route in
switch route {
case .detail(let id): DetailView(id: id)
case .profile: ProfileView()
}
},
sheet: { sheet in
SheetView(sheet: sheet)
.presentationDetents([.medium, .large])
},
cover: { cover in
CoverView(cover: cover)
.ignoresSafeArea()
}
)
}
}
```
172 changes: 172 additions & 0 deletions docs/wiki/API-Modifiers-and-Helpers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
# API — Modifiers & Helpers

## `.navigationRouter(...)` View modifier

Binds a `NavigationRouter` to an existing `NavigationStack` (or any view hierarchy) instead of using `BaseNavigationContainer`. Use it when you already build the `NavigationStack` yourself.

### Full form

```swift
public extension View {
@MainActor @ViewBuilder
func navigationRouter<
Route: Hashable & Sendable,
Sheet: Identifiable & Hashable & Sendable,
Cover: Identifiable & Hashable & Sendable,
Destination: View,
SheetContent: View,
CoverContent: View
>(
_ router: NavigationRouter<Route, Sheet, Cover>,
@ViewBuilder destination: @escaping (Route) -> Destination,
@ViewBuilder sheet: @escaping (Sheet) -> SheetContent,
@ViewBuilder cover: @escaping (Cover) -> CoverContent
) -> some View
}
```

Attaches, in order: `.navigationDestination(for:)`, `.sheet(item:)`, `.fullScreenCover(item:)` (iOS/tvOS/watchOS/visionOS only), and `.environment(router)`.

```swift
struct RootView: View {
@State private var router = AppRouter()

var body: some View {
NavigationStack(path: $router.path) {
HomeView()
}
.navigationRouter(
router,
destination: { route in
switch route {
case .detail(let id): DetailView(id: id)
case .profile: ProfileView()
}
},
sheet: { sheet in
SheetView(sheet: sheet)
},
cover: { cover in
CoverView(cover: cover)
}
)
}
}
```

### Stack-only overload

```swift
func navigationRouter<Route, Sheet, Cover, Destination: View>(
_ router: NavigationRouter<Route, Sheet, Cover>,
@ViewBuilder destination: @escaping (Route) -> Destination
) -> some View
```

Passes `EmptyView` for sheet and cover internally. On macOS the `fullScreenCover` branch is compiled out (same `#if` as the container).

## Collection helpers

`Sources/BaseNavigationStack/Utils/Collection+Extension.swift`:

```swift
extension Collection {
/// Returns nil instead of trapping when out of bounds.
public subscript(safe index: Index) -> Element? {
indices.contains(index) ? self[index] : nil
}
}

extension Array {
/// removeLast() that is a no-op on empty arrays.
public mutating func removeLastSafe() {
guard !isEmpty else { return }
removeLast()
}
}
```

```swift
let stack = ["a", "b"]
let first = stack[safe: 5] // nil, no crash
let mid = stack[safe: 0] // Optional("a")

var path = [Route]()
path.removeLastSafe() // no crash on empty path
```

These are public and used by the v1 `BaseNavigationStack` compatibility layer.

## `BaseViewProtocol`

v1 contract: a screen model that declares its own destinations.

```swift
public protocol BaseViewProtocol: Identifiable, Hashable, Sendable {
associatedtype PresentSheetScreenView: View
associatedtype DestinationView: View
associatedtype PresentFullScreenView: View

@MainActor @ViewBuilder func withNavigationDestination() -> DestinationView
@MainActor @ViewBuilder func withSheetDestination() -> PresentSheetScreenView
@MainActor @ViewBuilder func withFullScreenDestination() -> PresentFullScreenView
}
```

Example (from the demo app):

```swift
enum ViewNavigationTarget: BaseViewProtocol {
case splash
case list
case policy(URL)

var id: String { /* stable identity */ }

@MainActor func withNavigationDestination() -> some View {
switch self {
case .policy: PolicyView()
case .list: ListView()
case .splash: SplashView()
}
}

@MainActor func withSheetDestination() -> some View { /* ... */ }
@MainActor func withFullScreenDestination() -> some View { /* ... */ }
}
```

New code should prefer v2 routes (`Hashable & Sendable` enum + `destination:` closures) — views are mapped once in the container instead of on every model case. See [[Migration-Guide-v1-to-v2]].

## Legacy v1: `BaseNavigationStack<ScreenView>`

```swift
@MainActor @Observable
public final class BaseNavigationStack<ScreenView>: @unchecked Sendable
where ScreenView: BaseViewProtocol
```

Kept for backward compatibility. Public surface:

| Member | Behavior |
|---|---|
| `init(isPresented: Binding<ScreenView?> = .constant(nil))` | Root binding for modal-presentation ownership. |
| `pushToView(_:)` | Append with duplicate-top debounce (no `maxDepth` cap in v1). |
| `popBack()` | `removeLastSafe()` — safe on empty. |
| `navigateToRoot()` | Clear path + sheet + cover. |
| `replaceNavigationStack(path:)` | Clear modals, replace path. |
| `presentSheet(_:)` / `presentFullScreen(_:)` | Set modal state. |
| `dismiss()` | sheet → cover → pop → `isPresented = nil` ladder. |
| `urlHandler` | `(@MainActor (URL) -> OpenURLAction.Result)?` hook; `handleOpenURL(url:)` invokes it, defaults `.systemAction`. |
| `navigationPath` / `presentingSheet` / `presentingFullScreen` / `isPresented` | `Binding` accessors. |

```swift
typealias BaseNavigation = BaseNavigationStack<ViewNavigationTarget>

let nav = BaseNavigation()
nav.pushToView(.list)
nav.popBack()
nav.dismiss()
```

v1 fixes shipped in 2.0: `dismiss()` now pops single-screen stacks; `replaceNavigationStack(path:)` resets active modals.
Loading
Loading