From f7b5ac9cdd6f143ae81a9c3909c0092f2f45139c Mon Sep 17 00:00:00 2001 From: Varun Santhanam Date: Sat, 15 Jul 2023 14:46:42 -0700 Subject: [PATCH] Move configuration to environment & create view modifier (#17) --- Sources/SafariView/Environment.swift | 19 +++++++- Sources/SafariView/Modifiers.swift | 44 +++++++++++++------ .../SafariView/SafariView.docc/Environment.md | 1 + .../SafariView/SafariView.docc/Modifiers.md | 5 +++ Sources/SafariView/SafariView.docc/View.md | 2 +- Sources/SafariView/SafariView.swift | 7 ++- 6 files changed, 59 insertions(+), 19 deletions(-) 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)?