Skip to content

Commit 6d8a2c0

Browse files
authored
Add PresentationStyle enumeration + argument to standard presentation modifier(s) (#42)
1 parent a3d62b0 commit 6d8a2c0

File tree

5 files changed

+75
-10
lines changed

5 files changed

+75
-10
lines changed

Sources/SafariUI/SafariUI.docc/SafariView.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ UI features include the following:
1919

2020
You can present a `SafariView` using the built-in presentation view modifiers:
2121

22-
- ``SwiftUI/View/safari(isPresented:onDismiss:safariView:)``
23-
- ``SwiftUI/View/safari(isPresented:url:onDismiss:)``
22+
- ``SwiftUI/View/safari(isPresented:presentationStyle:onDismiss:safariView:)``
23+
- ``SwiftUI/View/safari(isPresented:url:presentationStyle:onDismiss:)``
2424
- ``SwiftUI/View/safari(item:onDismiss:safariView:)``
2525
- ``SwiftUI/View/safari(item:id:onDismiss:safariView:)``
2626
- ``SwiftUI/View/safari(url:onDismiss:)``

Sources/SafariUI/SafariUI.docc/View.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ SwiftUI view modifiers used to configure a ``SafariView`` or a ``WebAuthenticati
1717

1818
### SafariView Presentation
1919

20-
- ``SwiftUI/View/safari(isPresented:onDismiss:safariView:)``
21-
- ``SwiftUI/View/safari(isPresented:url:onDismiss:)``
20+
- ``SwiftUI/View/safari(isPresented:presentationStyle:onDismiss:safariView:)``
21+
- ``SwiftUI/View/safari(isPresented:url:presentationStyle:onDismiss:)``
2222
- ``SwiftUI/View/safari(item:onDismiss:safariView:)``
2323
- ``SwiftUI/View/safari(item:id:onDismiss:safariView:)``
2424
- ``SwiftUI/View/safari(url:onDismiss:)``

Sources/SafariView/Presentation/BoolPresentation.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,21 @@ public extension View {
6868
///
6969
/// - Parameters:
7070
/// - isPresented: A binding to a Boolean value that determines whether to present the ``SafariView`` that you create in the modifier’s content closure.
71+
/// - presentationStyle: The ``SafariView/PresentationStyle`` used to present the ``SafariView``.
7172
/// - onDismiss: The closure to execute when dismissing the ``SafariView``
7273
/// - safariView: A closure that returns the ``SafariView`` to present
7374
/// - Returns: The modified view
7475
func safari(
7576
isPresented: Binding<Bool>,
77+
presentationStyle: SafariView.PresentationStyle = .default,
7678
onDismiss: (() -> Void)? = nil,
7779
safariView: () -> SafariView
7880
) -> some View {
7981
ModifiedContent(
8082
content: self,
8183
modifier: IsPresentedModifier(
8284
isPresented: isPresented,
85+
presentationStyle: presentationStyle,
8386
safariView: safariView(),
8487
onDismiss: onDismiss
8588
)
@@ -95,10 +98,12 @@ private struct IsPresentedModifier: ViewModifier {
9598

9699
init(
97100
isPresented: Binding<Bool>,
101+
presentationStyle: SafariView.PresentationStyle,
98102
safariView: SafariView,
99103
onDismiss: (() -> Void)?
100104
) {
101105
self.isPresented = isPresented
106+
self.presentationStyle = presentationStyle
102107
self.safariView = safariView
103108
self.onDismiss = onDismiss
104109
}
@@ -113,6 +118,7 @@ private struct IsPresentedModifier: ViewModifier {
113118
Presenter(
114119
isPresented: isPresented,
115120
url: safariView.url,
121+
presentationStyle: presentationStyle,
116122
onInitialLoad: safariView.onInitialLoad,
117123
onInitialRedirect: safariView.onInitialRedirect,
118124
onOpenInBrowser: safariView.onOpenInBrowser,
@@ -130,6 +136,7 @@ private struct IsPresentedModifier: ViewModifier {
130136
init(
131137
isPresented: Binding<Bool>,
132138
url: URL,
139+
presentationStyle: SafariView.PresentationStyle,
133140
onInitialLoad: ((Bool) -> Void)?,
134141
onInitialRedirect: ((URL) -> Void)?,
135142
onOpenInBrowser: (() -> Void)?,
@@ -139,6 +146,7 @@ private struct IsPresentedModifier: ViewModifier {
139146
) {
140147
_isPresented = isPresented
141148
self.url = url
149+
self.presentationStyle = presentationStyle
142150
self.onInitialLoad = onInitialLoad
143151
self.onInitialRedirect = onInitialRedirect
144152
self.onOpenInBrowser = onOpenInBrowser
@@ -153,6 +161,7 @@ private struct IsPresentedModifier: ViewModifier {
153161
.init(
154162
isPresented: isPresented,
155163
url: url,
164+
presentationStyle: presentationStyle,
156165
bindingSetter: { newValue in isPresented = newValue },
157166
onInitialLoad: onInitialLoad,
158167
onInitialRedirect: onInitialRedirect,
@@ -191,6 +200,7 @@ private struct IsPresentedModifier: ViewModifier {
191200
init(
192201
isPresented: Bool,
193202
url: URL,
203+
presentationStyle: SafariView.PresentationStyle,
194204
bindingSetter: @escaping (Bool) -> Void,
195205
onInitialLoad: ((Bool) -> Void)?,
196206
onInitialRedirect: ((URL) -> Void)?,
@@ -201,6 +211,7 @@ private struct IsPresentedModifier: ViewModifier {
201211
) {
202212
self.isPresented = isPresented
203213
self.url = url
214+
self.presentationStyle = presentationStyle
204215
self.bindingSetter = bindingSetter
205216
self.onInitialLoad = onInitialLoad
206217
self.onInitialRedirect = onInitialRedirect
@@ -272,6 +283,7 @@ private struct IsPresentedModifier: ViewModifier {
272283

273284
private weak var safariViewController: SFSafariViewController?
274285
private let url: URL
286+
private let presentationStyle: SafariView.PresentationStyle
275287
private let bindingSetter: (Bool) -> Void
276288
private let onInitialLoad: ((Bool) -> Void)?
277289
private let onInitialRedirect: ((URL) -> Void)?
@@ -286,6 +298,14 @@ private struct IsPresentedModifier: ViewModifier {
286298
vc.preferredBarTintColor = barTintColor.map(UIColor.init)
287299
vc.preferredControlTintColor = UIColor(controlTintColor)
288300
vc.dismissButtonStyle = dismissButtonStyle.uikit
301+
switch presentationStyle {
302+
case .standard:
303+
break
304+
case .formSheet:
305+
vc.modalPresentationStyle = .formSheet
306+
case .pageSheet:
307+
vc.modalPresentationStyle = .pageSheet
308+
}
289309
guard let presenting = view.controller else {
290310
bindingSetter(false)
291311
return
@@ -347,6 +367,7 @@ private struct IsPresentedModifier: ViewModifier {
347367
private var excludedActivityTypes: SafariView.ExcludedActivityTypes
348368

349369
private let url: URL
370+
private let presentationStyle: SafariView.PresentationStyle
350371
private let onInitialLoad: ((Bool) -> Void)?
351372
private let onInitialRedirect: ((URL) -> Void)?
352373
private let onOpenInBrowser: (() -> Void)?
@@ -357,6 +378,7 @@ private struct IsPresentedModifier: ViewModifier {
357378
}
358379

359380
private let isPresented: Binding<Bool>
381+
private let presentationStyle: SafariView.PresentationStyle
360382
private let safariView: SafariView
361383
private let onDismiss: (() -> Void)?
362384

Sources/SafariView/Presentation/BoolURLPresentation.swift

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,22 @@ public extension View {
6666
/// - Parameters:
6767
/// - isPresented: A binding to a Boolean value that determines whether to present the ``SafariView`` that you create in the modifier’s content closure.
6868
/// - url: The URL to load in the presented ``SafariView``
69+
/// - presentationStyle: The ``SafariView/PresentationStyle`` used to present the ``SafariView``.
6970
/// - onDismiss: The closure to execute when dismissing the ``SafariView``
7071
/// - Returns: The modified view
7172
func safari(
7273
isPresented: Binding<Bool>,
7374
url: URL,
75+
presentationStyle: SafariView.PresentationStyle = .default,
7476
onDismiss: (() -> Void)? = nil
7577
) -> some View {
7678
ModifiedContent(
7779
content: self,
7880
modifier: BoolURLPresentation(
7981
isPresented: isPresented,
80-
onDismiss: onDismiss,
81-
url: url
82+
url: url,
83+
presentationStyle: presentationStyle,
84+
onDismiss: onDismiss
8285
)
8386
)
8487
}
@@ -89,12 +92,14 @@ private struct BoolURLPresentation: ViewModifier {
8992

9093
init(
9194
isPresented: Binding<Bool>,
92-
onDismiss: (() -> Void)?,
93-
url: URL
95+
url: URL,
96+
presentationStyle: SafariView.PresentationStyle,
97+
onDismiss: (() -> Void)?
9498
) {
9599
_isPresented = isPresented
96-
self.onDismiss = onDismiss
97100
self.url = url
101+
self.presentationStyle = presentationStyle
102+
self.onDismiss = onDismiss
98103
}
99104

100105
@MainActor
@@ -103,6 +108,7 @@ private struct BoolURLPresentation: ViewModifier {
103108
content
104109
.safari(
105110
isPresented: $isPresented,
111+
presentationStyle: presentationStyle,
106112
onDismiss: onDismiss
107113
) {
108114
SafariView(url: url)
@@ -111,7 +117,8 @@ private struct BoolURLPresentation: ViewModifier {
111117

112118
@Binding
113119
private var isPresented: Bool
114-
private let onDismiss: (() -> Void)?
115120
private let url: URL
121+
private let presentationStyle: SafariView.PresentationStyle
122+
private let onDismiss: (() -> Void)?
116123

117124
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// SafariUI
2+
// PresentationStyle.swift
3+
//
4+
// MIT License
5+
//
6+
// Copyright (c) 2023 Varun Santhanam
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files (the Software), to deal
9+
//
10+
// in the Software without restriction, including without limitation the rights
11+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
// copies of the Software, and to permit persons to whom the Software is
13+
// furnished to do so, subject to the following conditions:
14+
//
15+
// The above copyright notice and this permission notice shall be included in all
16+
// copies or substantial portions of the Software.
17+
//
18+
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
// SOFTWARE.
25+
26+
@available(iOS 14.0, visionOS 1.0, macCatalyst 14.0, *)
27+
public extension SafariView {
28+
29+
enum PresentationStyle: Equatable, Hashable, Sendable, Codable {
30+
case standard
31+
case formSheet
32+
case pageSheet
33+
public static let `default`: PresentationStyle = .standard
34+
}
35+
36+
}

0 commit comments

Comments
 (0)