├── .gitignore ├── .swiftlint.yml ├── LICENSE ├── Package.swift ├── README.md ├── Sources └── NavigationStackBackport │ ├── Backport.swift │ ├── CodableRepresentation.swift │ ├── Destination.swift │ ├── NavigationAuthority.swift │ ├── NavigationLink.swift │ ├── NavigationPath.swift │ ├── NavigationPathBackport.swift │ ├── NavigationPathBox.swift │ ├── NavigationPathItem.swift │ ├── NavigationStack.swift │ ├── NavigationUpdate.swift │ ├── Presentation.swift │ └── UIKitNavigation.swift ├── TestApp ├── .gitignore ├── TestApp.xcodeproj │ ├── project.pbxproj │ ├── project.xcworkspace │ │ ├── contents.xcworkspacedata │ │ └── xcshareddata │ │ │ └── IDEWorkspaceChecks.plist │ └── xcshareddata │ │ └── xcschemes │ │ └── TestApp.xcscheme ├── TestApp │ ├── Assets.xcassets │ │ ├── AccentColor.colorset │ │ │ └── Contents.json │ │ ├── AppIcon.appiconset │ │ │ └── Contents.json │ │ └── Contents.json │ ├── Preview Content │ │ └── Preview Assets.xcassets │ │ │ └── Contents.json │ └── TestAppApp.swift ├── TestAppUITests │ ├── DestinationUITests.swift │ ├── LinkUITests.swift │ ├── PathUITests.swift │ └── UserInteractionUITests.swift └── TestPlan.xctestplan └── Tests └── NavigationStackBackportTests ├── NavigationPathItemTests.swift └── NavigationPathTests.swift /.gitignore: -------------------------------------------------------------------------------- 1 | /.build 2 | /.swiftpm/xcode 3 | -------------------------------------------------------------------------------- /.swiftlint.yml: -------------------------------------------------------------------------------- 1 | excluded: 2 | - MedevioGraphQL/Sources/MedevioGraphQL/Generated 3 | - MedevioGraphQL/Sources/MedevioGraphQLFixtures/Generated 4 | - MedevioGraphQL/.build 5 | only_rules: 6 | - array_init 7 | - capture_variable 8 | - closing_brace 9 | - closure_parameter_position 10 | - closure_spacing 11 | - collection_alignment 12 | - colon 13 | - comma 14 | - comma_inheritance 15 | - comment_spacing 16 | - compiler_protocol_init 17 | - computed_accessors_order 18 | - contains_over_filter_count 19 | - contains_over_filter_is_empty 20 | - contains_over_first_not_nil 21 | - contains_over_range_nil_comparison 22 | - control_statement 23 | - convenience_type 24 | - deployment_target 25 | - discarded_notification_center_observer 26 | - discouraged_assert 27 | - discouraged_direct_init 28 | - discouraged_none_name 29 | - discouraged_object_literal 30 | - duplicate_imports 31 | - duplicated_key_in_dictionary_literal 32 | - empty_collection_literal 33 | - empty_count 34 | - empty_enum_arguments 35 | - empty_parameters 36 | - empty_parentheses_with_trailing_closure 37 | - empty_string 38 | - empty_xctest_method 39 | - expiring_todo 40 | - explicit_init 41 | - explicit_self 42 | - extension_access_modifier 43 | - file_header 44 | - file_name_no_space 45 | - first_where 46 | - flatmap_over_map_reduce 47 | - for_where 48 | - ibinspectable_in_extension 49 | - identical_operands 50 | - implicit_getter 51 | - implicit_return 52 | - indentation_width 53 | - joined_default_parameter 54 | - last_where 55 | - leading_whitespace 56 | - legacy_multiple 57 | - legacy_objc_type 58 | - literal_expression_end_indentation 59 | - local_doc_comment 60 | - lower_acl_than_parent 61 | - modifier_order 62 | - multiline_arguments_brackets 63 | - multiline_function_chains 64 | - multiline_literal_brackets 65 | - multiline_parameters 66 | - multiline_parameters_brackets 67 | - no_space_in_method_call 68 | - object_literal 69 | - operator_usage_whitespace 70 | - operator_whitespace 71 | - optional_enum_case_matching 72 | - overridden_super_call 73 | - override_in_extension 74 | - prefer_self_in_static_references 75 | - prefer_self_type_over_type_of_self 76 | - prefer_zero_over_explicit_init 77 | - prohibited_interface_builder 78 | - prohibited_super_call 79 | - protocol_property_accessors_order 80 | - raw_value_for_camel_cased_codable_enum 81 | - reduce_into 82 | - redundant_nil_coalescing 83 | - redundant_optional_initialization 84 | - redundant_set_access_control 85 | - redundant_string_enum_value 86 | - redundant_type_annotation 87 | - redundant_void_return 88 | - required_enum_case 89 | - return_arrow_whitespace 90 | - return_value_from_void_function 91 | - self_binding 92 | - shorthand_operator 93 | - shorthand_optional_binding 94 | - single_test_class 95 | - sorted_first_last 96 | - sorted_imports 97 | - statement_position 98 | - static_operator 99 | - strong_iboutlet 100 | - switch_case_alignment 101 | - switch_case_on_newline 102 | - syntactic_sugar 103 | - test_case_accessibility 104 | - toggle_bool 105 | - trailing_closure 106 | - trailing_newline 107 | - trailing_semicolon 108 | - trailing_whitespace 109 | - typesafe_array_init 110 | - unavailable_function 111 | - unneeded_break_in_switch 112 | - unneeded_parentheses_in_closure_argument 113 | - unused_capture_list 114 | - unused_declaration 115 | - unused_import 116 | - unused_setter_value 117 | - vertical_whitespace_opening_braces 118 | - void_return 119 | - xct_specific_matcher 120 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Ladislav Marek 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version: 5.6 2 | import PackageDescription 3 | 4 | let package = Package( 5 | name: "navigation-stack-backport", 6 | platforms: [ 7 | .iOS(.v14), 8 | ], 9 | products: [ 10 | .library(name: "NavigationStackBackport", targets: ["NavigationStackBackport"]), 11 | ], 12 | targets: [ 13 | .target(name: "NavigationStackBackport", dependencies: []), 14 | .testTarget(name: "NavigationStackBackportTests", dependencies: ["NavigationStackBackport"]) 15 | ] 16 | ) 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SwiftUI NavigationStack Backport 2 | 3 | `NavigationStack` for iOS 14 and 15 implemented on top of `UINavigationController`. Backport just bridges to existing SwiftUI API on iOS 16 or newer. 4 | 5 | ## Features 6 | 7 | - `NavigationPath` is fully supported including codable representation 8 | - `View.navigationDestination()`, `View.navigationDestination(isPresented:destination:)` and `View.navigationDestination(item:destination:)` 9 | - `NavigationLink` with value 10 | - for now tested only on iOS 11 | 12 | ## Getting Started 13 | 14 | Installation via Swift Package Manager is supported. Use `https://github.com/lm/navigation-stack-backport` as depedency URL. For more information how to add dependency in Xcode see https://developer.apple.com/documentation/xcode/adding-package-dependencies-to-your-app or add dependency in your Package.swift `.package(url: "https://github.com/lm/navigation-stack-backport", from: "1.1.0")` 15 | 16 | ## Usage Example 17 | 18 | Usage is the same as SwiftUI's `NavigationStack` on iOS 16, just prefix `NavigationStack` and other types with `NavigationStackBackport.` or import exact types from `NavigationStackBackport` package. For view modifiers introduced in iOS 16 use `backport.` prefix like `.backport.navigationDestination(for: …)`. 19 | 20 | ``` 21 | import NavigationStackBackport 22 | 23 | struct ContentView: View { 24 | @State private var navigationPath = NavigationStackBackport.NavigationPath() 25 | 26 | var body: some View { 27 | NavigationStackBackport.NavigationStack(path: $navigationPath) { 28 | Button("Push") { 29 | navigationPath.append("Hello World") 30 | } 31 | .backport.navigationDestination(for: String.self) { value in 32 | Image(systemName: "globe") 33 | .navigationTitle(value) // use available SwiftUI's modifiers 34 | } 35 | } 36 | } 37 | } 38 | 39 | ``` 40 | 41 | For more examples see the `TestApp` within this repository. 42 | -------------------------------------------------------------------------------- /Sources/NavigationStackBackport/Backport.swift: -------------------------------------------------------------------------------- 1 | import SwiftUI 2 | 3 | public struct Backport { 4 | let content: Content 5 | } 6 | 7 | public extension View { 8 | var backport: Backport { .init(content: self) } 9 | } 10 | -------------------------------------------------------------------------------- /Sources/NavigationStackBackport/CodableRepresentation.swift: -------------------------------------------------------------------------------- 1 | import SwiftUI 2 | 3 | public extension NavigationPath { 4 | struct CodableRepresentation { 5 | let storage: Any 6 | } 7 | } 8 | 9 | extension NavigationPath.CodableRepresentation: Codable { 10 | public init(from decoder: Decoder) throws { 11 | if #available(iOS 16.0, *) { 12 | storage = try SwiftUI.NavigationPath.CodableRepresentation(from: decoder) 13 | return 14 | } 15 | 16 | var container = try decoder.unkeyedContainer() 17 | var items: [NavigationPathItem] = [] 18 | 19 | while !container.isAtEnd { 20 | let typeName = try container.decode(String.self) 21 | let jsonValue = try container.decode(String.self) 22 | items.insert(NavigationPathItem(typeName: typeName, jsonValue: jsonValue), at: 0) 23 | } 24 | 25 | storage = items 26 | } 27 | 28 | public func encode(to encoder: Encoder) throws { 29 | if #available(iOS 16.0, *) { 30 | try (storage as! SwiftUI.NavigationPath.CodableRepresentation).encode(to: encoder) 31 | return 32 | } 33 | 34 | var container = encoder.unkeyedContainer() 35 | 36 | try (storage as! [NavigationPathItem]).reversed().forEach { item in 37 | try item.encodePair(container: &container) 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Sources/NavigationStackBackport/Destination.swift: -------------------------------------------------------------------------------- 1 | import SwiftUI 2 | 3 | public extension Backport { 4 | @ViewBuilder func navigationDestination(for data: D.Type, @ViewBuilder destination: @escaping (D) -> C) -> some View { 5 | if #available(iOS 16.0, *) { 6 | content.navigationDestination(for: D.self, destination: destination) 7 | } else { 8 | content.modifier(DestinationModifier(destination: destination)) 9 | } 10 | } 11 | } 12 | 13 | private struct DestinationModifier: ViewModifier { 14 | let destination: (D) -> C 15 | @Namespace private var id 16 | @Environment(\.navigationAuthority) private var authority 17 | 18 | func body(content: Content) -> some View { 19 | var updated = false 20 | 21 | content 22 | .transformPreference(DestinationIDsKey.self) { ids in 23 | ids.insert(id) 24 | 25 | guard !updated else { return } 26 | updated = true 27 | authority.update(id: id, destination: Destination(view: destination)) 28 | } 29 | } 30 | } 31 | 32 | struct Destination { 33 | let view: (NavigationPathItem, Int) -> AnyView? 34 | let accepts: (NavigationPathItem) -> Bool 35 | 36 | init(view: @escaping (Data) -> some View) { 37 | self.view = { data, contextId in 38 | guard let data = data.valueAs(Data.self) else { return nil } 39 | return AnyView(view(data).environment(\.navigationContextId, contextId)) 40 | } 41 | accepts = { $0.valueAs(Data.self) != nil } 42 | } 43 | } 44 | 45 | extension EnvironmentValues { 46 | var navigationContextId: Int { 47 | get { self[ContextIdKey.self] } 48 | set { self[ContextIdKey.self] = newValue } 49 | } 50 | } 51 | 52 | private struct ContextIdKey: EnvironmentKey { 53 | static var defaultValue = 0 54 | } 55 | 56 | struct DestinationIDsKey: PreferenceKey { 57 | static var defaultValue: Set = [] 58 | 59 | static func reduce(value: inout Set, nextValue: () -> Set) { 60 | value = value.union(nextValue()) 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /Sources/NavigationStackBackport/NavigationAuthority.swift: -------------------------------------------------------------------------------- 1 | import Combine 2 | import SwiftUI 3 | 4 | class NavigationAuthority: NSObject, ObservableObject { 5 | weak var navigationController: UINavigationController? { 6 | didSet { navigationController?.delegate = self } 7 | } 8 | 9 | var destinationIds: Set = [] { 10 | didSet { 11 | cleanupDestinations(oldValue.subtracting(destinationIds)) 12 | } 13 | } 14 | 15 | var presentationIds: [Namespace.ID] = [] { 16 | didSet { 17 | guard presentationIds.count < oldValue.count else { return } 18 | cleanupPresentations() 19 | } 20 | } 21 | 22 | let pathPopPublisher = PassthroughSubject() 23 | let pathPushPublisher = PassthroughSubject() 24 | let presentationPopPublisher = PassthroughSubject() 25 | var canNavigate: Bool { navigationController != nil } 26 | 27 | private var path = NavigationPathBackport(items: []) 28 | private var destinations: [Namespace.ID: Destination] = [:] 29 | private var presentations: [Namespace.ID: Presentation] = [:] 30 | private var viewControllersCount = 1 31 | } 32 | 33 | extension NavigationAuthority { 34 | func update(id: Namespace.ID, destination: Destination) { 35 | destinations[id] = destination 36 | 37 | guard let viewControllers = navigationController?.viewControllers else { return } 38 | 39 | path.items.enumerated().forEach { index, item in 40 | guard viewControllers.indices.contains(index + 1), let view = destination.view(item, index + 1) else { return } 41 | (viewControllers[index + 1] as? UIHostingController)?.rootView = view 42 | } 43 | } 44 | 45 | func update(id: Namespace.ID, presentation: Presentation) { 46 | let prevPresentation = presentations[id] 47 | presentations[id] = presentation 48 | 49 | guard let navigationController else { return } 50 | 51 | let wasPresented = prevPresentation?.isPresented ?? false 52 | let index = 1 + presentation.contextId + (presentationIds.lastIndex(of: id) ?? 0) 53 | 54 | if presentation.isPresented && wasPresented && navigationController.viewControllers.indices.contains(index) { 55 | (navigationController.viewControllers[index] as? UIHostingController)?.rootView = presentation.view 56 | return 57 | } 58 | 59 | guard presentation.isPresented != wasPresented else { return } 60 | 61 | Task { @MainActor in 62 | var update = NavigationUpdate(navigationController: navigationController) 63 | let count = presentation.isPresented ? (index + 1) : index 64 | 65 | update.viewControllers = Array(update.viewControllers.prefix(count)) 66 | if presentation.isPresented { 67 | update.view(presentation.view, at: index) 68 | } 69 | 70 | update.commit() 71 | viewControllersCount = update.viewControllers.count 72 | 73 | if path.count > presentation.contextId { 74 | path.items = Array(path.items.prefix(presentation.contextId)) 75 | pathPopPublisher.send(presentation.contextId) 76 | } 77 | } 78 | } 79 | 80 | @MainActor func update(path: NavigationPathBackport) { 81 | guard path != self.path else { return } 82 | self.path = path 83 | 84 | guard let navigationController else { return } 85 | 86 | var update = NavigationUpdate(navigationController: navigationController) 87 | update.viewControllers = Array(update.viewControllers.prefix(path.count + 1)) 88 | path.items.enumerated().forEach { index, data in update.view(view(for: data, index: index + 1), at: index + 1) } 89 | 90 | update.commit() 91 | viewControllersCount = update.viewControllers.count 92 | popPresentations() 93 | } 94 | } 95 | 96 | extension NavigationAuthority: UINavigationControllerDelegate { 97 | func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) { 98 | let count = navigationController.viewControllers.count 99 | defer { viewControllersCount = count } 100 | 101 | guard count < viewControllersCount else { return } 102 | let pathCount = count - 1 103 | 104 | if pathCount < path.count { 105 | pathPopPublisher.send(pathCount) 106 | return 107 | } 108 | 109 | popPresentations() 110 | } 111 | } 112 | 113 | private extension NavigationAuthority { 114 | func view(for item: NavigationPathItem, index: Int) -> AnyView { 115 | for destination in destinations.values { 116 | if let view = destination.view(item, index) { 117 | return view 118 | } 119 | } 120 | 121 | return AnyView(Image(systemName: "exclamationmark.triangle.fill")) 122 | } 123 | 124 | func popPresentations() { 125 | guard let id = presentationIds.last(where: { id in presentations[id]?.isPresented ?? false }) else { return } 126 | presentationPopPublisher.send(id) 127 | } 128 | 129 | func cleanupDestinations(_ removedIds: Set) { 130 | let removedDestinations = removedIds.map { destinations[$0] } 131 | destinations = destinations.filter { destinationIds.contains($0.key) } 132 | 133 | guard let viewControllers = navigationController?.viewControllers else { return } 134 | 135 | path.items.enumerated().forEach { index, item in 136 | guard 137 | removedDestinations.contains(where: { $0?.accepts(item) ?? false }), 138 | viewControllers.indices.contains(index + 1) 139 | else { return } 140 | 141 | (viewControllers[index + 1] as? UIHostingController)?.rootView = view(for: item, index: index + 1) 142 | } 143 | } 144 | 145 | func cleanupPresentations() { 146 | presentations = presentations.filter { presentationIds.contains($0.key) } 147 | } 148 | } 149 | 150 | extension EnvironmentValues { 151 | var navigationAuthority: NavigationAuthority { 152 | get { self[NavigationAuthorityKey.self] } 153 | set { self[NavigationAuthorityKey.self] = newValue } 154 | } 155 | } 156 | 157 | private struct NavigationAuthorityKey: EnvironmentKey { 158 | static var defaultValue = NavigationAuthority() 159 | } 160 | -------------------------------------------------------------------------------- /Sources/NavigationStackBackport/NavigationLink.swift: -------------------------------------------------------------------------------- 1 | import SwiftUI 2 | 3 | public struct NavigationLink: View { 4 | public let body: AnyView 5 | 6 | public init(value: P?, @ViewBuilder label: () -> Label) { 7 | if #available(iOS 16.0, *) { 8 | body = AnyView(SwiftUI.NavigationLink(value: value, label: label)) 9 | } else { 10 | body = AnyView(Backport(label: label(), item: value.map { .init(value: $0) })) 11 | } 12 | } 13 | 14 | public init(value: P?, @ViewBuilder label: () -> Label) where P: Codable { 15 | if #available(iOS 16.0, *) { 16 | body = AnyView(SwiftUI.NavigationLink(value: value, label: label)) 17 | } else { 18 | body = AnyView(Backport(label: label(), item: value.map { .init(value: $0) })) 19 | } 20 | } 21 | 22 | public init(_ titleKey: LocalizedStringKey, value: P?) where Label == Text { 23 | if #available(iOS 16.0, *) { 24 | body = AnyView(SwiftUI.NavigationLink(titleKey, value: value)) 25 | } else { 26 | body = AnyView(Backport(label: Text(titleKey), item: value.map { .init(value: $0) })) 27 | } 28 | } 29 | 30 | public init(_ titleKey: LocalizedStringKey, value: P?) where Label == Text, P: Codable { 31 | if #available(iOS 16.0, *) { 32 | body = AnyView(SwiftUI.NavigationLink(titleKey, value: value)) 33 | } else { 34 | body = AnyView(Backport(label: Text(titleKey), item: value.map { .init(value: $0) })) 35 | } 36 | } 37 | 38 | public init(_ title: S, value: P?) where Label == Text, S: StringProtocol { 39 | if #available(iOS 16.0, *) { 40 | body = AnyView(SwiftUI.NavigationLink(title, value: value)) 41 | } else { 42 | body = AnyView(Backport(label: Text(title), item: value.map { .init(value: $0) })) 43 | } 44 | } 45 | 46 | public init(_ title: S, value: P?) where Label == Text, S: StringProtocol, P: Codable { 47 | if #available(iOS 16.0, *) { 48 | body = AnyView(SwiftUI.NavigationLink(title, value: value)) 49 | } else { 50 | body = AnyView(Backport(label: Text(title), item: value.map { .init(value: $0) })) 51 | } 52 | } 53 | } 54 | 55 | private extension NavigationLink { 56 | struct Backport: View { 57 | let label: Label 58 | let item: NavigationPathItem? 59 | @Environment(\.navigationAuthority) private var authority 60 | 61 | var body: some View { 62 | Button { 63 | guard let item else { return } 64 | authority.pathPushPublisher.send(item) 65 | } label: { 66 | label 67 | } 68 | .disabled(item == nil || !authority.canNavigate) 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /Sources/NavigationStackBackport/NavigationPath.swift: -------------------------------------------------------------------------------- 1 | import SwiftUI 2 | 3 | public struct NavigationPath { 4 | public var count: Int { box.count } 5 | public var isEmpty: Bool { box.isEmpty } 6 | public var codable: CodableRepresentation? { box.backportedCodable } 7 | 8 | private var box: any NavigationPathBox 9 | 10 | @available(iOS 16.0, *) 11 | var swiftUIPath: SwiftUI.NavigationPath { 12 | get { box as! SwiftUI.NavigationPath } 13 | set { box = newValue } 14 | } 15 | 16 | var storage: NavigationPathBackport { 17 | get { box as! NavigationPathBackport } 18 | set { box = newValue } 19 | } 20 | 21 | public init() { 22 | if #available(iOS 16.0, *) { 23 | box = SwiftUI.NavigationPath() 24 | } else { 25 | box = NavigationPathBackport(items: []) 26 | } 27 | } 28 | 29 | public init(_ elements: S) where S.Element: Hashable { 30 | if #available(iOS 16.0, *) { 31 | box = SwiftUI.NavigationPath(elements) 32 | } else { 33 | box = NavigationPathBackport(items: elements.map { .init(value: $0) }) 34 | } 35 | } 36 | 37 | public init(_ elements: S) where S.Element: Hashable, S.Element: Codable { 38 | if #available(iOS 16.0, *) { 39 | box = SwiftUI.NavigationPath(elements) 40 | } else { 41 | box = NavigationPathBackport(items: elements.map { .init(value: $0) }) 42 | } 43 | } 44 | 45 | public init(_ codable: CodableRepresentation) { 46 | if #available(iOS 16.0, *) { 47 | box = SwiftUI.NavigationPath(codable.storage as! SwiftUI.NavigationPath.CodableRepresentation) 48 | } else { 49 | box = NavigationPathBackport(items: codable.storage as! [NavigationPathItem]) 50 | } 51 | } 52 | } 53 | 54 | public extension NavigationPath { 55 | mutating func append(_ value: V) { 56 | box.append(value) 57 | } 58 | 59 | mutating func append(_ value: V) where V: Hashable, V: Codable { 60 | box.append(value) 61 | } 62 | 63 | mutating func removeLast(_ k: Int = 1) { 64 | box.removeLast(k) 65 | } 66 | } 67 | 68 | extension NavigationPath: Equatable { 69 | public static func == (lhs: Self, rhs: Self) -> Bool { 70 | if #available(iOS 16.0, *) { 71 | return lhs.box as? SwiftUI.NavigationPath == rhs.box as? SwiftUI.NavigationPath 72 | } else { 73 | return lhs.box as? NavigationPathBackport == rhs.box as? NavigationPathBackport 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /Sources/NavigationStackBackport/NavigationPathBackport.swift: -------------------------------------------------------------------------------- 1 | struct NavigationPathBackport { 2 | var items: [NavigationPathItem] 3 | } 4 | 5 | extension NavigationPathBackport: Equatable {} 6 | 7 | extension NavigationPathBackport: NavigationPathBox { 8 | var count: Int { items.count } 9 | var isEmpty: Bool { items.isEmpty } 10 | 11 | var backportedCodable: NavigationPath.CodableRepresentation? { 12 | guard items.allSatisfy(\.isCodable) else { return nil } 13 | return .init(storage: items) 14 | } 15 | 16 | mutating func append(_ value: V) { 17 | items.append(NavigationPathItem(value: value)) 18 | } 19 | 20 | mutating func append(_ value: V) where V: Hashable, V: Codable { 21 | items.append(NavigationPathItem(value: value)) 22 | } 23 | 24 | mutating func removeLast(_ k: Int) { 25 | items.removeLast(k) 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Sources/NavigationStackBackport/NavigationPathBox.swift: -------------------------------------------------------------------------------- 1 | import SwiftUI 2 | 3 | protocol NavigationPathBox { 4 | var count: Int { get } 5 | var isEmpty: Bool { get } 6 | var backportedCodable: NavigationPath.CodableRepresentation? { get } 7 | 8 | mutating func append(_ value: V) 9 | mutating func append(_ value: V) where V: Hashable, V: Codable 10 | mutating func removeLast(_ k: Int) 11 | } 12 | 13 | @available(iOS 16.0, *) 14 | extension SwiftUI.NavigationPath: NavigationPathBox { 15 | var backportedCodable: NavigationPath.CodableRepresentation? { 16 | codable.map(NavigationPath.CodableRepresentation.init(storage:)) 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Sources/NavigationStackBackport/NavigationPathItem.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | 3 | struct NavigationPathItem { 4 | var isCodable: Bool { box.isCodable } 5 | private let box: any Box 6 | 7 | init(value: V) { 8 | box = EagerBox(value: value) 9 | } 10 | 11 | init(value: V) where V: Codable { 12 | box = EagerBox(value: value) 13 | } 14 | 15 | init(typeName: String, jsonValue: String) { 16 | box = LazyBox(typeName: typeName, jsonValue: jsonValue) 17 | } 18 | 19 | func valueAs(_ type: T.Type) -> T? { 20 | box.valueAs(T.self) 21 | } 22 | 23 | func encodePair(container: inout UnkeyedEncodingContainer) throws { 24 | try box.encodePair(container: &container) 25 | } 26 | } 27 | 28 | extension NavigationPathItem: Equatable { 29 | static func == (lhs: Self, rhs: Self) -> Bool { 30 | lhs.box.equalsTo(rhs.box) 31 | } 32 | } 33 | 34 | private protocol Box { 35 | var isCodable: Bool { get } 36 | func valueAs(_ type: T.Type) -> T? 37 | func encodePair(container: inout UnkeyedEncodingContainer) throws 38 | func equalsTo(_: any Box) -> Bool 39 | } 40 | 41 | private struct EagerBox: Box { 42 | let value: Value 43 | let encode: ((JSONEncoder) throws -> Data)? 44 | var isCodable: Bool { encode != nil } 45 | 46 | init(value: Value) { 47 | self.value = value 48 | encode = nil 49 | } 50 | 51 | init(value: Value) where Value: Codable { 52 | self.value = value 53 | encode = { encoder in try encoder.encode(value) } 54 | } 55 | 56 | func valueAs(_ type: T.Type) -> T? { 57 | value as? T 58 | } 59 | 60 | func encodePair(container: inout UnkeyedEncodingContainer) throws { 61 | let jsonValue = String(data: try encode!(JSONEncoder()), encoding: .utf8) 62 | try container.encode(_typeName(type(of: value))) 63 | try container.encode(jsonValue) 64 | } 65 | 66 | func equalsTo(_ other: any Box) -> Bool { 67 | value == other.valueAs(Value.self) 68 | } 69 | } 70 | 71 | private class LazyBox: Box { 72 | var isCodable: Bool { true } 73 | private let typeName: String 74 | private let jsonValue: String 75 | private var decodedValue: Any? 76 | 77 | init(typeName: String, jsonValue: String) { 78 | self.typeName = typeName 79 | self.jsonValue = jsonValue 80 | } 81 | 82 | func valueAs(_ type: T.Type) -> T? { 83 | if let decodedValue { 84 | return decodedValue as? T 85 | } 86 | 87 | guard let decodableType = T.self as? any Decodable.Type else { return nil } 88 | decodedValue = try? JSONDecoder().decode(decodableType, from: jsonValue.data(using: .utf8)!) 89 | return decodedValue as? T 90 | } 91 | 92 | func encodePair(container: inout UnkeyedEncodingContainer) throws { 93 | try container.encode(typeName) 94 | try container.encode(jsonValue) 95 | } 96 | 97 | func equalsTo(_ other: any Box) -> Bool { 98 | if let other = other as? Self { 99 | return typeName == other.typeName && jsonValue == other.jsonValue 100 | } 101 | return other.equalsTo(self) 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /Sources/NavigationStackBackport/NavigationStack.swift: -------------------------------------------------------------------------------- 1 | import SwiftUI 2 | 3 | public struct NavigationStack: View { 4 | public let body: AnyView 5 | 6 | public init(@ViewBuilder root: () -> Root) where Data == NavigationPath { 7 | if #available(iOS 16.0, *) { 8 | body = AnyView(SwiftUI.NavigationStack(root: root)) 9 | } else { 10 | body = AnyView(ImplicitStateView(root: root())) 11 | } 12 | } 13 | 14 | public init(path: Binding, @ViewBuilder root: () -> Root) where Data == NavigationPath { 15 | if #available(iOS 16.0, *) { 16 | body = AnyView(SwiftUI.NavigationStack(path: path.swiftUIPath, root: root)) 17 | } else { 18 | body = AnyView(AuthorityView(path: path.storage, root: root())) 19 | } 20 | } 21 | 22 | public init(path: Binding, @ViewBuilder root: () -> Root) where Data: MutableCollection, Data: RandomAccessCollection, Data: RangeReplaceableCollection, Data.Element: Hashable { 23 | if #available(iOS 16.0, *) { 24 | body = AnyView(SwiftUI.NavigationStack(path: path, root: root)) 25 | } else { 26 | // TODO: implement special homogeneous NavigationPathBox? 27 | body = AnyView(AuthorityView(path: Binding { 28 | NavigationPathBackport(items: path.wrappedValue.map { .init(value: $0) }) 29 | } set: { 30 | path.wrappedValue = .init($0.items.compactMap { $0.valueAs(Data.Element.self) }) 31 | }, root: root())) 32 | } 33 | } 34 | } 35 | 36 | private extension NavigationStack { 37 | struct ImplicitStateView: View { 38 | let root: Root 39 | @State private var path = NavigationPathBackport(items: []) 40 | 41 | var body: some View { 42 | AuthorityView(path: $path, root: root) 43 | } 44 | } 45 | 46 | struct AuthorityView: View { 47 | @Binding var path: NavigationPathBackport 48 | let root: Root 49 | 50 | @StateObject private var authority = NavigationAuthority() 51 | 52 | var body: some View { 53 | UIKitNavigation(root: root.environment(\.navigationContextId, 0), path: path) 54 | .ignoresSafeArea() 55 | .environment(\.navigationAuthority, authority) 56 | .onPreferenceChange(DestinationIDsKey.self) { ids in 57 | authority.destinationIds = ids 58 | } 59 | .onPreferenceChange(PresentationIDsKey.self) { ids in 60 | authority.presentationIds = ids 61 | } 62 | .onReceive(authority.pathPopPublisher) { count in 63 | path.removeLast(path.count - count) 64 | } 65 | .onReceive(authority.pathPushPublisher) { item in 66 | path.items.append(item) 67 | } 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /Sources/NavigationStackBackport/NavigationUpdate.swift: -------------------------------------------------------------------------------- 1 | import SwiftUI 2 | 3 | @MainActor struct NavigationUpdate { 4 | var viewControllers: [UIViewController] { 5 | didSet { changed = true } 6 | } 7 | 8 | private let navigationController: UINavigationController 9 | private var addedViewControllers: [UIViewController] = [] 10 | private var changed = false 11 | 12 | init(navigationController: UINavigationController) { 13 | self.navigationController = navigationController 14 | viewControllers = navigationController.viewControllers 15 | } 16 | 17 | mutating func view(_ view: AnyView, at index: Int) { 18 | changed = true 19 | 20 | if navigationController.viewControllers.indices.contains(index) { 21 | (navigationController.viewControllers[index] as? UIHostingController)?.rootView = view 22 | return 23 | } 24 | 25 | let hostingController = UIHostingController(rootView: view) 26 | viewControllers.append(hostingController) 27 | 28 | addedViewControllers.append(hostingController) 29 | navigationController.view.insertSubview(hostingController.view, at: 0) 30 | navigationController.addChild(hostingController) 31 | hostingController.didMove(toParent: navigationController) 32 | } 33 | 34 | func commit() { 35 | guard changed else { return } 36 | 37 | Task { 38 | addedViewControllers.forEach { 39 | $0.willMove(toParent: nil) 40 | $0.view.removeFromSuperview() 41 | $0.removeFromParent() 42 | } 43 | 44 | if viewControllers[0].view.superview == navigationController.view { 45 | viewControllers[0].view.removeFromSuperview() 46 | } 47 | 48 | navigationController.setViewControllers(viewControllers, animated: true) 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /Sources/NavigationStackBackport/Presentation.swift: -------------------------------------------------------------------------------- 1 | import SwiftUI 2 | 3 | public extension Backport { 4 | @ViewBuilder func navigationDestination(isPresented: Binding, @ViewBuilder destination: () -> C) -> some View { 5 | if #available(iOS 16.0, *) { 6 | content.navigationDestination(isPresented: isPresented, destination: destination) 7 | } else { 8 | content.modifier(PresentationModifier(isPresented: isPresented, destination: destination())) 9 | } 10 | } 11 | 12 | @ViewBuilder func navigationDestination( 13 | item: Binding, 14 | @ViewBuilder destination: @escaping (D) -> C 15 | ) -> some View { 16 | if #available(iOS 17.0, *) { 17 | content.navigationDestination(item: item, destination: destination) 18 | } else { 19 | content.modifier(ItemPresentationModifier(item: item, destination: destination)) 20 | } 21 | } 22 | } 23 | 24 | private struct PresentationModifier: ViewModifier { 25 | @Binding var isPresented: Bool 26 | let destination: C 27 | 28 | @Namespace private var id 29 | @Environment(\.navigationContextId) private var contextId 30 | @Environment(\.navigationAuthority) private var authority 31 | 32 | func body(content: Content) -> some View { 33 | var updated = false 34 | 35 | content 36 | .transformPreference(PresentationIDsKey.self) { ids in 37 | ids.append(id) 38 | 39 | guard !updated else { return } 40 | updated = true 41 | authority.update(id: id, presentation: Presentation(contextId: contextId, isPresented: isPresented, view: destination)) 42 | } 43 | .onReceive(authority.presentationPopPublisher) { id in 44 | guard id == self.id else { return } 45 | isPresented = false 46 | } 47 | } 48 | } 49 | 50 | private struct ItemPresentationModifier: ViewModifier { 51 | @Binding var item: D? 52 | let destination: (D) -> C 53 | 54 | func body(content: Content) -> some View { 55 | let isPresented = Binding { 56 | item != nil 57 | } set: { 58 | if !$0 { 59 | item = nil 60 | } 61 | } 62 | 63 | content.backport.navigationDestination(isPresented: isPresented) { 64 | if let item { 65 | destination(item) 66 | } 67 | } 68 | } 69 | } 70 | 71 | struct Presentation { 72 | let contextId: Int 73 | let isPresented: Bool 74 | let view: AnyView 75 | 76 | init(contextId: Int, isPresented: Bool, view: some View) { 77 | self.contextId = contextId 78 | self.isPresented = isPresented 79 | self.view = AnyView(view.environment(\.navigationContextId, contextId)) 80 | } 81 | } 82 | 83 | struct PresentationIDsKey: PreferenceKey { 84 | static var defaultValue: [Namespace.ID] = [] 85 | 86 | static func reduce(value: inout [Namespace.ID], nextValue: () -> [Namespace.ID]) { 87 | value += nextValue() 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /Sources/NavigationStackBackport/UIKitNavigation.swift: -------------------------------------------------------------------------------- 1 | import SwiftUI 2 | 3 | struct UIKitNavigation: UIViewControllerRepresentable { 4 | let root: Root 5 | let path: NavigationPathBackport 6 | @Environment(\.navigationAuthority) private var authority 7 | 8 | func makeUIViewController(context: Context) -> UINavigationController { 9 | let navigationController = UINavigationController() 10 | navigationController.navigationBar.prefersLargeTitles = true 11 | navigationController.navigationBar.barStyle = .default 12 | navigationController.navigationBar.isTranslucent = true 13 | authority.navigationController = navigationController 14 | return navigationController 15 | } 16 | 17 | func updateUIViewController(_ navigationController: UINavigationController, context: Context) { 18 | if !navigationController.viewControllers.isEmpty, let hostingController = navigationController.viewControllers[0] as? UIHostingController { 19 | hostingController.rootView = root 20 | } else { 21 | let rootViewController = UIHostingController(rootView: root) 22 | navigationController.viewControllers = [rootViewController] 23 | prelayout(rootViewController: rootViewController, navigationController: navigationController) 24 | } 25 | 26 | authority.update(path: path) 27 | } 28 | } 29 | 30 | private extension UIKitNavigation { 31 | func prelayout(rootViewController: UIHostingController, navigationController: UINavigationController) { 32 | navigationController.view.insertSubview(rootViewController.view, at: 0) 33 | navigationController.addChild(rootViewController) 34 | rootViewController.didMove(toParent: navigationController) 35 | 36 | navigationController.view.setNeedsLayout() 37 | navigationController.view.layoutIfNeeded() 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /TestApp/.gitignore: -------------------------------------------------------------------------------- 1 | /TestApp.xcodeproj/xcuserdata 2 | /TestApp.xcodeproj/project.xcworkspace/xcuserdata 3 | -------------------------------------------------------------------------------- /TestApp/TestApp.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 56; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 8A1434412917CA8500E8DE1B /* TestAppApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A1434402917CA8500E8DE1B /* TestAppApp.swift */; }; 11 | 8A1434452917CA8600E8DE1B /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 8A1434442917CA8600E8DE1B /* Assets.xcassets */; }; 12 | 8A1434482917CA8600E8DE1B /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 8A1434472917CA8600E8DE1B /* Preview Assets.xcassets */; }; 13 | 8A1434552917CAEB00E8DE1B /* DestinationUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A1434542917CAEB00E8DE1B /* DestinationUITests.swift */; }; 14 | 8A1434602917CBC800E8DE1B /* NavigationStackBackport in Frameworks */ = {isa = PBXBuildFile; productRef = 8A14345F2917CBC800E8DE1B /* NavigationStackBackport */; }; 15 | 8AF517E2293FE59C000AEE35 /* PathUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8AF517E1293FE59C000AEE35 /* PathUITests.swift */; }; 16 | 8AF517E4293FE63D000AEE35 /* LinkUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8AF517E3293FE63D000AEE35 /* LinkUITests.swift */; }; 17 | 8AF517E6293FE68B000AEE35 /* UserInteractionUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8AF517E5293FE68B000AEE35 /* UserInteractionUITests.swift */; }; 18 | /* End PBXBuildFile section */ 19 | 20 | /* Begin PBXContainerItemProxy section */ 21 | 8A1434582917CAEB00E8DE1B /* PBXContainerItemProxy */ = { 22 | isa = PBXContainerItemProxy; 23 | containerPortal = 8A1434352917CA8500E8DE1B /* Project object */; 24 | proxyType = 1; 25 | remoteGlobalIDString = 8A14343C2917CA8500E8DE1B; 26 | remoteInfo = TestApp; 27 | }; 28 | /* End PBXContainerItemProxy section */ 29 | 30 | /* Begin PBXFileReference section */ 31 | 8A14343D2917CA8500E8DE1B /* TestApp.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = TestApp.app; sourceTree = BUILT_PRODUCTS_DIR; }; 32 | 8A1434402917CA8500E8DE1B /* TestAppApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestAppApp.swift; sourceTree = ""; }; 33 | 8A1434442917CA8600E8DE1B /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 34 | 8A1434472917CA8600E8DE1B /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = ""; }; 35 | 8A1434522917CAEB00E8DE1B /* TestAppUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = TestAppUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 36 | 8A1434542917CAEB00E8DE1B /* DestinationUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DestinationUITests.swift; sourceTree = ""; }; 37 | 8A7F2BEE294142E600FB4C56 /* navigation-stack-backport */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = "navigation-stack-backport"; path = ..; sourceTree = ""; }; 38 | 8AF517E0293FD80C000AEE35 /* TestPlan.xctestplan */ = {isa = PBXFileReference; lastKnownFileType = text; path = TestPlan.xctestplan; sourceTree = ""; }; 39 | 8AF517E1293FE59C000AEE35 /* PathUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PathUITests.swift; sourceTree = ""; }; 40 | 8AF517E3293FE63D000AEE35 /* LinkUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LinkUITests.swift; sourceTree = ""; }; 41 | 8AF517E5293FE68B000AEE35 /* UserInteractionUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserInteractionUITests.swift; sourceTree = ""; }; 42 | /* End PBXFileReference section */ 43 | 44 | /* Begin PBXFrameworksBuildPhase section */ 45 | 8A14343A2917CA8500E8DE1B /* Frameworks */ = { 46 | isa = PBXFrameworksBuildPhase; 47 | buildActionMask = 2147483647; 48 | files = ( 49 | 8A1434602917CBC800E8DE1B /* NavigationStackBackport in Frameworks */, 50 | ); 51 | runOnlyForDeploymentPostprocessing = 0; 52 | }; 53 | 8A14344F2917CAEB00E8DE1B /* Frameworks */ = { 54 | isa = PBXFrameworksBuildPhase; 55 | buildActionMask = 2147483647; 56 | files = ( 57 | ); 58 | runOnlyForDeploymentPostprocessing = 0; 59 | }; 60 | /* End PBXFrameworksBuildPhase section */ 61 | 62 | /* Begin PBXGroup section */ 63 | 8A1434342917CA8500E8DE1B = { 64 | isa = PBXGroup; 65 | children = ( 66 | 8A7F2BEE294142E600FB4C56 /* navigation-stack-backport */, 67 | 8A14343F2917CA8500E8DE1B /* TestApp */, 68 | 8AF517E0293FD80C000AEE35 /* TestPlan.xctestplan */, 69 | 8A1434532917CAEB00E8DE1B /* TestAppUITests */, 70 | 8A14343E2917CA8500E8DE1B /* Products */, 71 | 8A14345E2917CBC800E8DE1B /* Frameworks */, 72 | ); 73 | sourceTree = ""; 74 | }; 75 | 8A14343E2917CA8500E8DE1B /* Products */ = { 76 | isa = PBXGroup; 77 | children = ( 78 | 8A14343D2917CA8500E8DE1B /* TestApp.app */, 79 | 8A1434522917CAEB00E8DE1B /* TestAppUITests.xctest */, 80 | ); 81 | name = Products; 82 | sourceTree = ""; 83 | }; 84 | 8A14343F2917CA8500E8DE1B /* TestApp */ = { 85 | isa = PBXGroup; 86 | children = ( 87 | 8A1434402917CA8500E8DE1B /* TestAppApp.swift */, 88 | 8A1434442917CA8600E8DE1B /* Assets.xcassets */, 89 | 8A1434462917CA8600E8DE1B /* Preview Content */, 90 | ); 91 | path = TestApp; 92 | sourceTree = ""; 93 | }; 94 | 8A1434462917CA8600E8DE1B /* Preview Content */ = { 95 | isa = PBXGroup; 96 | children = ( 97 | 8A1434472917CA8600E8DE1B /* Preview Assets.xcassets */, 98 | ); 99 | path = "Preview Content"; 100 | sourceTree = ""; 101 | }; 102 | 8A1434532917CAEB00E8DE1B /* TestAppUITests */ = { 103 | isa = PBXGroup; 104 | children = ( 105 | 8A1434542917CAEB00E8DE1B /* DestinationUITests.swift */, 106 | 8AF517E3293FE63D000AEE35 /* LinkUITests.swift */, 107 | 8AF517E1293FE59C000AEE35 /* PathUITests.swift */, 108 | 8AF517E5293FE68B000AEE35 /* UserInteractionUITests.swift */, 109 | ); 110 | path = TestAppUITests; 111 | sourceTree = ""; 112 | }; 113 | 8A14345E2917CBC800E8DE1B /* Frameworks */ = { 114 | isa = PBXGroup; 115 | children = ( 116 | ); 117 | name = Frameworks; 118 | sourceTree = ""; 119 | }; 120 | /* End PBXGroup section */ 121 | 122 | /* Begin PBXNativeTarget section */ 123 | 8A14343C2917CA8500E8DE1B /* TestApp */ = { 124 | isa = PBXNativeTarget; 125 | buildConfigurationList = 8A14344B2917CA8600E8DE1B /* Build configuration list for PBXNativeTarget "TestApp" */; 126 | buildPhases = ( 127 | 8A1434392917CA8500E8DE1B /* Sources */, 128 | 8A14343A2917CA8500E8DE1B /* Frameworks */, 129 | 8A14343B2917CA8500E8DE1B /* Resources */, 130 | ); 131 | buildRules = ( 132 | ); 133 | dependencies = ( 134 | ); 135 | name = TestApp; 136 | packageProductDependencies = ( 137 | 8A14345F2917CBC800E8DE1B /* NavigationStackBackport */, 138 | ); 139 | productName = TestApp; 140 | productReference = 8A14343D2917CA8500E8DE1B /* TestApp.app */; 141 | productType = "com.apple.product-type.application"; 142 | }; 143 | 8A1434512917CAEB00E8DE1B /* TestAppUITests */ = { 144 | isa = PBXNativeTarget; 145 | buildConfigurationList = 8A14345A2917CAEB00E8DE1B /* Build configuration list for PBXNativeTarget "TestAppUITests" */; 146 | buildPhases = ( 147 | 8A14344E2917CAEB00E8DE1B /* Sources */, 148 | 8A14344F2917CAEB00E8DE1B /* Frameworks */, 149 | 8A1434502917CAEB00E8DE1B /* Resources */, 150 | ); 151 | buildRules = ( 152 | ); 153 | dependencies = ( 154 | 8A1434592917CAEB00E8DE1B /* PBXTargetDependency */, 155 | ); 156 | name = TestAppUITests; 157 | productName = TestAppUITests; 158 | productReference = 8A1434522917CAEB00E8DE1B /* TestAppUITests.xctest */; 159 | productType = "com.apple.product-type.bundle.ui-testing"; 160 | }; 161 | /* End PBXNativeTarget section */ 162 | 163 | /* Begin PBXProject section */ 164 | 8A1434352917CA8500E8DE1B /* Project object */ = { 165 | isa = PBXProject; 166 | attributes = { 167 | BuildIndependentTargetsInParallel = 1; 168 | LastSwiftUpdateCheck = 1410; 169 | LastUpgradeCheck = 1410; 170 | TargetAttributes = { 171 | 8A14343C2917CA8500E8DE1B = { 172 | CreatedOnToolsVersion = 14.1; 173 | }; 174 | 8A1434512917CAEB00E8DE1B = { 175 | CreatedOnToolsVersion = 14.1; 176 | TestTargetID = 8A14343C2917CA8500E8DE1B; 177 | }; 178 | }; 179 | }; 180 | buildConfigurationList = 8A1434382917CA8500E8DE1B /* Build configuration list for PBXProject "TestApp" */; 181 | compatibilityVersion = "Xcode 14.0"; 182 | developmentRegion = en; 183 | hasScannedForEncodings = 0; 184 | knownRegions = ( 185 | en, 186 | Base, 187 | ); 188 | mainGroup = 8A1434342917CA8500E8DE1B; 189 | productRefGroup = 8A14343E2917CA8500E8DE1B /* Products */; 190 | projectDirPath = ""; 191 | projectRoot = ""; 192 | targets = ( 193 | 8A14343C2917CA8500E8DE1B /* TestApp */, 194 | 8A1434512917CAEB00E8DE1B /* TestAppUITests */, 195 | ); 196 | }; 197 | /* End PBXProject section */ 198 | 199 | /* Begin PBXResourcesBuildPhase section */ 200 | 8A14343B2917CA8500E8DE1B /* Resources */ = { 201 | isa = PBXResourcesBuildPhase; 202 | buildActionMask = 2147483647; 203 | files = ( 204 | 8A1434482917CA8600E8DE1B /* Preview Assets.xcassets in Resources */, 205 | 8A1434452917CA8600E8DE1B /* Assets.xcassets in Resources */, 206 | ); 207 | runOnlyForDeploymentPostprocessing = 0; 208 | }; 209 | 8A1434502917CAEB00E8DE1B /* Resources */ = { 210 | isa = PBXResourcesBuildPhase; 211 | buildActionMask = 2147483647; 212 | files = ( 213 | ); 214 | runOnlyForDeploymentPostprocessing = 0; 215 | }; 216 | /* End PBXResourcesBuildPhase section */ 217 | 218 | /* Begin PBXSourcesBuildPhase section */ 219 | 8A1434392917CA8500E8DE1B /* Sources */ = { 220 | isa = PBXSourcesBuildPhase; 221 | buildActionMask = 2147483647; 222 | files = ( 223 | 8A1434412917CA8500E8DE1B /* TestAppApp.swift in Sources */, 224 | ); 225 | runOnlyForDeploymentPostprocessing = 0; 226 | }; 227 | 8A14344E2917CAEB00E8DE1B /* Sources */ = { 228 | isa = PBXSourcesBuildPhase; 229 | buildActionMask = 2147483647; 230 | files = ( 231 | 8AF517E2293FE59C000AEE35 /* PathUITests.swift in Sources */, 232 | 8A1434552917CAEB00E8DE1B /* DestinationUITests.swift in Sources */, 233 | 8AF517E4293FE63D000AEE35 /* LinkUITests.swift in Sources */, 234 | 8AF517E6293FE68B000AEE35 /* UserInteractionUITests.swift in Sources */, 235 | ); 236 | runOnlyForDeploymentPostprocessing = 0; 237 | }; 238 | /* End PBXSourcesBuildPhase section */ 239 | 240 | /* Begin PBXTargetDependency section */ 241 | 8A1434592917CAEB00E8DE1B /* PBXTargetDependency */ = { 242 | isa = PBXTargetDependency; 243 | target = 8A14343C2917CA8500E8DE1B /* TestApp */; 244 | targetProxy = 8A1434582917CAEB00E8DE1B /* PBXContainerItemProxy */; 245 | }; 246 | /* End PBXTargetDependency section */ 247 | 248 | /* Begin XCBuildConfiguration section */ 249 | 8A1434492917CA8600E8DE1B /* Debug */ = { 250 | isa = XCBuildConfiguration; 251 | buildSettings = { 252 | ALWAYS_SEARCH_USER_PATHS = NO; 253 | CLANG_ANALYZER_NONNULL = YES; 254 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 255 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++20"; 256 | CLANG_ENABLE_MODULES = YES; 257 | CLANG_ENABLE_OBJC_ARC = YES; 258 | CLANG_ENABLE_OBJC_WEAK = YES; 259 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 260 | CLANG_WARN_BOOL_CONVERSION = YES; 261 | CLANG_WARN_COMMA = YES; 262 | CLANG_WARN_CONSTANT_CONVERSION = YES; 263 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 264 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 265 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 266 | CLANG_WARN_EMPTY_BODY = YES; 267 | CLANG_WARN_ENUM_CONVERSION = YES; 268 | CLANG_WARN_INFINITE_RECURSION = YES; 269 | CLANG_WARN_INT_CONVERSION = YES; 270 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 271 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 272 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 273 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 274 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 275 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 276 | CLANG_WARN_STRICT_PROTOTYPES = YES; 277 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 278 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 279 | CLANG_WARN_UNREACHABLE_CODE = YES; 280 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 281 | COPY_PHASE_STRIP = NO; 282 | DEBUG_INFORMATION_FORMAT = dwarf; 283 | ENABLE_STRICT_OBJC_MSGSEND = YES; 284 | ENABLE_TESTABILITY = YES; 285 | GCC_C_LANGUAGE_STANDARD = gnu11; 286 | GCC_DYNAMIC_NO_PIC = NO; 287 | GCC_NO_COMMON_BLOCKS = YES; 288 | GCC_OPTIMIZATION_LEVEL = 0; 289 | GCC_PREPROCESSOR_DEFINITIONS = ( 290 | "DEBUG=1", 291 | "$(inherited)", 292 | ); 293 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 294 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 295 | GCC_WARN_UNDECLARED_SELECTOR = YES; 296 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 297 | GCC_WARN_UNUSED_FUNCTION = YES; 298 | GCC_WARN_UNUSED_VARIABLE = YES; 299 | IPHONEOS_DEPLOYMENT_TARGET = 14.5; 300 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 301 | MTL_FAST_MATH = YES; 302 | ONLY_ACTIVE_ARCH = YES; 303 | SDKROOT = iphoneos; 304 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 305 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 306 | }; 307 | name = Debug; 308 | }; 309 | 8A14344A2917CA8600E8DE1B /* Release */ = { 310 | isa = XCBuildConfiguration; 311 | buildSettings = { 312 | ALWAYS_SEARCH_USER_PATHS = NO; 313 | CLANG_ANALYZER_NONNULL = YES; 314 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 315 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++20"; 316 | CLANG_ENABLE_MODULES = YES; 317 | CLANG_ENABLE_OBJC_ARC = YES; 318 | CLANG_ENABLE_OBJC_WEAK = YES; 319 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 320 | CLANG_WARN_BOOL_CONVERSION = YES; 321 | CLANG_WARN_COMMA = YES; 322 | CLANG_WARN_CONSTANT_CONVERSION = YES; 323 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 324 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 325 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 326 | CLANG_WARN_EMPTY_BODY = YES; 327 | CLANG_WARN_ENUM_CONVERSION = YES; 328 | CLANG_WARN_INFINITE_RECURSION = YES; 329 | CLANG_WARN_INT_CONVERSION = YES; 330 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 331 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 332 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 333 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 334 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 335 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 336 | CLANG_WARN_STRICT_PROTOTYPES = YES; 337 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 338 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 339 | CLANG_WARN_UNREACHABLE_CODE = YES; 340 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 341 | COPY_PHASE_STRIP = NO; 342 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 343 | ENABLE_NS_ASSERTIONS = NO; 344 | ENABLE_STRICT_OBJC_MSGSEND = YES; 345 | GCC_C_LANGUAGE_STANDARD = gnu11; 346 | GCC_NO_COMMON_BLOCKS = YES; 347 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 348 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 349 | GCC_WARN_UNDECLARED_SELECTOR = YES; 350 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 351 | GCC_WARN_UNUSED_FUNCTION = YES; 352 | GCC_WARN_UNUSED_VARIABLE = YES; 353 | IPHONEOS_DEPLOYMENT_TARGET = 14.5; 354 | MTL_ENABLE_DEBUG_INFO = NO; 355 | MTL_FAST_MATH = YES; 356 | SDKROOT = iphoneos; 357 | SWIFT_COMPILATION_MODE = wholemodule; 358 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 359 | VALIDATE_PRODUCT = YES; 360 | }; 361 | name = Release; 362 | }; 363 | 8A14344C2917CA8600E8DE1B /* Debug */ = { 364 | isa = XCBuildConfiguration; 365 | buildSettings = { 366 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 367 | ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; 368 | CODE_SIGN_STYLE = Automatic; 369 | CURRENT_PROJECT_VERSION = 1; 370 | DEVELOPMENT_ASSET_PATHS = "\"TestApp/Preview Content\""; 371 | ENABLE_PREVIEWS = YES; 372 | GENERATE_INFOPLIST_FILE = YES; 373 | INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; 374 | INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; 375 | INFOPLIST_KEY_UILaunchScreen_Generation = YES; 376 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 377 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 378 | IPHONEOS_DEPLOYMENT_TARGET = 14.5; 379 | LD_RUNPATH_SEARCH_PATHS = ( 380 | "$(inherited)", 381 | "@executable_path/Frameworks", 382 | ); 383 | MARKETING_VERSION = 1.0; 384 | PRODUCT_BUNDLE_IDENTIFIER = com.example.TestApp; 385 | PRODUCT_NAME = "$(TARGET_NAME)"; 386 | SWIFT_EMIT_LOC_STRINGS = YES; 387 | SWIFT_VERSION = 5.0; 388 | TARGETED_DEVICE_FAMILY = "1,2"; 389 | }; 390 | name = Debug; 391 | }; 392 | 8A14344D2917CA8600E8DE1B /* Release */ = { 393 | isa = XCBuildConfiguration; 394 | buildSettings = { 395 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 396 | ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; 397 | CODE_SIGN_STYLE = Automatic; 398 | CURRENT_PROJECT_VERSION = 1; 399 | DEVELOPMENT_ASSET_PATHS = "\"TestApp/Preview Content\""; 400 | ENABLE_PREVIEWS = YES; 401 | GENERATE_INFOPLIST_FILE = YES; 402 | INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; 403 | INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; 404 | INFOPLIST_KEY_UILaunchScreen_Generation = YES; 405 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 406 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 407 | IPHONEOS_DEPLOYMENT_TARGET = 14.5; 408 | LD_RUNPATH_SEARCH_PATHS = ( 409 | "$(inherited)", 410 | "@executable_path/Frameworks", 411 | ); 412 | MARKETING_VERSION = 1.0; 413 | PRODUCT_BUNDLE_IDENTIFIER = com.example.TestApp; 414 | PRODUCT_NAME = "$(TARGET_NAME)"; 415 | SWIFT_EMIT_LOC_STRINGS = YES; 416 | SWIFT_VERSION = 5.0; 417 | TARGETED_DEVICE_FAMILY = "1,2"; 418 | }; 419 | name = Release; 420 | }; 421 | 8A14345B2917CAEB00E8DE1B /* Debug */ = { 422 | isa = XCBuildConfiguration; 423 | buildSettings = { 424 | CODE_SIGN_STYLE = Automatic; 425 | CURRENT_PROJECT_VERSION = 1; 426 | GENERATE_INFOPLIST_FILE = YES; 427 | MARKETING_VERSION = 1.0; 428 | PRODUCT_BUNDLE_IDENTIFIER = com.example.TestAppUITests; 429 | PRODUCT_NAME = "$(TARGET_NAME)"; 430 | SWIFT_EMIT_LOC_STRINGS = NO; 431 | SWIFT_VERSION = 5.0; 432 | TARGETED_DEVICE_FAMILY = "1,2"; 433 | TEST_TARGET_NAME = TestApp; 434 | }; 435 | name = Debug; 436 | }; 437 | 8A14345C2917CAEB00E8DE1B /* Release */ = { 438 | isa = XCBuildConfiguration; 439 | buildSettings = { 440 | CODE_SIGN_STYLE = Automatic; 441 | CURRENT_PROJECT_VERSION = 1; 442 | GENERATE_INFOPLIST_FILE = YES; 443 | MARKETING_VERSION = 1.0; 444 | PRODUCT_BUNDLE_IDENTIFIER = com.example.TestAppUITests; 445 | PRODUCT_NAME = "$(TARGET_NAME)"; 446 | SWIFT_EMIT_LOC_STRINGS = NO; 447 | SWIFT_VERSION = 5.0; 448 | TARGETED_DEVICE_FAMILY = "1,2"; 449 | TEST_TARGET_NAME = TestApp; 450 | }; 451 | name = Release; 452 | }; 453 | /* End XCBuildConfiguration section */ 454 | 455 | /* Begin XCConfigurationList section */ 456 | 8A1434382917CA8500E8DE1B /* Build configuration list for PBXProject "TestApp" */ = { 457 | isa = XCConfigurationList; 458 | buildConfigurations = ( 459 | 8A1434492917CA8600E8DE1B /* Debug */, 460 | 8A14344A2917CA8600E8DE1B /* Release */, 461 | ); 462 | defaultConfigurationIsVisible = 0; 463 | defaultConfigurationName = Release; 464 | }; 465 | 8A14344B2917CA8600E8DE1B /* Build configuration list for PBXNativeTarget "TestApp" */ = { 466 | isa = XCConfigurationList; 467 | buildConfigurations = ( 468 | 8A14344C2917CA8600E8DE1B /* Debug */, 469 | 8A14344D2917CA8600E8DE1B /* Release */, 470 | ); 471 | defaultConfigurationIsVisible = 0; 472 | defaultConfigurationName = Release; 473 | }; 474 | 8A14345A2917CAEB00E8DE1B /* Build configuration list for PBXNativeTarget "TestAppUITests" */ = { 475 | isa = XCConfigurationList; 476 | buildConfigurations = ( 477 | 8A14345B2917CAEB00E8DE1B /* Debug */, 478 | 8A14345C2917CAEB00E8DE1B /* Release */, 479 | ); 480 | defaultConfigurationIsVisible = 0; 481 | defaultConfigurationName = Release; 482 | }; 483 | /* End XCConfigurationList section */ 484 | 485 | /* Begin XCSwiftPackageProductDependency section */ 486 | 8A14345F2917CBC800E8DE1B /* NavigationStackBackport */ = { 487 | isa = XCSwiftPackageProductDependency; 488 | productName = NavigationStackBackport; 489 | }; 490 | /* End XCSwiftPackageProductDependency section */ 491 | }; 492 | rootObject = 8A1434352917CA8500E8DE1B /* Project object */; 493 | } 494 | -------------------------------------------------------------------------------- /TestApp/TestApp.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /TestApp/TestApp.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /TestApp/TestApp.xcodeproj/xcshareddata/xcschemes/TestApp.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 31 | 32 | 35 | 36 | 37 | 38 | 48 | 50 | 56 | 57 | 58 | 59 | 65 | 67 | 73 | 74 | 75 | 76 | 78 | 79 | 82 | 83 | 84 | -------------------------------------------------------------------------------- /TestApp/TestApp/Assets.xcassets/AccentColor.colorset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "colors" : [ 3 | { 4 | "idiom" : "universal" 5 | } 6 | ], 7 | "info" : { 8 | "author" : "xcode", 9 | "version" : 1 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /TestApp/TestApp/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "platform" : "ios", 6 | "size" : "1024x1024" 7 | } 8 | ], 9 | "info" : { 10 | "author" : "xcode", 11 | "version" : 1 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /TestApp/TestApp/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "author" : "xcode", 4 | "version" : 1 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /TestApp/TestApp/Preview Content/Preview Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "author" : "xcode", 4 | "version" : 1 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /TestApp/TestApp/TestAppApp.swift: -------------------------------------------------------------------------------- 1 | import struct NavigationStackBackport.NavigationStack 2 | import struct NavigationStackBackport.NavigationPath 3 | import struct NavigationStackBackport.NavigationLink 4 | import SwiftUI 5 | 6 | @main 7 | struct TestAppApp: App { 8 | let testName = ProcessInfo.processInfo.arguments[1] 9 | 10 | var body: some Scene { 11 | WindowGroup { 12 | switch testName { 13 | case "--test-root": 14 | RootTestView() 15 | case "--test-push": 16 | PushTestView() 17 | case "--test-pop": 18 | PopTestView() 19 | case "--test-push-many": 20 | PushManyTestView() 21 | case "--test-pop-many": 22 | PopManyTestView() 23 | case "--test-missing-destination": 24 | MissingDestinationTestView() 25 | case "--test-conditional-destination": 26 | ConditionalDestinationTestView() 27 | case "--test-back-tap": 28 | BackTapTestView() 29 | case "--test-presented-destination": 30 | PresentedDestinationTestView(path: [1, 2]) 31 | case "--test-root-presented-destination": 32 | PresentedDestinationTestView(path: []) 33 | case "--test-item-destination": 34 | DestinatinItemTestView(path: [1, 2]) 35 | case "--test-root-item-destination": 36 | DestinatinItemTestView(path: []) 37 | case "--test-link": 38 | NavigationLinkTestView() 39 | case "--test-link-outside-stack": 40 | NavigationLinkOutsideStackTestView() 41 | case "--test-link-invalid-value": 42 | NavigationLinkWithInvalidValueTestView() 43 | case "--test-link-explicit-state": 44 | NavigationLinkExplicitStateTestView() 45 | default: 46 | fatalError() 47 | } 48 | } 49 | } 50 | } 51 | 52 | struct RootTestView: View { 53 | var body: some View { 54 | NavigationStack { 55 | Text("Root Content") 56 | .navigationTitle("Root Title") 57 | } 58 | } 59 | } 60 | 61 | struct PushTestView: View { 62 | @State private var path = NavigationPath() 63 | 64 | var body: some View { 65 | NavigationStack(path: $path) { 66 | Button("Push") { path.append(0) } 67 | .navigationTitle("Root Title") 68 | .backport.navigationDestination(for: Int.self) { _ in 69 | Text("Pushed Content") 70 | .navigationTitle("Pushed Title") 71 | } 72 | } 73 | } 74 | } 75 | 76 | struct PopTestView: View { 77 | @State private var path = NavigationPath([0]) 78 | 79 | var body: some View { 80 | NavigationStack(path: $path) { 81 | Text("\(path.count)") 82 | .backport.navigationDestination(for: Int.self) { _ in } 83 | } 84 | } 85 | } 86 | 87 | struct PushManyTestView: View { 88 | @State private var path = NavigationPath() 89 | 90 | var body: some View { 91 | NavigationStack(path: $path) { 92 | Button("Push") { path = NavigationPath(Array(0...10)) } 93 | .backport.navigationDestination(for: Int.self) { i in 94 | Text("\(i)") 95 | } 96 | } 97 | } 98 | } 99 | 100 | struct PopManyTestView: View { 101 | @State private var path = NavigationPath() 102 | 103 | var body: some View { 104 | NavigationStack(path: $path) { 105 | Button("Push") { path = NavigationPath(Array(0...10)) } 106 | .backport.navigationDestination(for: Int.self) { i in 107 | Button("Pop") { path = NavigationPath() } 108 | } 109 | } 110 | } 111 | } 112 | 113 | struct MissingDestinationTestView: View { 114 | @State private var path = NavigationPath([0]) 115 | 116 | var body: some View { 117 | NavigationStack(path: $path) { 118 | Text("Root") 119 | } 120 | } 121 | } 122 | 123 | struct ConditionalDestinationTestView: View { 124 | @State private var path = NavigationPath([0]) 125 | @State private var enableDestination = false 126 | 127 | var body: some View { 128 | NavigationStack(path: $path) { 129 | if enableDestination { 130 | Text("\(String(describing: enableDestination))") 131 | .backport.navigationDestination(for: Int.self) { i in 132 | Text("Destination") 133 | } 134 | 135 | } else { 136 | Text("Root") 137 | } 138 | } 139 | .overlay(Button("Toggle Destination") { enableDestination.toggle() }) 140 | } 141 | } 142 | 143 | struct BackTapTestView: View { 144 | @State private var path = NavigationPath(Array(0...5)) 145 | 146 | var body: some View { 147 | NavigationStack(path: $path) { 148 | Text("Root") 149 | .backport.navigationDestination(for: Int.self) { i in 150 | Text("Pushed Content \(i)") 151 | .navigationTitle("Pushed Title") 152 | } 153 | .navigationTitle("Root Title") 154 | } 155 | } 156 | } 157 | 158 | struct PresentedDestinationTestView: View { 159 | @State var path: [Int] 160 | @State private var isPresented = false 161 | @State private var counter = 0 162 | 163 | var body: some View { 164 | NavigationStack(path: $path) { 165 | Text("Root") 166 | .backport.navigationDestination(for: Int.self) { i in 167 | VStack { 168 | Text("Path Destination") 169 | PresentationView() 170 | } 171 | } 172 | .backport.navigationDestination(isPresented: $isPresented) { 173 | VStack { 174 | Text("Presentation") 175 | Text("Counter \(counter)") 176 | PresentationView() 177 | } 178 | } 179 | } 180 | .overlay(VStack { 181 | Button("Toggle Presentation") { isPresented.toggle() } 182 | Button("Update Path") { path[0] += 1 } 183 | Button("Inc Counter") { counter += 1 } 184 | }, alignment: .bottom) 185 | } 186 | 187 | struct PresentationView: View { 188 | @State private var isPresented = false 189 | 190 | var body: some View { 191 | Button("Toggle Nested Presentation") { 192 | isPresented.toggle() 193 | } 194 | .backport.navigationDestination(isPresented: $isPresented) { 195 | Text("Nested Presentation") 196 | } 197 | } 198 | } 199 | } 200 | 201 | struct DestinatinItemTestView: View { 202 | @State var path: [Int] 203 | @State var item: Int? 204 | 205 | var body: some View { 206 | NavigationStack(path: $path) { 207 | Text("Root") 208 | .backport.navigationDestination(for: Int.self) { _ in 209 | VStack { 210 | Text("Path Destination") 211 | PresentationView() 212 | } 213 | } 214 | .backport.navigationDestination(item: $item) { item in 215 | VStack { 216 | Text("Item \(item)") 217 | PresentationView() 218 | } 219 | } 220 | } 221 | .overlay(VStack { 222 | Button("Update Item") { item = (item ?? 0) + 1 } 223 | Button("Clear Item") { item = nil } 224 | Button("Update Path") { path[0] += 1 } 225 | }, alignment: .bottom) 226 | } 227 | 228 | struct PresentationView: View { 229 | @State var item: Int? 230 | 231 | var body: some View { 232 | Button("Update Nested Item") { 233 | item = (item ?? 0) + 1 234 | } 235 | .backport.navigationDestination(item: $item) { _ in 236 | Text("Nested Item") 237 | } 238 | } 239 | } 240 | } 241 | 242 | struct NavigationLinkTestView: View { 243 | var body: some View { 244 | NavigationStack { 245 | NavigationLink("Link", value: 0) 246 | .backport.navigationDestination(for: Int.self) { i in 247 | Text("Link Destination") 248 | } 249 | } 250 | } 251 | } 252 | 253 | struct NavigationLinkOutsideStackTestView: View { 254 | var body: some View { 255 | NavigationLink("Link", value: 0) 256 | } 257 | } 258 | 259 | struct NavigationLinkWithInvalidValueTestView: View { 260 | @State private var path: [Int] = [] 261 | 262 | var body: some View { 263 | NavigationStack(path: $path) { 264 | NavigationLink("Link", value: "0") 265 | } 266 | } 267 | } 268 | 269 | struct NavigationLinkExplicitStateTestView: View { 270 | @State private var path = NavigationPath() 271 | 272 | var body: some View { 273 | NavigationStack(path: $path) { 274 | NavigationLink("Link", value: 0) 275 | .backport.navigationDestination(for: Int.self) { i in 276 | Text("Link Destination") 277 | } 278 | } 279 | } 280 | } 281 | -------------------------------------------------------------------------------- /TestApp/TestAppUITests/DestinationUITests.swift: -------------------------------------------------------------------------------- 1 | import XCTest 2 | 3 | final class DestinationUITests: XCTestCase { 4 | func testMissingDestination() { 5 | let app = XCUIApplication() 6 | app.launchArguments = ["--test-missing-destination"] 7 | app.launch() 8 | if #available(iOS 15.0, *) { 9 | XCTAssert(app.images["Warning"].waitForExistence(timeout: 1)) 10 | } else { 11 | XCTAssert(app.images["warning"].waitForExistence(timeout: 1)) 12 | } 13 | } 14 | 15 | func testConditionalDestination() { 16 | let app = XCUIApplication() 17 | app.launchArguments = ["--test-conditional-destination"] 18 | app.launch() 19 | app.buttons["Toggle Destination"].tap() 20 | XCTAssert(app.staticTexts["Destination"].waitForExistence(timeout: 1)) 21 | app.buttons["Toggle Destination"].tap() 22 | if #available(iOS 15.0, *) { 23 | XCTAssert(app.images["Warning"].waitForExistence(timeout: 1)) 24 | } else { 25 | XCTAssert(app.images["warning"].waitForExistence(timeout: 1)) 26 | } 27 | } 28 | 29 | func testPresentingDestination() { 30 | let app = XCUIApplication() 31 | app.launchArguments = ["--test-presented-destination"] 32 | app.launch() 33 | app.buttons["Toggle Nested Presentation"].tap() 34 | XCTAssert(app.staticTexts["Nested Presentation"].waitForExistence(timeout: 1)) 35 | } 36 | 37 | func testUpdatingPresentingDestination() { 38 | let app = XCUIApplication() 39 | app.launchArguments = ["--test-root-presented-destination"] 40 | app.launch() 41 | app.buttons["Toggle Presentation"].tap() 42 | XCTAssert(app.staticTexts["Counter 0"].waitForExistence(timeout: 1)) 43 | app.buttons["Inc Counter"].tap() 44 | XCTAssert(app.staticTexts["Counter 1"].waitForExistence(timeout: 1)) 45 | } 46 | 47 | func testRootPresentingDestination() { 48 | let app = XCUIApplication() 49 | app.launchArguments = ["--test-root-presented-destination"] 50 | app.launch() 51 | app.buttons["Toggle Presentation"].tap() 52 | XCTAssert(app.staticTexts["Presentation"].waitForExistence(timeout: 1)) 53 | } 54 | 55 | func testRootPresentingDestinationOverPath() throws { 56 | if #available(iOS 16.1, *) {} else if #available(iOS 16.0, *) { 57 | throw XCTSkip("Broken in iOS 16.0, fixed in iOS 16.1") 58 | } 59 | 60 | let app = XCUIApplication() 61 | app.launchArguments = ["--test-presented-destination"] 62 | app.launch() 63 | app.buttons["Toggle Presentation"].tap() 64 | XCTAssert(app.staticTexts["Presentation"].waitForExistence(timeout: 1)) 65 | app.buttons["Back"].tap() 66 | XCTAssert(app.staticTexts["Root"].waitForExistence(timeout: 1)) 67 | } 68 | 69 | func testPresentationWithinPresentation() { 70 | let app = XCUIApplication() 71 | app.launchArguments = ["--test-root-presented-destination"] 72 | app.launch() 73 | app.buttons["Toggle Presentation"].tap() 74 | app.buttons["Toggle Nested Presentation"].tap() 75 | XCTAssert(app.staticTexts["Nested Presentation"].waitForExistence(timeout: 1)) 76 | } 77 | 78 | func testUpdatingPathWithinPresentation() { 79 | let app = XCUIApplication() 80 | app.launchArguments = ["--test-presented-destination"] 81 | app.launch() 82 | app.buttons["Toggle Nested Presentation"].tap() 83 | app.buttons["Update Path"].tap() 84 | XCTAssert(app.staticTexts["Path Destination"].waitForExistence(timeout: 1)) 85 | } 86 | 87 | func testPresentingItem() { 88 | let app = XCUIApplication() 89 | app.launchArguments = ["--test-item-destination"] 90 | app.launch() 91 | app.buttons["Update Nested Item"].tap() 92 | XCTAssert(app.staticTexts["Nested Item"].waitForExistence(timeout: 1)) 93 | } 94 | 95 | func testUpdatingPresentingItem() { 96 | let app = XCUIApplication() 97 | app.launchArguments = ["--test-root-item-destination"] 98 | app.launch() 99 | app.buttons["Update Item"].tap() 100 | XCTAssert(app.staticTexts["Item 1"].waitForExistence(timeout: 1)) 101 | app.buttons["Update Item"].tap() 102 | XCTAssert(app.staticTexts["Item 2"].waitForExistence(timeout: 1)) 103 | } 104 | 105 | func testRootPresentingItem() { 106 | let app = XCUIApplication() 107 | app.launchArguments = ["--test-root-item-destination"] 108 | app.launch() 109 | app.buttons["Update Item"].tap() 110 | XCTAssert(app.staticTexts["Item 1"].waitForExistence(timeout: 1)) 111 | } 112 | 113 | func testRootPresentingItemOverPath() throws { 114 | let app = XCUIApplication() 115 | app.launchArguments = ["--test-item-destination"] 116 | app.launch() 117 | app.buttons["Update Item"].tap() 118 | XCTAssert(app.staticTexts["Item 1"].waitForExistence(timeout: 1)) 119 | app.buttons["Back"].tap() 120 | XCTAssert(app.staticTexts["Root"].waitForExistence(timeout: 1)) 121 | } 122 | 123 | func testItemPresentationWithinItemPresentation() { 124 | let app = XCUIApplication() 125 | app.launchArguments = ["--test-root-item-destination"] 126 | app.launch() 127 | app.buttons["Update Item"].tap() 128 | app.buttons["Update Nested Item"].tap() 129 | XCTAssert(app.staticTexts["Nested Item"].waitForExistence(timeout: 1)) 130 | } 131 | 132 | func testUpdatingPathWithinItemPresentation() throws { 133 | if #available(iOS 17.0, *) { 134 | if #unavailable(iOS 17.2) { 135 | throw XCTSkip("Causes crash on iOS 17.0, fixed in iOS 17.2") 136 | } 137 | } 138 | 139 | let app = XCUIApplication() 140 | app.launchArguments = ["--test-item-destination"] 141 | app.launch() 142 | app.buttons["Update Nested Item"].tap() 143 | app.buttons["Update Path"].tap() 144 | XCTAssert(app.staticTexts["Path Destination"].waitForExistence(timeout: 1)) 145 | } 146 | 147 | func testClearingRootItemPresentation() { 148 | let app = XCUIApplication() 149 | app.launchArguments = ["--test-root-item-destination"] 150 | app.launch() 151 | app.buttons["Update Item"].tap() 152 | app.buttons["Update Nested Item"].tap() 153 | app.buttons["Clear Item"].tap() 154 | XCTAssert(app.staticTexts["Root"].waitForExistence(timeout: 1)) 155 | } 156 | } 157 | -------------------------------------------------------------------------------- /TestApp/TestAppUITests/LinkUITests.swift: -------------------------------------------------------------------------------- 1 | import XCTest 2 | 3 | final class LinkUITests: XCTestCase { 4 | func testNavigationLink() { 5 | let app = XCUIApplication() 6 | app.launchArguments = ["--test-link"] 7 | app.launch() 8 | app.buttons["Link"].tap() 9 | XCTAssert(app.staticTexts["Link Destination"].waitForExistence(timeout: 1)) 10 | } 11 | 12 | func testNavigationLinkOutsideStack() { 13 | let app = XCUIApplication() 14 | app.launchArguments = ["--test-link-outside-stack"] 15 | app.launch() 16 | XCTAssertFalse(app.buttons["Link"].isEnabled) 17 | } 18 | 19 | func testNavigationLinkWithInvalidValue() { 20 | let app = XCUIApplication() 21 | app.launchArguments = ["--test-link-invalid-value"] 22 | app.launch() 23 | app.buttons["Link"].tap() 24 | XCTAssert(app.buttons["Link"].waitForExistence(timeout: 1)) 25 | } 26 | 27 | func testNavigationLinkExplicitState() { 28 | let app = XCUIApplication() 29 | app.launchArguments = ["--test-link-explicit-state"] 30 | app.launch() 31 | app.buttons["Link"].tap() 32 | XCTAssert(app.staticTexts["Link Destination"].waitForExistence(timeout: 1)) 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /TestApp/TestAppUITests/PathUITests.swift: -------------------------------------------------------------------------------- 1 | import XCTest 2 | 3 | final class PathUITests: XCTestCase { 4 | func testRoot() { 5 | let app = XCUIApplication() 6 | app.launchArguments = ["--test-root"] 7 | app.launch() 8 | XCTAssert(app.staticTexts["Root Title"].waitForExistence(timeout: 1)) 9 | XCTAssert(app.staticTexts["Root Content"].waitForExistence(timeout: 1)) 10 | } 11 | 12 | func testPush() { 13 | let app = XCUIApplication() 14 | app.launchArguments = ["--test-push"] 15 | app.launch() 16 | app.buttons["Push"].tap() 17 | XCTAssert(app.staticTexts["Pushed Title"].waitForExistence(timeout: 1)) 18 | XCTAssert(app.staticTexts["Pushed Content"].waitForExistence(timeout: 1)) 19 | } 20 | 21 | func testPop() { 22 | let app = XCUIApplication() 23 | app.launchArguments = ["--test-pop"] 24 | app.launch() 25 | app.buttons["Back"].tap() 26 | XCTAssert(app.staticTexts["0"].waitForExistence(timeout: 1)) 27 | } 28 | 29 | func testPushMany() { 30 | let app = XCUIApplication() 31 | app.launchArguments = ["--test-push-many"] 32 | app.launch() 33 | app.buttons["Push"].tap() 34 | XCTAssert(app.staticTexts["10"].waitForExistence(timeout: 1)) 35 | } 36 | 37 | func testPopMany() { 38 | let app = XCUIApplication() 39 | app.launchArguments = ["--test-pop-many"] 40 | app.launch() 41 | app.buttons["Push"].tap() 42 | _ = app.buttons["Pop"].waitForExistence(timeout: 1) 43 | app.buttons["Pop"].tap() 44 | XCTAssert(app.buttons["Push"].waitForExistence(timeout: 1)) 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /TestApp/TestAppUITests/UserInteractionUITests.swift: -------------------------------------------------------------------------------- 1 | import XCTest 2 | 3 | final class UserInteractionUITests: XCTestCase { 4 | func testBackTap() { 5 | let app = XCUIApplication() 6 | app.launchArguments = ["--test-back-tap"] 7 | app.launch() 8 | app.navigationBars.buttons.firstMatch.tap() 9 | XCTAssert(app.staticTexts["Pushed Content 4"].waitForExistence(timeout: 1)) 10 | } 11 | 12 | func testBackMenu() { 13 | let app = XCUIApplication() 14 | app.launchArguments = ["--test-back-tap"] 15 | app.launch() 16 | app.navigationBars.buttons.firstMatch.press(forDuration: 0.5) 17 | app.collectionViews.buttons["Root Title"].tap() 18 | XCTAssert(app.staticTexts["Root"].waitForExistence(timeout: 1)) 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /TestApp/TestPlan.xctestplan: -------------------------------------------------------------------------------- 1 | { 2 | "configurations" : [ 3 | { 4 | "id" : "FF8FB074-BF18-46EE-A592-F15C530826E5", 5 | "name" : "Configuration 1", 6 | "options" : { 7 | 8 | } 9 | } 10 | ], 11 | "defaultOptions" : { 12 | "testExecutionOrdering" : "random", 13 | "testTimeoutsEnabled" : true 14 | }, 15 | "testTargets" : [ 16 | { 17 | "parallelizable" : true, 18 | "target" : { 19 | "containerPath" : "container:TestApp.xcodeproj", 20 | "identifier" : "8A1434512917CAEB00E8DE1B", 21 | "name" : "TestAppUITests" 22 | } 23 | }, 24 | { 25 | "parallelizable" : true, 26 | "target" : { 27 | "containerPath" : "container:..", 28 | "identifier" : "NavigationStackBackportTests", 29 | "name" : "NavigationStackBackportTests" 30 | } 31 | } 32 | ], 33 | "version" : 1 34 | } 35 | -------------------------------------------------------------------------------- /Tests/NavigationStackBackportTests/NavigationPathItemTests.swift: -------------------------------------------------------------------------------- 1 | @testable import NavigationStackBackport 2 | import XCTest 3 | 4 | final class NavigationPathItemTests: XCTestCase { 5 | func testEquals() { 6 | XCTAssertEqual(NavigationPathItem(value: 0), NavigationPathItem(value: 0)) 7 | XCTAssertEqual(NavigationPathItem(value: 0), NavigationPathItem(typeName: _typeName(Int.self), jsonValue: "0")) 8 | XCTAssertEqual(NavigationPathItem(typeName: _typeName(Int.self), jsonValue: "0"), NavigationPathItem(typeName: _typeName(Int.self), jsonValue: "0")) 9 | XCTAssertEqual(NavigationPathItem(typeName: _typeName(Int.self), jsonValue: "0"), NavigationPathItem(value: 0)) 10 | } 11 | 12 | func testValueAs() { 13 | XCTAssertEqual(0, NavigationPathItem(value: 0).valueAs(Int.self)) 14 | XCTAssertNil(NavigationPathItem(value: 0).valueAs(String.self)) 15 | XCTAssertEqual(0, NavigationPathItem(typeName: _typeName(Int.self), jsonValue: "0").valueAs(Int.self)) 16 | XCTAssertNil(NavigationPathItem(typeName: _typeName(Int.self), jsonValue: "0").valueAs(String.self)) 17 | } 18 | 19 | func testIsCodable() { 20 | struct NonCodable: Hashable {} 21 | XCTAssert(NavigationPathItem(value: 0).isCodable) 22 | XCTAssertFalse(NavigationPathItem(value: NonCodable()).isCodable) 23 | XCTAssert(NavigationPathItem(typeName: _typeName(Int.self), jsonValue: "0").isCodable) 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Tests/NavigationStackBackportTests/NavigationPathTests.swift: -------------------------------------------------------------------------------- 1 | @testable import NavigationStackBackport 2 | import XCTest 3 | 4 | final class NavigationPathTests: XCTestCase { 5 | struct CodableItem: Hashable, Codable { 6 | let x: Int 7 | } 8 | 9 | struct UncodableItem: Hashable {} 10 | 11 | private let encodablePath: NavigationPath = { 12 | var path = NavigationPath() 13 | path.append(1) 14 | path.append("foo") 15 | path.append(CodableItem(x: 2)) 16 | return path 17 | }() 18 | 19 | private let encodedPath = #"["NavigationStackBackportTests.NavigationPathTests.CodableItem","{\"x\":2}","Swift.String","\"foo\"","Swift.Int","1"]"# 20 | 21 | override func setUpWithError() throws { 22 | try super.setUpWithError() 23 | 24 | if #available(iOS 16.0, *) { 25 | throw XCTSkip() 26 | } 27 | } 28 | 29 | func testEncode() throws { 30 | let data = try JSONEncoder().encode(XCTUnwrap(encodablePath.codable)) 31 | XCTAssertEqual(encodedPath, try XCTUnwrap(String(data: data, encoding: .utf8))) 32 | } 33 | 34 | func testDecode() throws { 35 | let decoded = try JSONDecoder().decode(NavigationPath.CodableRepresentation.self, from: try XCTUnwrap(encodedPath.data(using: .utf8))) 36 | XCTAssertEqual(encodablePath, NavigationPath(decoded)) 37 | } 38 | 39 | func testReencodeDecoded() throws { 40 | let decoded = try JSONDecoder().decode(NavigationPath.CodableRepresentation.self, from: try XCTUnwrap(encodedPath.data(using: .utf8))) 41 | let decodedPath = NavigationPath(decoded) 42 | let data = try JSONEncoder().encode(XCTUnwrap(decodedPath.codable)) 43 | XCTAssertEqual(encodedPath, try XCTUnwrap(String(data: data, encoding: .utf8))) 44 | } 45 | 46 | func testCodableIsNilForUncodablePath() throws { 47 | var path = NavigationPath([1]) 48 | XCTAssertNotNil(path.codable) 49 | path.append(UncodableItem()) 50 | XCTAssertNil(path.codable) 51 | } 52 | } 53 | --------------------------------------------------------------------------------