From 09102d81b14e6459dc3ac6248e97ea995ce2d219 Mon Sep 17 00:00:00 2001 From: Varun Santhanam Date: Sun, 9 Jul 2023 14:17:59 -0700 Subject: [PATCH] Move configuration to separate file --- Sources/SafariView/Configuration.swift | 73 ++++++++++++++++++++++++++ Sources/SafariView/SafariView.swift | 53 ++++--------------- 2 files changed, 82 insertions(+), 44 deletions(-) create mode 100644 Sources/SafariView/Configuration.swift diff --git a/Sources/SafariView/Configuration.swift b/Sources/SafariView/Configuration.swift new file mode 100644 index 000000000..d46e089cd --- /dev/null +++ b/Sources/SafariView/Configuration.swift @@ -0,0 +1,73 @@ +// SafariView +// Configuration.swift +// +// MIT License +// +// Copyright (c) 2021 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 UIKit + +public extension SafariView { + + /// The configuration for a ``SafariView/SafariView`` + /// + /// To change the configuration for the current scope, see the ``SwiftUI/View/safariConfiguration(_:)`` view modifier. + struct Configuration { + + /// Create a new safari view configuration + /// - Parameters: + /// - entersReaderIfAvailable: Whether or not the view should enter reader mode automatically, if available. + /// - barCollapsingEnabled: Whether or not the view should support bar collapsing. + /// - activityButton: A custom activity button. + /// - eventAttribution: Event attribution data for Private Click Measurement. + public init( + entersReaderIfAvailable: Bool = false, + barCollapsingEnabled: Bool = false, + activityButton: ActivityButton? = nil, + eventAttribution: UIEventAttribution? = nil + ) { + self.entersReaderIfAvailable = entersReaderIfAvailable + self.barCollapsingEnabled = barCollapsingEnabled + self.activityButton = activityButton + self.eventAttribution = eventAttribution + } + + /// A value that specifies whether Safari should enter Reader mode, if it is available. + /// + /// Set the value to `true` if Reader mode should be entered automatically when it is available for the webpage; otherwise, false. The default value is `false`. + public var entersReaderIfAvailable: Bool + + /// A value that specifies whether Safari should allow bar collapsing + /// + /// Set the value `true` if bar collapsing should be enabled when the user scrolls; otherwise, false. The default value is `false`. + public var barCollapsingEnabled: Bool + + /// The activity button to use in the Safari View. + public var activityButton: ActivityButton? + + /// An object you use to send tap event attribution data to the browser for Private Click Measurement. + /// + /// For more information about preparing event attribution data, see [`UIEventAttribution`](https://developer.apple.com/documentation/uikit/uieventattribution). + public var eventAttribution: UIEventAttribution? + + } + +} diff --git a/Sources/SafariView/SafariView.swift b/Sources/SafariView/SafariView.swift index 4149c0449..bf9220a51 100644 --- a/Sources/SafariView/SafariView.swift +++ b/Sources/SafariView/SafariView.swift @@ -60,55 +60,15 @@ public struct SafariView: View { /// A convenience typealias for [`SFSafariViewController.ActivityButton`](https://developer.apple.com/documentation/safariservices/sfsafariviewcontroller/activitybutton) /// /// To change the configuration for the current scope, see the ``SwiftUI/View/safariConfiguration(_:)`` view modifier. + @available(iOS 15.0, *) public typealias ActivityButton = SFSafariViewController.ActivityButton /// A convenience typealias for [`SFSafariViewController.PrewarmingToken`](https://developer.apple.com/documentation/safariservices/sfsafariviewcontroller/prewarmingtoken) /// /// You can generate prewarming tokens for invalidation using the ``prewarmConnections(to:)`` static method. + @available(iOS 15.0, *) public typealias PrewarmingToken = SFSafariViewController.PrewarmingToken - /// The configuration for a ``SafariView/SafariView`` - /// - /// To change the configuration for the current scope, see the ``SwiftUI/View/safariConfiguration(_:)`` view modifier. - public struct Configuration { - - /// Create a configuration for a ``SafariView/SafariView`` - /// - Parameters: - /// - entersReaderIfAvailable: Whether or not Safari should enter reader mode, if it is available. - /// - barCollapsingEnabled: Whether or not Safari should support bar collapsing. - /// - eventAttribution: Event attribution for Private Click Measurement. - /// - activityButton: A custom activity button. - public init( - entersReaderIfAvailable: Bool = false, - barCollapsingEnabled: Bool = false, - eventAttribution: UIEventAttribution? = nil, - activityButton: ActivityButton? = nil - ) { - self.entersReaderIfAvailable = entersReaderIfAvailable - self.barCollapsingEnabled = barCollapsingEnabled - self.eventAttribution = eventAttribution - self.activityButton = activityButton - } - - /// A value that specifies whether Safari should enter Reader mode, if it is available. - /// - /// Set the value to `true` if Reader mode should be entered automatically when it is available for the webpage; otherwise, false. The default value is `false`. - public var entersReaderIfAvailable: Bool - - /// A value that specifies whether Safari should allow bar collapsing - /// - /// Set the value `true` if bar collapsing should be enabled when the user scrolls; otherwise, false. The default value is `false`. - public var barCollapsingEnabled: Bool - - /// An object you use to send tap event attribution data to the browser for Private Click Measurement. - /// - /// For more information about preparing event attribution data, see [`UIEventAttribution`](https://developer.apple.com/documentation/uikit/uieventattribution). - public var eventAttribution: UIEventAttribution? - - /// The activity button to use in the Safari View. - public var activityButton: ActivityButton? - } - /// Prewarm the connection to a list of provided URLs /// /// You can use this returned value of this method to invalidate the prewarmed cache by invoking the `invaldate()` method on the token. @@ -121,6 +81,7 @@ public struct SafariView: View { /// /// - Parameter URLs: The URLs to prewarm /// - Returns: A prewarming token for the provided URLs. + @available(iOS 15.0, *) @discardableResult public static func prewarmConnections(to URLs: [URL]) -> PrewarmingToken { SFSafariViewController.prewarmConnections(to: URLs) @@ -683,8 +644,12 @@ private extension SafariView.Configuration { let configuration = SFSafariViewController.Configuration() configuration.entersReaderIfAvailable = entersReaderIfAvailable configuration.barCollapsingEnabled = barCollapsingEnabled - configuration.eventAttribution = eventAttribution - configuration.activityButton = activityButton + if #available(iOS 15.2, *) { + configuration.eventAttribution = eventAttribution + } + if #available(iOS 15.0, *) { + configuration.activityButton = activityButton + } return configuration }