Skip to content

Commit

Permalink
Move configuration to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
vsanthanam committed Jul 9, 2023
1 parent affa2c9 commit 09102d8
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 44 deletions.
73 changes: 73 additions & 0 deletions Sources/SafariView/Configuration.swift
Original file line number Diff line number Diff line change
@@ -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?

}

}
53 changes: 9 additions & 44 deletions Sources/SafariView/SafariView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 09102d8

Please sign in to comment.