Skip to content

Commit

Permalink
Move configuration to environment & create view modifier (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
vsanthanam committed Jul 15, 2023
1 parent e0ff79a commit f7b5ac9
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 19 deletions.
19 changes: 18 additions & 1 deletion Sources/SafariView/Environment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -39,7 +56,7 @@ public extension EnvironmentValues {
/// var safariViewIncludedActivities
///
/// }
/// `
/// ```
var safariViewIncludedActivities: SafariView.IncludedActivities {
get { self[SafariViewIncludedActivitiesEnvironmentKey.self] }
set { self[SafariViewIncludedActivitiesEnvironmentKey.self] = newValue }
Expand Down
44 changes: 31 additions & 13 deletions Sources/SafariView/Modifiers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions Sources/SafariView/SafariView.docc/Environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ Environment Values used by Safari View

### Values

- ``safariViewConfiguration``
- ``safariViewIncludedActivities``
- ``safariViewExcludedActivityTypes``
5 changes: 5 additions & 0 deletions Sources/SafariView/SafariView.docc/Modifiers.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ View modifiers used to configure and present Safari Views.

## Topics

### Configuration

- ``safariConfiguration(_:)``

### Appearance

- ``safariBarTintColor(_:)``
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion Sources/SafariView/SafariView.docc/View.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 3 additions & 4 deletions Sources/SafariView/SafariView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -106,6 +103,9 @@ public struct SafariView: View {

// MARK: - Private

@Environment(\.safariViewConfiguration)
private var configuration: Configuration

@Environment(\.safariViewBarTintColor)
private var barTintColor: Color?

Expand All @@ -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)?
Expand Down

0 comments on commit f7b5ac9

Please sign in to comment.