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

Add presentation style argument to item presentation modifiers #43

Merged
merged 1 commit into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
44 changes: 42 additions & 2 deletions Sources/SafariView/Presentation/ItemPresentation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,21 @@ public extension View {
///
/// - Parameters:
/// - item: A binding to an optional source of truth for the ``SafariView``. When item is non-nil, the system passes the item’s content to the modifier’s closure. You display this content in a ``SafariView`` that you create that the system displays to the user. If item changes, the system dismisses the ``SafariView`` and replaces it with a new one using the same process.
/// - presentationStyle: The ``SafariView/PresentationStyle`` used to present the ``SafariView``.
/// - onDismiss: The closure to execute when dismissing the ``SafariView``
/// - safariView: A closure that returns the ``SafariView`` to present
/// - Returns: The modified view
func safari<Item>(
item: Binding<Item?>,
presentationStyle: SafariView.PresentationStyle = .default,
onDismiss: (() -> Void)? = nil,
safariView: @escaping (Item) -> SafariView
) -> some View where Item: Identifiable {
ModifiedContent(
content: self,
modifier: ItemModifier(
item: item,
presentationStyle: presentationStyle,
safariView: safariView
)
)
Expand All @@ -107,10 +110,12 @@ private struct ItemModifier<Item>: ViewModifier where Item: Identifiable {

init(
item: Binding<Item?>,
presentationStyle: SafariView.PresentationStyle,
safariView: @escaping (Item) -> SafariView,
onDismiss: (() -> Void)? = nil
) {
_item = item
self.presentationStyle = presentationStyle
self.safariView = safariView
self.onDismiss = onDismiss
}
Expand All @@ -124,6 +129,7 @@ private struct ItemModifier<Item>: ViewModifier where Item: Identifiable {
.background(
Presenter(
item: $item,
presentationStyle: presentationStyle,
safariView: safariView,
onDismiss: onDismiss
)
Expand All @@ -138,10 +144,12 @@ private struct ItemModifier<Item>: ViewModifier where Item: Identifiable {

init(
item: Binding<Item?>,
presentationStyle: SafariView.PresentationStyle,
safariView: @escaping (Item) -> SafariView,
onDismiss: (() -> Void)?
) {
_item = item
self.presentationStyle = presentationStyle
self.safariView = safariView
self.onDismiss = onDismiss
}
Expand All @@ -156,11 +164,13 @@ private struct ItemModifier<Item>: ViewModifier where Item: Identifiable {

init(
item: Item? = nil,
presentationStyle: SafariView.PresentationStyle,
safariView: @escaping (Item) -> SafariView,
bindingSetter: @escaping (Item?) -> Void,
onDismiss: (() -> Void)?
) {
self.item = item
self.presentationStyle = presentationStyle
self.safariView = safariView
self.bindingSetter = bindingSetter
self.onDismiss = onDismiss
Expand Down Expand Up @@ -238,6 +248,7 @@ private struct ItemModifier<Item>: ViewModifier where Item: Identifiable {
// MARK: - Private

private weak var safariViewController: SFSafariViewController?
private let presentationStyle: SafariView.PresentationStyle
private let safariView: (Item) -> SafariView
private var bindingSetter: (Item?) -> Void
private var onInitialLoad: ((Bool) -> Void)?
Expand All @@ -259,6 +270,14 @@ private struct ItemModifier<Item>: ViewModifier where Item: Identifiable {
vc.preferredBarTintColor = barTintColor.map(UIColor.init)
vc.preferredControlTintColor = UIColor(controlTintColor)
vc.dismissButtonStyle = dismissButtonStyle.uikit
switch presentationStyle {
case .standard:
break
case .formSheet:
vc.modalPresentationStyle = .formSheet
case .pageSheet:
vc.modalPresentationStyle = .pageSheet
}
guard let presenting = view.controller else {
bindingSetter(nil)
return
Expand Down Expand Up @@ -296,17 +315,36 @@ private struct ItemModifier<Item>: ViewModifier where Item: Identifiable {

func makeCoordinator() -> Coordinator {
.init(
item: item,
presentationStyle: presentationStyle,
safariView: safariView,
bindingSetter: { newValue in item = newValue },
onDismiss: onDismiss
)
}

func makeUIView(context: Context) -> UIViewType {
context.coordinator.view
context.coordinator.entersReaderIfAvailable = entersReaderIfAvailable
context.coordinator.barCollapsingEnabled = barCollapsingEnabled
context.coordinator.barTintColor = barTintColor
context.coordinator.controlTintColor = controlTintColor
context.coordinator.dismissButtonStyle = dismissButtonStyle
context.coordinator.includedActivities = includedActivities
context.coordinator.excludedActivityTypes = excludedActivityTypes
context.coordinator.item = item
return context.coordinator.view
}

func updateUIView(_ uiView: UIViewType, context: Context) {}
func updateUIView(_ uiView: UIViewType, context: Context) {
context.coordinator.entersReaderIfAvailable = entersReaderIfAvailable
context.coordinator.barCollapsingEnabled = barCollapsingEnabled
context.coordinator.barTintColor = barTintColor
context.coordinator.controlTintColor = controlTintColor
context.coordinator.dismissButtonStyle = dismissButtonStyle
context.coordinator.includedActivities = includedActivities
context.coordinator.excludedActivityTypes = excludedActivityTypes
context.coordinator.item = item
}

// MARK: - Private

Expand Down Expand Up @@ -334,6 +372,7 @@ private struct ItemModifier<Item>: ViewModifier where Item: Identifiable {
@Environment(\.safariViewExcludedActivityTypes)
private var excludedActivityTypes: SafariView.ExcludedActivityTypes

private let presentationStyle: SafariView.PresentationStyle
private let safariView: (Item) -> SafariView
private let onDismiss: (() -> Void)?

Expand All @@ -342,6 +381,7 @@ private struct ItemModifier<Item>: ViewModifier where Item: Identifiable {
@Binding
private var item: Item?

private let presentationStyle: SafariView.PresentationStyle
private let safariView: (Item) -> SafariView
private let onDismiss: (() -> Void)?

Expand Down
7 changes: 7 additions & 0 deletions Sources/SafariView/Presentation/URLPresentation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,19 @@ public extension View {
/// ```
/// - Parameters:
/// - url: The URL used to load the view
/// - presentationStyle: The ``SafariView/PresentationStyle`` used to present the ``SafariView``.
/// - onDismiss: The closure to execute when dismissing the ``SafariView``
/// - Returns: The modified view
func safari(
url: Binding<URL?>,
presentationStyle: SafariView.PresentationStyle = .default,
onDismiss: (() -> Void)? = nil
) -> some View {
ModifiedContent(
content: self,
modifier: URLPresentation(
url: url,
presentationStyle: presentationStyle,
onDismiss: onDismiss
)
)
Expand All @@ -82,9 +85,11 @@ private struct URLPresentation: ViewModifier {

init(
url: Binding<URL?>,
presentationStyle: SafariView.PresentationStyle,
onDismiss: (() -> Void)?
) {
_url = url
self.presentationStyle = presentationStyle
self.onDismiss = onDismiss
}

Expand All @@ -95,6 +100,7 @@ private struct URLPresentation: ViewModifier {
.safari(
item: $url,
id: \.hashValue,
presentationStyle: presentationStyle,
onDismiss: onDismiss
) { url in
SafariView(url: url)
Expand All @@ -103,6 +109,7 @@ private struct URLPresentation: ViewModifier {

@Binding
private var url: URL?
private let presentationStyle: SafariView.PresentationStyle
private let onDismiss: (() -> Void)?

}
11 changes: 10 additions & 1 deletion Sources/SafariView/Presentation/WrappedItemPresentation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,14 @@ public extension View {
/// - Parameters:
/// - item: A binding to an optional source of truth for the ``SafariView``. When item is non-nil, the system passes the item’s content to the modifier’s closure. You display this content in a ``SafariView`` that you create that the system displays to the user. If item changes, the system dismisses the ``SafariView`` and replaces it with a new one using the same process.
/// - id: A keypath used to generate stable identifier for instances of Item.
/// - presentationStyle: The ``SafariView/PresentationStyle`` used to present the ``SafariView``.
/// - onDismiss: The closure to execute when dismissing the ``SafariView``
/// - safariView: A closure that returns the ``SafariView`` to present
/// - Returns: The modified view
func safari<Item, ID>(
item: Binding<Item?>,
id: KeyPath<Item, ID>,
presentationStyle: SafariView.PresentationStyle = .default,
onDismiss: (() -> Void)? = nil,
safariView: @escaping (Item) -> SafariView
) -> some View where ID: Hashable {
Expand All @@ -94,6 +96,7 @@ public extension View {
modifier: WrappedItemPresentation(
item: item,
id: id,
presentationStyle: presentationStyle,
onDismiss: onDismiss,
safariView: safariView
)
Expand All @@ -109,11 +112,13 @@ private struct WrappedItemPresentation<Item, ID>: ViewModifier where ID: Hashabl
init(
item: Binding<Item?>,
id: KeyPath<Item, ID>,
presentationStyle: SafariView.PresentationStyle,
onDismiss: (() -> Void)? = nil,
safariView: @escaping (Item) -> SafariView
) {
_item = item
self.id = id
self.presentationStyle = presentationStyle
self.onDismiss = onDismiss
self.safariView = safariView
}
Expand All @@ -124,7 +129,10 @@ private struct WrappedItemPresentation<Item, ID>: ViewModifier where ID: Hashabl
@ViewBuilder
func body(content: Content) -> some View {
content
.safari(item: wrappedItem) { item in
.safari(
item: wrappedItem,
presentationStyle: presentationStyle
) { item in
safariView(item.value)
}
}
Expand All @@ -134,6 +142,7 @@ private struct WrappedItemPresentation<Item, ID>: ViewModifier where ID: Hashabl
@Binding
private var item: Item?
private let id: KeyPath<Item, ID>
private let presentationStyle: SafariView.PresentationStyle
private let onDismiss: (() -> Void)?
private let safariView: (Item) -> SafariView

Expand Down
Loading