Skip to content

Commit

Permalink
Remove configuration struct
Browse files Browse the repository at this point in the history
  • Loading branch information
vsanthanam committed Jul 16, 2023
1 parent f7b5ac9 commit 7bc764e
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 159 deletions.
99 changes: 0 additions & 99 deletions Sources/SafariView/Configuration.swift

This file was deleted.

43 changes: 23 additions & 20 deletions Sources/SafariView/Environment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,6 @@ 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 Down Expand Up @@ -83,6 +66,16 @@ public extension EnvironmentValues {

extension EnvironmentValues {

var safariViewEntersReaderIfAvailable: Bool {
get { self[SafariViewEntersReaderIfAvailableEnvironmentKey.self] }
set { self[SafariViewEntersReaderIfAvailableEnvironmentKey.self] = newValue }
}

var safariViewBarCollapsingEnabled: Bool {
get { self[SafariViewBarCollapsingEnabledEnvironmentKey.self] }
set { self[SafariViewBarCollapsingEnabledEnvironmentKey.self] = newValue }
}

var safariViewControlTintColor: Color {
get { self[SafariViewControlTintColorEnvironmentKey.self] }
set { self[SafariViewControlTintColorEnvironmentKey.self] = newValue }
Expand All @@ -100,13 +93,23 @@ extension EnvironmentValues {

}

private struct SafariViewConfigurationEnvironmentKey: EnvironmentKey {
private struct SafariViewEntersReaderIfAvailableEnvironmentKey: EnvironmentKey {

// MARK: - EnvironmentKey

typealias Value = Bool

static let defaultValue: Value = false

}

private struct SafariViewBarCollapsingEnabledEnvironmentKey: EnvironmentKey {

// MARK: - EnvironmentKey

typealias Value = SafariView.Configuration
typealias Value = Bool

static var defaultValue: SafariView.Configuration { .init() }
static let defaultValue: Value = false

}

Expand Down
46 changes: 35 additions & 11 deletions Sources/SafariView/Modifiers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ import SwiftUI
@available(iOS 15.0, macCatalyst 15.0, *)
public extension View {

/// Set the configuration of safari views within this view
///
/// - Parameter configuration: The configuration to use
/// - Returns: The modified view
func safariConfiguration(_ configuration: SafariView.Configuration) -> some View {
let modifier = SafariViewConfigurationModifier(configuration: configuration)
func safariEntersReaderIfAvailable(_ entersReaderIfAvailable: Bool = true) -> some View {
let modifier = SafariViewEntersReaderIfAvailableModifier(entersReaderIfAvailable: entersReaderIfAvailable)
return ModifiedContent(content: self, modifier: modifier)
}

func safariBarCollapsingEnabled(_ barCollapsingEnabled: Bool = true) -> some View {
let modifier = SafariViewBarCollapsingEnabledModifier(barCollapsingEnabled: barCollapsingEnabled)
return ModifiedContent(content: self, modifier: modifier)
}

Expand Down Expand Up @@ -191,24 +192,47 @@ public extension View {

}

private struct SafariViewConfigurationModifier: ViewModifier {
private struct SafariViewEntersReaderIfAvailableModifier: ViewModifier {

// MARK: - Initializers

init(configuration: SafariView.Configuration) {
self.configuration = configuration
init(entersReaderIfAvailable: Bool) {
self.entersReaderIfAvailable = entersReaderIfAvailable
}

// MARK: - ViewModifier

@ViewBuilder
func body(content: Content) -> some View {
content
.environment(\.safariViewConfiguration, configuration)
.environment(\.safariViewEntersReaderIfAvailable, entersReaderIfAvailable)
}

// MARK: - Private

private let configuration: SafariView.Configuration
private let entersReaderIfAvailable: Bool

}

private struct SafariViewBarCollapsingEnabledModifier: ViewModifier {

// MARK: - Initializers

init(barCollapsingEnabled: Bool) {
self.barCollapsingEnabled = barCollapsingEnabled
}

// MARK: - ViewModifier

@ViewBuilder
func body(content: Content) -> some View {
content
.environment(\.safariViewBarCollapsingEnabled, barCollapsingEnabled)
}

// MARK: - Private

private let barCollapsingEnabled: Bool
}

private struct SafariViewControlTintColorModifier: ViewModifier {
Expand Down
10 changes: 5 additions & 5 deletions Sources/SafariView/Presentation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,11 @@ public extension View {
/// }
/// ```
///
/// - Parameter url: A binding to an optional source of truth for the ``SafariView``. When the URL is non-nil, the system passes the URL to the modifier’s closure. You display this content in a ``SafariView`` that you create that the system displays to the user. If the URL changes, the system dismisses the ``SafariView`` and replaces it with a new one using the same process.
/// - Parameters:
/// - url: A binding to an optional source of truth for the ``SafariView``. When the URL is non-nil, the system passes the URL to the modifier’s closure. You display this content in a ``SafariView`` that you create that the system displays to the user. If the URL changes, the system dismisses the ``SafariView`` and replaces it with a new one using the same process.
/// - onDismiss: The closure to execute when dismissing the ``SafariView``
/// - Returns: The modified view
func safari(url: Binding<URL?>) -> some View {
safari(item: url, id: \.hashValue) { url in
SafariView(url: url)
}
func safari(url: Binding<URL?>, onDismiss: (() -> Void)? = nil) -> some View {
safari(url: url, onDismiss: onDismiss) { url in SafariView(url: url) }
}
}
1 change: 0 additions & 1 deletion Sources/SafariView/SafariView.docc/Environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ Environment Values used by Safari View

### Values

- ``safariViewConfiguration``
- ``safariViewIncludedActivities``
- ``safariViewExcludedActivityTypes``
3 changes: 2 additions & 1 deletion Sources/SafariView/SafariView.docc/Modifiers.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ View modifiers used to configure and present Safari Views.

### Configuration

- ``safariConfiguration(_:)``
- ``safariEntersReaderIfAvailable(_:)``
- ``safariBarCollapsingEnabled(_:)``

### Appearance

Expand Down
4 changes: 2 additions & 2 deletions Sources/SafariView/SafariView.docc/View.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ A SafariView is best presented by using one the `safari` view modifiers, or via

### Creating a Safari View

- ``init(url:onInitialLoad:onInitialRedirect:onOpenInBrowser:)``
- ``init(url:activityButton:onInitialLoad:onInitialRedirect:onOpenInBrowser:)``
- ``init(url:activityButton:eventAttribution:onInitialLoad:onInitialRedirect:onOpenInBrowser:)``

### Configuring the View

- ``Configuration``
- ``ActivityButton``
- ``DismissButtonStyle``

Expand Down
Loading

0 comments on commit 7bc764e

Please sign in to comment.