Last active
October 20, 2021 04:31
-
-
Save edudnyk/446f745f072ae4a32acbbd39001bd577 to your computer and use it in GitHub Desktop.
Initial sandwich solution implementation in SheeKit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct SheetModifier<Item, SheetContent>: ViewModifier where Item : Identifiable, SheetContent : View { | |
@Binding var item: Item? | |
let presentationStyle: ModalPresentationStyle | |
let presentedViewControllerParameters: UIViewControllerProxy? | |
let onDismiss: (() -> Void)? | |
@ViewBuilder | |
let content: (Item) -> SheetContent | |
@State var proxy = UIViewControllerProxy() | |
func body(content: Content) -> some View { | |
content.overlay(SheetPresenterControllerRepresentable(item: $item, | |
onDismiss: onDismiss, | |
sheetHostProvider: sheetHostProvider, | |
sheetHostUpdater: sheetHostUpdater).opacity(0).accessibilityHidden(true)) | |
} | |
private var sheetHostProvider: (AdaptiveDelegate<Item>, UIViewController, Item, DismissAction) -> SheetHostingController<Item> { { coordinator, presenter, item, dismiss in | |
let sheetHost = SheetHostingController(rootView: sheetContent(for: item, isPresented: true, dismiss: dismiss), item: item) | |
presentationStyle.setup(sheetHost, presenter: presenter, isInitial: true) | |
sheetHost.configure(by: presentedViewControllerParameters) | |
coordinator.sheetHost = sheetHost | |
coordinator.selectedDetentIdentifierBinding = presentationStyle.selectedDetentIdentifierBinding | |
return sheetHost | |
} } | |
private var sheetHostUpdater: (AdaptiveDelegate<Item>, UIViewController, Bool, DismissAction) -> Void { { coordinator, presenter, isPresented, dismiss in | |
guard let sheetHost = coordinator.sheetHost else { return } | |
if isPresented { | |
sheetHost.rootView = sheetContent(for: sheetHost.item, isPresented: isPresented, dismiss: dismiss) | |
presentationStyle.setup(sheetHost, presenter: presenter, isInitial: false) | |
sheetHost.configure(by: presentedViewControllerParameters) | |
coordinator.selectedDetentIdentifierBinding = presentationStyle.selectedDetentIdentifierBinding | |
} | |
} } | |
private func sheetContent(for item: Item, isPresented: Bool, dismiss: DismissAction) -> AnyView { | |
AnyView( | |
content(item) | |
.environment(\.shee_isPresented, isPresented) | |
.environment(\.shee_dismiss, dismiss) | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment