Skip to content

Commit d3bb07a

Browse files
authored
Add presentation style argument to item presentation modifiers (#43)
1 parent 6d8a2c0 commit d3bb07a

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

Sources/SafariView/Presentation/ItemPresentation.swift

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,21 @@ public extension View {
8282
///
8383
/// - Parameters:
8484
/// - 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.
85+
/// - presentationStyle: The ``SafariView/PresentationStyle`` used to present the ``SafariView``.
8586
/// - onDismiss: The closure to execute when dismissing the ``SafariView``
8687
/// - safariView: A closure that returns the ``SafariView`` to present
8788
/// - Returns: The modified view
8889
func safari<Item>(
8990
item: Binding<Item?>,
91+
presentationStyle: SafariView.PresentationStyle = .default,
9092
onDismiss: (() -> Void)? = nil,
9193
safariView: @escaping (Item) -> SafariView
9294
) -> some View where Item: Identifiable {
9395
ModifiedContent(
9496
content: self,
9597
modifier: ItemModifier(
9698
item: item,
99+
presentationStyle: presentationStyle,
97100
safariView: safariView
98101
)
99102
)
@@ -107,10 +110,12 @@ private struct ItemModifier<Item>: ViewModifier where Item: Identifiable {
107110

108111
init(
109112
item: Binding<Item?>,
113+
presentationStyle: SafariView.PresentationStyle,
110114
safariView: @escaping (Item) -> SafariView,
111115
onDismiss: (() -> Void)? = nil
112116
) {
113117
_item = item
118+
self.presentationStyle = presentationStyle
114119
self.safariView = safariView
115120
self.onDismiss = onDismiss
116121
}
@@ -124,6 +129,7 @@ private struct ItemModifier<Item>: ViewModifier where Item: Identifiable {
124129
.background(
125130
Presenter(
126131
item: $item,
132+
presentationStyle: presentationStyle,
127133
safariView: safariView,
128134
onDismiss: onDismiss
129135
)
@@ -138,10 +144,12 @@ private struct ItemModifier<Item>: ViewModifier where Item: Identifiable {
138144

139145
init(
140146
item: Binding<Item?>,
147+
presentationStyle: SafariView.PresentationStyle,
141148
safariView: @escaping (Item) -> SafariView,
142149
onDismiss: (() -> Void)?
143150
) {
144151
_item = item
152+
self.presentationStyle = presentationStyle
145153
self.safariView = safariView
146154
self.onDismiss = onDismiss
147155
}
@@ -156,11 +164,13 @@ private struct ItemModifier<Item>: ViewModifier where Item: Identifiable {
156164

157165
init(
158166
item: Item? = nil,
167+
presentationStyle: SafariView.PresentationStyle,
159168
safariView: @escaping (Item) -> SafariView,
160169
bindingSetter: @escaping (Item?) -> Void,
161170
onDismiss: (() -> Void)?
162171
) {
163172
self.item = item
173+
self.presentationStyle = presentationStyle
164174
self.safariView = safariView
165175
self.bindingSetter = bindingSetter
166176
self.onDismiss = onDismiss
@@ -238,6 +248,7 @@ private struct ItemModifier<Item>: ViewModifier where Item: Identifiable {
238248
// MARK: - Private
239249

240250
private weak var safariViewController: SFSafariViewController?
251+
private let presentationStyle: SafariView.PresentationStyle
241252
private let safariView: (Item) -> SafariView
242253
private var bindingSetter: (Item?) -> Void
243254
private var onInitialLoad: ((Bool) -> Void)?
@@ -259,6 +270,14 @@ private struct ItemModifier<Item>: ViewModifier where Item: Identifiable {
259270
vc.preferredBarTintColor = barTintColor.map(UIColor.init)
260271
vc.preferredControlTintColor = UIColor(controlTintColor)
261272
vc.dismissButtonStyle = dismissButtonStyle.uikit
273+
switch presentationStyle {
274+
case .standard:
275+
break
276+
case .formSheet:
277+
vc.modalPresentationStyle = .formSheet
278+
case .pageSheet:
279+
vc.modalPresentationStyle = .pageSheet
280+
}
262281
guard let presenting = view.controller else {
263282
bindingSetter(nil)
264283
return
@@ -296,17 +315,36 @@ private struct ItemModifier<Item>: ViewModifier where Item: Identifiable {
296315

297316
func makeCoordinator() -> Coordinator {
298317
.init(
318+
item: item,
319+
presentationStyle: presentationStyle,
299320
safariView: safariView,
300321
bindingSetter: { newValue in item = newValue },
301322
onDismiss: onDismiss
302323
)
303324
}
304325

305326
func makeUIView(context: Context) -> UIViewType {
306-
context.coordinator.view
327+
context.coordinator.entersReaderIfAvailable = entersReaderIfAvailable
328+
context.coordinator.barCollapsingEnabled = barCollapsingEnabled
329+
context.coordinator.barTintColor = barTintColor
330+
context.coordinator.controlTintColor = controlTintColor
331+
context.coordinator.dismissButtonStyle = dismissButtonStyle
332+
context.coordinator.includedActivities = includedActivities
333+
context.coordinator.excludedActivityTypes = excludedActivityTypes
334+
context.coordinator.item = item
335+
return context.coordinator.view
307336
}
308337

309-
func updateUIView(_ uiView: UIViewType, context: Context) {}
338+
func updateUIView(_ uiView: UIViewType, context: Context) {
339+
context.coordinator.entersReaderIfAvailable = entersReaderIfAvailable
340+
context.coordinator.barCollapsingEnabled = barCollapsingEnabled
341+
context.coordinator.barTintColor = barTintColor
342+
context.coordinator.controlTintColor = controlTintColor
343+
context.coordinator.dismissButtonStyle = dismissButtonStyle
344+
context.coordinator.includedActivities = includedActivities
345+
context.coordinator.excludedActivityTypes = excludedActivityTypes
346+
context.coordinator.item = item
347+
}
310348

311349
// MARK: - Private
312350

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

375+
private let presentationStyle: SafariView.PresentationStyle
337376
private let safariView: (Item) -> SafariView
338377
private let onDismiss: (() -> Void)?
339378

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

384+
private let presentationStyle: SafariView.PresentationStyle
345385
private let safariView: (Item) -> SafariView
346386
private let onDismiss: (() -> Void)?
347387

Sources/SafariView/Presentation/URLPresentation.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,19 @@ public extension View {
6161
/// ```
6262
/// - Parameters:
6363
/// - url: The URL used to load the view
64+
/// - presentationStyle: The ``SafariView/PresentationStyle`` used to present the ``SafariView``.
6465
/// - onDismiss: The closure to execute when dismissing the ``SafariView``
6566
/// - Returns: The modified view
6667
func safari(
6768
url: Binding<URL?>,
69+
presentationStyle: SafariView.PresentationStyle = .default,
6870
onDismiss: (() -> Void)? = nil
6971
) -> some View {
7072
ModifiedContent(
7173
content: self,
7274
modifier: URLPresentation(
7375
url: url,
76+
presentationStyle: presentationStyle,
7477
onDismiss: onDismiss
7578
)
7679
)
@@ -82,9 +85,11 @@ private struct URLPresentation: ViewModifier {
8285

8386
init(
8487
url: Binding<URL?>,
88+
presentationStyle: SafariView.PresentationStyle,
8589
onDismiss: (() -> Void)?
8690
) {
8791
_url = url
92+
self.presentationStyle = presentationStyle
8893
self.onDismiss = onDismiss
8994
}
9095

@@ -95,6 +100,7 @@ private struct URLPresentation: ViewModifier {
95100
.safari(
96101
item: $url,
97102
id: \.hashValue,
103+
presentationStyle: presentationStyle,
98104
onDismiss: onDismiss
99105
) { url in
100106
SafariView(url: url)
@@ -103,6 +109,7 @@ private struct URLPresentation: ViewModifier {
103109

104110
@Binding
105111
private var url: URL?
112+
private let presentationStyle: SafariView.PresentationStyle
106113
private let onDismiss: (() -> Void)?
107114

108115
}

Sources/SafariView/Presentation/WrappedItemPresentation.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,14 @@ public extension View {
8080
/// - Parameters:
8181
/// - 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.
8282
/// - id: A keypath used to generate stable identifier for instances of Item.
83+
/// - presentationStyle: The ``SafariView/PresentationStyle`` used to present the ``SafariView``.
8384
/// - onDismiss: The closure to execute when dismissing the ``SafariView``
8485
/// - safariView: A closure that returns the ``SafariView`` to present
8586
/// - Returns: The modified view
8687
func safari<Item, ID>(
8788
item: Binding<Item?>,
8889
id: KeyPath<Item, ID>,
90+
presentationStyle: SafariView.PresentationStyle = .default,
8991
onDismiss: (() -> Void)? = nil,
9092
safariView: @escaping (Item) -> SafariView
9193
) -> some View where ID: Hashable {
@@ -94,6 +96,7 @@ public extension View {
9496
modifier: WrappedItemPresentation(
9597
item: item,
9698
id: id,
99+
presentationStyle: presentationStyle,
97100
onDismiss: onDismiss,
98101
safariView: safariView
99102
)
@@ -109,11 +112,13 @@ private struct WrappedItemPresentation<Item, ID>: ViewModifier where ID: Hashabl
109112
init(
110113
item: Binding<Item?>,
111114
id: KeyPath<Item, ID>,
115+
presentationStyle: SafariView.PresentationStyle,
112116
onDismiss: (() -> Void)? = nil,
113117
safariView: @escaping (Item) -> SafariView
114118
) {
115119
_item = item
116120
self.id = id
121+
self.presentationStyle = presentationStyle
117122
self.onDismiss = onDismiss
118123
self.safariView = safariView
119124
}
@@ -124,7 +129,10 @@ private struct WrappedItemPresentation<Item, ID>: ViewModifier where ID: Hashabl
124129
@ViewBuilder
125130
func body(content: Content) -> some View {
126131
content
127-
.safari(item: wrappedItem) { item in
132+
.safari(
133+
item: wrappedItem,
134+
presentationStyle: presentationStyle
135+
) { item in
128136
safariView(item.value)
129137
}
130138
}
@@ -134,6 +142,7 @@ private struct WrappedItemPresentation<Item, ID>: ViewModifier where ID: Hashabl
134142
@Binding
135143
private var item: Item?
136144
private let id: KeyPath<Item, ID>
145+
private let presentationStyle: SafariView.PresentationStyle
137146
private let onDismiss: (() -> Void)?
138147
private let safariView: (Item) -> SafariView
139148

0 commit comments

Comments
 (0)