-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRouter.swift
68 lines (53 loc) · 1.52 KB
/
Router.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//
// SwiftRouter.swift
// SwiftRouter
//
// Created by Ales Kocur on 21.12.2023.
//
import Foundation
import SwiftUI
class SwiftRouter<Route: Routeable>: ObservableObject {
@Published var path = NavigationPath()
func popToRoot() {
path.removeLast(path.count)
}
func push(_ item: Route) {
path.append(item)
}
func pop() {
if path.count > 0 {
path.removeLast()
}
}
}
protocol Routeable: Hashable {
associatedtype DestinationView: View
associatedtype Route: Hashable
@ViewBuilder static func routes(_ route: Route) -> DestinationView
}
struct RouteableNavigationStack<Content: View, Route: Routeable>: View {
// MARK: Lifecycle
init(_ routerType: Route.Type, @ViewBuilder content: @escaping (SwiftRouter<Route>) -> Content) {
self.content = content
}
init(_ routerType: Route.Type, @ViewBuilder content: @escaping () -> Content) {
self.content = { _ in content() }
}
// MARK: Internal
let content: (SwiftRouter<Route>) -> Content
@StateObject var router = SwiftRouter<Route>()
var body: some View {
NavigationStack(path: $router.path) {
content(router)
.attachPushRoutes(routes: Route.self)
}.environmentObject(router)
}
}
extension View {
@MainActor
func attachPushRoutes<Routes: Routeable>(routes: Routes.Type) -> some View {
navigationDestination(for: Routes.Route.self) { route in
Routes.routes(route)
}
}
}