Scope `navigationDestination` bind workaround (#93)

* Scope `navigationDestination` bind workaround

* Update NavigationDestination.swift

* wip
This commit is contained in:
Stephen Celis 2023-03-14 11:31:25 -07:00 committed by GitHub
parent 1bc2bc9111
commit 47dd574b90
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 6 deletions

View File

@ -38,16 +38,23 @@
/// the source of truth. Likewise, changes to `value` are instantly reflected in the /// the source of truth. Likewise, changes to `value` are instantly reflected in the
/// destination. If `value` becomes `nil`, the destination is popped. /// destination. If `value` becomes `nil`, the destination is popped.
/// - destination: A closure returning the content of the destination. /// - destination: A closure returning the content of the destination.
@ViewBuilder
public func navigationDestination<Value, Destination: View>( public func navigationDestination<Value, Destination: View>(
unwrapping value: Binding<Value?>, unwrapping value: Binding<Value?>,
@ViewBuilder destination: (Binding<Value>) -> Destination @ViewBuilder destination: (Binding<Value>) -> Destination
) -> some View { ) -> some View {
self.modifier( if requiresBindWorkaround {
_NavigationDestination( self.modifier(
isPresented: value.isPresent(), _NavigationDestinationBindWorkaround(
destination: Binding(unwrapping: value).map(destination) isPresented: value.isPresent(),
destination: Binding(unwrapping: value).map(destination)
)
) )
) } else {
self.navigationDestination(isPresented: value.isPresent()) {
Binding(unwrapping: value).map(destination)
}
}
} }
/// Pushes a view onto a `NavigationStack` using a binding and case path as a data source for /// Pushes a view onto a `NavigationStack` using a binding and case path as a data source for
@ -79,7 +86,7 @@
// NB: This view modifier works around a bug in SwiftUI's built-in modifier: // NB: This view modifier works around a bug in SwiftUI's built-in modifier:
// https://gist.github.com/mbrandonw/f8b94957031160336cac6898a919cbb7#file-fb11056434-md // https://gist.github.com/mbrandonw/f8b94957031160336cac6898a919cbb7#file-fb11056434-md
@available(iOS 16, macOS 13, tvOS 16, watchOS 9, *) @available(iOS 16, macOS 13, tvOS 16, watchOS 9, *)
private struct _NavigationDestination<Destination: View>: ViewModifier { private struct _NavigationDestinationBindWorkaround<Destination: View>: ViewModifier {
@Binding var isPresented: Bool @Binding var isPresented: Bool
let destination: Destination let destination: Destination
@ -91,4 +98,10 @@
.bind(self.$isPresented, to: self.$isPresentedState) .bind(self.$isPresented, to: self.$isPresentedState)
} }
} }
private let requiresBindWorkaround = {
guard #available(iOS 16.4, macOS 13.3, tvOS 16.4, watchOS 9.4, *)
else { return true }
return false
}()
#endif #endif