diff --git a/Sources/SafariView/Environment.swift b/Sources/SafariView/Environment.swift index 7de8f6eb8..27a1c12d8 100644 --- a/Sources/SafariView/Environment.swift +++ b/Sources/SafariView/Environment.swift @@ -28,6 +28,23 @@ import SwiftUI @available(iOS 15.0, macCatalyst 15.0, *) public extension EnvironmentValues { + /// The configuration value used by a ``SafariView`` + /// + /// You can retrieve this value for the currnet scope using the `@Environment` property wrapper + /// + /// ```swift + /// struct MyView: View { + /// + /// @Environment(\.safariViewConfiguration) + /// var safariViewConfiguration + /// + /// } + /// ``` + var safariViewConfiguration: SafariView.Configuration { + get { self[SafariViewConfigurationEnvironmentKey.self] } + set { self[SafariViewConfigurationEnvironmentKey.self] = newValue } + } + /// The additional activies to include the share sheet displayed inside a ``SafariView`` /// /// You can retrieve this value for the currnet scope using the `@Environment` property wrapper @@ -39,7 +56,7 @@ public extension EnvironmentValues { /// var safariViewIncludedActivities /// /// } - /// ` + /// ``` var safariViewIncludedActivities: SafariView.IncludedActivities { get { self[SafariViewIncludedActivitiesEnvironmentKey.self] } set { self[SafariViewIncludedActivitiesEnvironmentKey.self] = newValue } diff --git a/Sources/SafariView/Modifiers.swift b/Sources/SafariView/Modifiers.swift index 7e01e58e7..550b10399 100644 --- a/Sources/SafariView/Modifiers.swift +++ b/Sources/SafariView/Modifiers.swift @@ -28,22 +28,19 @@ import SwiftUI @available(iOS 15.0, macCatalyst 15.0, *) public extension View { - /// Set the bar tint color of safari views within this view - /// - /// Use this modifier to set the bar tint color of safari views within this view: + /// Set the configuration of safari views within this view /// - /// ```swift - /// struct MyView: View { - /// var body: some View { - /// HStack { - /// SafariView(url: URL("https://www.apple.com")!) - /// } - /// .safariBarTintColor(Color.purple) - /// } - /// } - /// ``` + /// - Parameter configuration: The configuration to use + /// - Returns: The modified view + func safariConfiguration(_ configuration: SafariView.Configuration) -> some View { + let modifier = SafariViewConfigurationModifier(configuration: configuration) + return ModifiedContent(content: self, modifier: modifier) + } + + /// Set the bar tint color of safari views within this view /// /// This modifier is the equivelent of the [`.preferredBarTintColor`](https://developer.apple.com/documentation/safariservices/sfsafariviewcontroller/2274394-preferredbartintcolor) property of a [`SFSafariViewController`](https://developer.apple.com/documentation/safariservices/sfsafariviewcontroller) + /// /// - Parameter color: The color to use, or `nil` for the system default /// - Returns: The modified view func safariBarTintColor(_ color: Color?) -> some View { @@ -54,6 +51,7 @@ public extension View { /// Set the control tint color of safari views within this view /// /// This modifier is the equivelent of the [`.preferredControlTintColor`](https://developer.apple.com/documentation/safariservices/sfsafariviewcontroller/2274393-preferredcontroltintcolor) property of a [`SFSafariViewController`](https://developer.apple.com/documentation/safariservices/sfsafariviewcontroller) + /// /// - Parameter color: The color to use /// - Returns: The modified view func safariControlTintColor(_ color: Color) -> some View { @@ -193,6 +191,26 @@ public extension View { } +private struct SafariViewConfigurationModifier: ViewModifier { + + // MARK: - Initializers + + init(configuration: SafariView.Configuration) { + self.configuration = configuration + } + + @ViewBuilder + func body(content: Content) -> some View { + content + .environment(\.safariViewConfiguration, configuration) + } + + // MARK: - Private + + private let configuration: SafariView.Configuration + +} + private struct SafariViewControlTintColorModifier: ViewModifier { // MARK: - Initializers diff --git a/Sources/SafariView/SafariView.docc/Environment.md b/Sources/SafariView/SafariView.docc/Environment.md index b1e679902..488945159 100644 --- a/Sources/SafariView/SafariView.docc/Environment.md +++ b/Sources/SafariView/SafariView.docc/Environment.md @@ -6,5 +6,6 @@ Environment Values used by Safari View ### Values +- ``safariViewConfiguration`` - ``safariViewIncludedActivities`` - ``safariViewExcludedActivityTypes`` diff --git a/Sources/SafariView/SafariView.docc/Modifiers.md b/Sources/SafariView/SafariView.docc/Modifiers.md index 38481e9d7..f0e28418f 100644 --- a/Sources/SafariView/SafariView.docc/Modifiers.md +++ b/Sources/SafariView/SafariView.docc/Modifiers.md @@ -4,6 +4,10 @@ View modifiers used to configure and present Safari Views. ## Topics +### Configuration + +- ``safariConfiguration(_:)`` + ### Appearance - ``safariBarTintColor(_:)`` @@ -16,6 +20,7 @@ View modifiers used to configure and present Safari Views. - ``safari(url:onDismiss:safariView:)`` - ``safari(item:onDismiss:safariView:)`` - ``safari(item:id:onDismiss:safariView:)`` +- ``safari(url:)`` ### Activities diff --git a/Sources/SafariView/SafariView.docc/View.md b/Sources/SafariView/SafariView.docc/View.md index 8e2ca4030..3be7d10ff 100644 --- a/Sources/SafariView/SafariView.docc/View.md +++ b/Sources/SafariView/SafariView.docc/View.md @@ -10,7 +10,7 @@ A SafariView is best presented by using one the `safari` view modifiers, or via ### Creating a Safari View -- ``init(url:configuration:onInitialLoad:onInitialRedirect:onOpenInBrowser:)`` +- ``init(url:onInitialLoad:onInitialRedirect:onOpenInBrowser:)`` ### Configuring the View diff --git a/Sources/SafariView/SafariView.swift b/Sources/SafariView/SafariView.swift index a864b586f..5da32cfd4 100644 --- a/Sources/SafariView/SafariView.swift +++ b/Sources/SafariView/SafariView.swift @@ -36,19 +36,16 @@ public struct SafariView: View { /// Create a SafariView /// - Parameters: /// - url: URL to load - /// - configuration: The configuration for the new view. /// - onInitialLoad: Closure to execute on initial load /// - onInitialRedirect: Closure to execute on intial redirect /// - onOpenInBrowser: Closure to execute if a user moves from a SafariView to Safari.app public init( url: URL, - configuration: Configuration = .init(), onInitialLoad: ((_ didLoadSuccessfully: Bool) -> Void)? = nil, onInitialRedirect: ((_ url: URL) -> Void)? = nil, onOpenInBrowser: (() -> Void)? = nil ) { self.url = url - self.configuration = configuration self.onInitialLoad = onInitialLoad self.onInitialRedirect = onInitialRedirect self.onOpenInBrowser = onOpenInBrowser @@ -106,6 +103,9 @@ public struct SafariView: View { // MARK: - Private + @Environment(\.safariViewConfiguration) + private var configuration: Configuration + @Environment(\.safariViewBarTintColor) private var barTintColor: Color? @@ -122,7 +122,6 @@ public struct SafariView: View { private var excludedActivityTypes: ExcludedActivityTypes private let url: URL - private let configuration: Configuration private let onInitialLoad: ((Bool) -> Void)? private let onInitialRedirect: ((URL) -> Void)? private let onOpenInBrowser: (() -> Void)?