Skip to content

Commit

Permalink
Add presentation style environment key & view modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
vsanthanam committed Mar 23, 2024
1 parent 518296c commit 1fb6b52
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 2 deletions.
1 change: 1 addition & 0 deletions Sources/SafariUI/SafariUI.docc/SafariView.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ You can also use sheet presentation, or any other presentation mechanism of your
### Appearance

- ``DismissButtonStyle``
- ``PresentationStyle``

### Connection Prewarming

Expand Down
1 change: 1 addition & 0 deletions Sources/SafariUI/SafariUI.docc/View.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ SwiftUI view modifiers used to configure a ``SafariView`` or a ``WebAuthenticati
- ``SwiftUI/View/safariBarTintColor(_:)``
- ``SwiftUI/View/safariControlTintColor(_:)``
- ``SwiftUI/View/safariDismissButtonStyle(_:)``
- ``SwiftUI/View/safariPresentationStyle(_:)``

### SafariView Presentation

Expand Down
27 changes: 27 additions & 0 deletions Sources/SafariView/Environment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,22 @@ public extension EnvironmentValues {
set { self[SafariViewExcludedActivityTypesEnvironmentKey.self] = newValue }
}

/// The presentation style used by the ``SafariView`` presentation view modifiers.
///
/// You can retrieve this value for the current scope using the `@Environment` property wrapper
///
/// ```swift
/// struct MyView {
///
/// @Environment(\.safariViewPresentationStyle)
/// var safariViewPresentationStyle
///
/// }
/// ```
var safariViewPresentationStyle: SafariView.PresentationStyle {
get { self[SafariViewPresentationStyleEnvironmentKey.self] }
set { self[SafariViewPresentationStyleEnvironmentKey.self] = newValue }
}
}

@available(iOS 14.0, macCatalyst 14.0, *)
Expand Down Expand Up @@ -170,3 +186,14 @@ private struct SafariViewExcludedActivityTypesEnvironmentKey: EnvironmentKey {
static let defaultValue: Value = .default

}

@available(iOS 14.0, macCatalyst 14.0, *)
private struct SafariViewPresentationStyleEnvironmentKey: EnvironmentKey {

// MARK: - EnvironmentKey

typealias Value = SafariView.PresentationStyle

static let defaultValue: Value = .default

}
38 changes: 38 additions & 0 deletions Sources/SafariView/Modifiers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,12 @@ public extension View {
)
}

func safariPresentationStyle(_ presentationStyle: SafariView.PresentationStyle) -> some View {
ModifiedContent(
content: self,
modifier: SafariViewPresentationStyleModifier(presentationStyle: presentationStyle)
)
}
}

@available(iOS 14.0, macCatalyst 14.0, *)
Expand All @@ -235,6 +241,7 @@ private struct SafariViewEntersReaderIfAvailableModifier: ViewModifier {

// MARK: - ViewModifier

@MainActor
@ViewBuilder
func body(content: Content) -> some View {
content
Expand All @@ -261,6 +268,7 @@ private struct SafariViewBarCollapsingEnabledModifier: ViewModifier {

// MARK: - ViewModifier

@MainActor
@ViewBuilder
func body(content: Content) -> some View {
content
Expand All @@ -286,6 +294,7 @@ private struct SafariViewControlTintColorModifier: ViewModifier {

// MARK: - ViewModifier

@MainActor
@ViewBuilder
func body(content: Content) -> some View {
content
Expand All @@ -312,6 +321,7 @@ private struct SafariViewBarTintColorModifier: ViewModifier {

// MARK: - ViewModifier

@MainActor
@ViewBuilder
func body(content: Content) -> some View {
content
Expand All @@ -338,6 +348,7 @@ private struct SafariViewDismissButtonStyleModifier: ViewModifier {

// MARK: - ViewModifier

@MainActor
@ViewBuilder
func body(content: Content) -> some View {
content
Expand All @@ -364,6 +375,7 @@ private struct SafariViewIncludedActivitiesModifier: ViewModifier {

// MARK: - ViewModifier

@MainActor
@ViewBuilder
func body(content: Content) -> some View {
content
Expand All @@ -390,6 +402,7 @@ private struct SafariViewExcludedActivityTypesModifier: ViewModifier {

// MARK: - ViewModifier

@MainActor
@ViewBuilder
func body(content: Content) -> some View {
content
Expand All @@ -404,3 +417,28 @@ private struct SafariViewExcludedActivityTypesModifier: ViewModifier {
private let activityTypes: SafariView.ExcludedActivityTypes

}

@available(iOS 14.0, macCatalyst 14.0, *)
private struct SafariViewPresentationStyleModifier: ViewModifier {

// MARK: - Initializers

init(presentationStyle: SafariView.PresentationStyle) {
self.presentationStyle = presentationStyle
}

@MainActor
@ViewBuilder
func body(content: Content) -> some View {
content
.environment(
\.safariViewPresentationStyle,
presentationStyle
)
}

// MARK: - Private

private let presentationStyle: SafariView.PresentationStyle

}
15 changes: 15 additions & 0 deletions Sources/SafariView/Presentation/BoolPresentation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ private struct IsPresentedModifier: ViewModifier {
context.coordinator.dismissButtonStyle = dismissButtonStyle
context.coordinator.includedActivities = includedActivities
context.coordinator.excludedActivityTypes = excludedActivityTypes
context.coordinator.presentationStyle = presentationStyle
context.coordinator.isPresented = isPresented
return context.coordinator.view
}
Expand All @@ -183,6 +184,7 @@ private struct IsPresentedModifier: ViewModifier {
context.coordinator.dismissButtonStyle = dismissButtonStyle
context.coordinator.includedActivities = includedActivities
context.coordinator.excludedActivityTypes = excludedActivityTypes
context.coordinator.presentationStyle = presentationStyle
context.coordinator.isPresented = isPresented
}

Expand Down Expand Up @@ -234,6 +236,7 @@ private struct IsPresentedModifier: ViewModifier {
var dismissButtonStyle: SafariView.DismissButtonStyle = .default
var includedActivities: SafariView.IncludedActivities = []
var excludedActivityTypes: SafariView.ExcludedActivityTypes = []
var presentationStyle: SafariView.PresentationStyle = .default

func safariViewController(_ controller: SFSafariViewController, didCompleteInitialLoad didLoadSuccessfully: Bool) {
onInitialLoad?(didLoadSuccessfully)
Expand Down Expand Up @@ -282,6 +285,15 @@ private struct IsPresentedModifier: ViewModifier {

private func presentSafari() {
let vc = SFSafariViewController(url: url, configuration: buildConfiguration())
print(vc.modalPresentationStyle)
switch presentationStyle {
case .standard:
break
case .formSheet:
vc.modalPresentationStyle = .formSheet
case .pageSheet:
vc.modalPresentationStyle = .pageSheet
}
vc.delegate = self
vc.preferredBarTintColor = barTintColor.map(UIColor.init)
vc.preferredControlTintColor = UIColor(controlTintColor)
Expand Down Expand Up @@ -346,6 +358,9 @@ private struct IsPresentedModifier: ViewModifier {
@Environment(\.safariViewExcludedActivityTypes)
private var excludedActivityTypes: SafariView.ExcludedActivityTypes

@Environment(\.safariViewPresentationStyle)
private var presentationStyle: SafariView.PresentationStyle

private let url: URL
private let onInitialLoad: ((Bool) -> Void)?
private let onInitialRedirect: ((URL) -> Void)?
Expand Down
35 changes: 33 additions & 2 deletions Sources/SafariView/Presentation/ItemPresentation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ private struct ItemModifier<Item>: ViewModifier where Item: Identifiable {
var dismissButtonStyle: SafariView.DismissButtonStyle = .default
var includedActivities: SafariView.IncludedActivities = []
var excludedActivityTypes: SafariView.ExcludedActivityTypes = []
var presentationStyle: SafariView.PresentationStyle = .default

// MARK: - SFSafariViewDelegate

Expand Down Expand Up @@ -256,6 +257,14 @@ private struct ItemModifier<Item>: ViewModifier where Item: Identifiable {
activityButton = safari.activityButton
eventAttribution = safari.eventAttribution
let vc = SFSafariViewController(url: safari.url, configuration: buildConfiguration())
switch presentationStyle {
case .standard:
break
case .formSheet:
vc.modalPresentationStyle = .formSheet
case .pageSheet:
vc.modalPresentationStyle = .pageSheet
}
vc.delegate = self
vc.preferredBarTintColor = barTintColor.map(UIColor.init)
vc.preferredControlTintColor = UIColor(controlTintColor)
Expand Down Expand Up @@ -304,10 +313,29 @@ private struct ItemModifier<Item>: ViewModifier where Item: Identifiable {
}

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.presentationStyle = presentationStyle
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.presentationStyle = presentationStyle
context.coordinator.item = item
}

// MARK: - Private

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

@Environment(\.safariViewPresentationStyle)
private var presentationStyle: SafariView.PresentationStyle

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

Expand Down
37 changes: 37 additions & 0 deletions Sources/SafariView/PresentationStyle.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// SafariUI
// PresentationStyle.swift
//
// MIT License
//
// Copyright (c) 2023 Varun Santhanam
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
//
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import Foundation

public extension SafariView {

enum PresentationStyle {
case standard
case formSheet
case pageSheet
public static let `default`: PresentationStyle = .standard
}

}

0 comments on commit 1fb6b52

Please sign in to comment.