Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove View.inspector(item:) #221

Merged
merged 3 commits into from
Aug 30, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions Sources/SwiftUINavigation/Inspector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,7 @@
item: Binding<Item?>,
@ViewBuilder content: @escaping (Binding<Item>) -> Content
) -> some View {
inspector(isPresented: Binding(item)) {
if let defaultValue = item.wrappedValue {
content(Binding(unwrapping: item, default: defaultValue))
}
}
modifier(InspectorModifier(item: item, destination: content))
}

/// Presents a sheet using a binding as a data source for the sheet's content.
Expand All @@ -72,4 +68,25 @@
}
}
}

@available(iOS 17, macOS 14, *)
@available(tvOS, unavailable)
@available(watchOS, unavailable)
@available(visionOS, unavailable)
private struct InspectorModifier<Item, Destination: View>: ViewModifier {
@Binding var item: Item?
let destination: (Binding<Item>) -> Destination
@State var cachedItem: Item?

func body(content: Content) -> some View {
content.inspector(isPresented: Binding($item)) {
if let defaultValue = item ?? cachedItem {
destination(Binding(unwrapping: $item, default: defaultValue))
.onAppear {
stephencelis marked this conversation as resolved.
Show resolved Hide resolved
cachedItem = defaultValue
}
}
}
}
}
#endif