|
38 | 38 | /// the source of truth. Likewise, changes to `value` are instantly reflected in the
|
39 | 39 | /// destination. If `value` becomes `nil`, the destination is popped.
|
40 | 40 | /// - destination: A closure returning the content of the destination.
|
| 41 | + @ViewBuilder |
41 | 42 | public func navigationDestination<Value, Destination: View>(
|
42 | 43 | unwrapping value: Binding<Value?>,
|
43 | 44 | @ViewBuilder destination: (Binding<Value>) -> Destination
|
44 | 45 | ) -> some View {
|
45 |
| - self.modifier( |
46 |
| - _NavigationDestination( |
47 |
| - isPresented: value.isPresent(), |
48 |
| - destination: Binding(unwrapping: value).map(destination) |
| 46 | + if requiresBindWorkaround { |
| 47 | + self.modifier( |
| 48 | + _NavigationDestinationBindWorkaround( |
| 49 | + isPresented: value.isPresent(), |
| 50 | + destination: Binding(unwrapping: value).map(destination) |
| 51 | + ) |
49 | 52 | )
|
50 |
| - ) |
| 53 | + } else { |
| 54 | + self.navigationDestination(isPresented: value.isPresent()) { |
| 55 | + Binding(unwrapping: value).map(destination) |
| 56 | + } |
| 57 | + } |
51 | 58 | }
|
52 | 59 |
|
53 | 60 | /// Pushes a view onto a `NavigationStack` using a binding and case path as a data source for
|
|
79 | 86 | // NB: This view modifier works around a bug in SwiftUI's built-in modifier:
|
80 | 87 | // https://gist.github.com/mbrandonw/f8b94957031160336cac6898a919cbb7#file-fb11056434-md
|
81 | 88 | @available(iOS 16, macOS 13, tvOS 16, watchOS 9, *)
|
82 |
| - private struct _NavigationDestination<Destination: View>: ViewModifier { |
| 89 | + private struct _NavigationDestinationBindWorkaround<Destination: View>: ViewModifier { |
83 | 90 | @Binding var isPresented: Bool
|
84 | 91 | let destination: Destination
|
85 | 92 |
|
|
91 | 98 | .bind(self.$isPresented, to: self.$isPresentedState)
|
92 | 99 | }
|
93 | 100 | }
|
| 101 | + |
| 102 | + private let requiresBindWorkaround = { |
| 103 | + guard #available(iOS 16.4, macOS 13.3, tvOS 16.4, watchOS 9.4, *) |
| 104 | + else { return true } |
| 105 | + return false |
| 106 | + }() |
94 | 107 | #endif
|
0 commit comments