|
| 1 | +// |
| 2 | +// Navigation.swift |
| 3 | +// AndroidSwiftUICore |
| 4 | +// |
| 5 | +// Swift owns the navigation stack. Links push, `dismiss` pops, and the stack |
| 6 | +// emits every screen (root + pushed) so Compose can animate between them and |
| 7 | +// cache the outgoing screen during a pop. The model persists across |
| 8 | +// re-evaluation via the stack's identity path. |
| 9 | +// |
| 10 | + |
| 11 | +/// The pushed navigation stack, shared with the links of the hosted screens. |
| 12 | +public final class NavigationModel: @unchecked Sendable { |
| 13 | + |
| 14 | + enum Entry { |
| 15 | + case view(() -> any View) |
| 16 | + case value(AnyHashable) |
| 17 | + } |
| 18 | + |
| 19 | + var stack: [Entry] = [] |
| 20 | + var destinations: [ObjectIdentifier: (Any) -> (any View)?] = [:] |
| 21 | + var onChange: (() -> Void)? |
| 22 | + |
| 23 | + public init() {} |
| 24 | + |
| 25 | + func pushView(_ content: @escaping () -> any View) { |
| 26 | + stack.append(.view(content)) |
| 27 | + onChange?() |
| 28 | + } |
| 29 | + |
| 30 | + func pushValue(_ value: AnyHashable) { |
| 31 | + stack.append(.value(value)) |
| 32 | + onChange?() |
| 33 | + } |
| 34 | + |
| 35 | + func pop() { |
| 36 | + guard !stack.isEmpty else { return } |
| 37 | + stack.removeLast() |
| 38 | + onChange?() |
| 39 | + } |
| 40 | + |
| 41 | + func register<V>(_ type: V.Type, _ build: @escaping (V) -> any View) { |
| 42 | + destinations[ObjectIdentifier(type)] = { ($0 as? V).map(build) } |
| 43 | + } |
| 44 | + |
| 45 | + func resolve(_ entry: Entry) -> (any View)? { |
| 46 | + switch entry { |
| 47 | + case .view(let content): |
| 48 | + return content() |
| 49 | + case .value(let value): |
| 50 | + return destinations[ObjectIdentifier(type(of: value.base))]?(value.base) ?? nil |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +/// A stack-based navigation container. |
| 56 | +public struct NavigationStack<Root: View>: View { |
| 57 | + |
| 58 | + internal let root: Root |
| 59 | + |
| 60 | + public init(@ViewBuilder root: () -> Root) { |
| 61 | + self.root = root() |
| 62 | + } |
| 63 | + |
| 64 | + public typealias Body = Never |
| 65 | +} |
| 66 | + |
| 67 | +extension NavigationStack: PrimitiveView { |
| 68 | + |
| 69 | + public func _render(in context: ResolveContext) -> RenderNode { |
| 70 | + let model = context.storage.persistentObject(at: context.path + ".navModel") { |
| 71 | + NavigationModel() |
| 72 | + } |
| 73 | + model.onChange = context.storage.onChange |
| 74 | + |
| 75 | + // resolve each screen with the model in scope; a per-screen title sink |
| 76 | + // captures its navigationTitle |
| 77 | + var screens: [RenderNode] = [] |
| 78 | + var titles: [PropValue] = [] |
| 79 | + |
| 80 | + func resolveScreen(_ view: any View, index: Int, canDismiss: Bool) { |
| 81 | + var childContext = context.descending("screen\(index)") |
| 82 | + childContext.environment.set(model) |
| 83 | + if canDismiss { |
| 84 | + childContext.environment.values.dismiss = DismissAction { model.pop() } |
| 85 | + } |
| 86 | + let sink = TitleSink() |
| 87 | + childContext.titleSink = sink |
| 88 | + screens.append(Evaluator.resolve(view, childContext)) |
| 89 | + titles.append(.string(sink.title ?? "")) |
| 90 | + } |
| 91 | + |
| 92 | + resolveScreen(root, index: 0, canDismiss: false) |
| 93 | + for (offset, entry) in model.stack.enumerated() { |
| 94 | + let view = model.resolve(entry) ?? AnyView(EmptyView()) |
| 95 | + resolveScreen(view, index: offset + 1, canDismiss: true) |
| 96 | + } |
| 97 | + |
| 98 | + let popID = context.callbacks.register(.void { model.pop() }) |
| 99 | + return RenderNode( |
| 100 | + type: "NavStack", |
| 101 | + id: context.path, |
| 102 | + props: ["titles": .array(titles), "onPop": .int(Int(popID))], |
| 103 | + children: screens |
| 104 | + ) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +/// A button that pushes a destination onto the enclosing navigation stack. |
| 109 | +public struct NavigationLink<Label: View>: View { |
| 110 | + |
| 111 | + internal enum Destination { |
| 112 | + case view(() -> any View) |
| 113 | + case value(AnyHashable) |
| 114 | + } |
| 115 | + |
| 116 | + internal let destination: Destination |
| 117 | + internal let label: Label |
| 118 | + |
| 119 | + public init<V: View>(destination: V, @ViewBuilder label: () -> Label) { |
| 120 | + self.destination = .view { destination } |
| 121 | + self.label = label() |
| 122 | + } |
| 123 | + |
| 124 | + public init<P: Hashable>(value: P, @ViewBuilder label: () -> Label) { |
| 125 | + self.destination = .value(AnyHashable(value)) |
| 126 | + self.label = label() |
| 127 | + } |
| 128 | + |
| 129 | + public typealias Body = Never |
| 130 | +} |
| 131 | + |
| 132 | +public extension NavigationLink where Label == Text { |
| 133 | + init<S: StringProtocol, V: View>(_ title: S, destination: V) { |
| 134 | + self.init(destination: destination) { Text(title) } |
| 135 | + } |
| 136 | + init<S: StringProtocol, P: Hashable>(_ title: S, value: P) { |
| 137 | + self.init(value: value) { Text(title) } |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +extension NavigationLink: PrimitiveView { |
| 142 | + |
| 143 | + public func _render(in context: ResolveContext) -> RenderNode { |
| 144 | + let model = context.environment.object(of: NavigationModel.self) |
| 145 | + let destination = self.destination |
| 146 | + let callbackID = context.callbacks.register(.void { |
| 147 | + switch destination { |
| 148 | + case .view(let content): model?.pushView(content) |
| 149 | + case .value(let value): model?.pushValue(value) |
| 150 | + } |
| 151 | + }) |
| 152 | + return RenderNode( |
| 153 | + type: "NavigationLink", |
| 154 | + id: context.path, |
| 155 | + props: ["onTap": .int(Int(callbackID))], |
| 156 | + children: Evaluator.resolveChildren(label, context.descending("label")) |
| 157 | + ) |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +// MARK: - Modifiers |
| 162 | + |
| 163 | +/// Records a screen's navigation title. |
| 164 | +public struct _NavigationTitleView<Content: View>: View { |
| 165 | + internal let title: String |
| 166 | + internal let content: Content |
| 167 | + public typealias Body = Never |
| 168 | +} |
| 169 | + |
| 170 | +extension _NavigationTitleView: _ResolutionEffectView { |
| 171 | + public func _applyEffect(_ context: inout ResolveContext) -> any View { |
| 172 | + context.titleSink?.title = title |
| 173 | + return content |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +public extension View { |
| 178 | + func navigationTitle<S: StringProtocol>(_ title: S) -> _NavigationTitleView<Self> { |
| 179 | + _NavigationTitleView(title: String(title), content: self) |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +/// Registers a value-type destination builder with the enclosing stack. |
| 184 | +public struct _NavigationDestinationView<Content: View, D: Hashable>: View { |
| 185 | + internal let build: (D) -> any View |
| 186 | + internal let content: Content |
| 187 | + public typealias Body = Never |
| 188 | +} |
| 189 | + |
| 190 | +extension _NavigationDestinationView: _ResolutionEffectView { |
| 191 | + public func _applyEffect(_ context: inout ResolveContext) -> any View { |
| 192 | + context.environment.object(of: NavigationModel.self)?.register(D.self, build) |
| 193 | + return content |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | +public extension View { |
| 198 | + func navigationDestination<D: Hashable, C: View>( |
| 199 | + for type: D.Type, |
| 200 | + @ViewBuilder destination: @escaping (D) -> C |
| 201 | + ) -> _NavigationDestinationView<Self, D> { |
| 202 | + _NavigationDestinationView(build: { destination($0) }, content: self) |
| 203 | + } |
| 204 | +} |
0 commit comments