Skip to content

Commit

Permalink
Move configuration and styles to swiftui environment
Browse files Browse the repository at this point in the history
  • Loading branch information
vsanthanam committed Jul 8, 2023
1 parent fcaae9c commit 3cad0ce
Show file tree
Hide file tree
Showing 6 changed files with 639 additions and 541 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import PackageDescription
let package = Package(
name: "SafariView",
platforms: [
.iOS(.v15)
.iOS(.v16)
],
products: [
.library(
Expand Down
127 changes: 127 additions & 0 deletions Sources/SafariView/Environment.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// SafariView
// Environment.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 SwiftUI

public extension EnvironmentValues {

var safariViewConfiguration: SafariView.Configuration {
get { self[SafariViewConfigurationEnvironmentKey.self] }
set { self[SafariViewConfigurationEnvironmentKey.self] = newValue }
}

/// Apply a control tint color to the view
///
/// This property is equivelent to `SFSafariViewController`'s `.preferredControlTintColor` property
var safariViewControlTintColor: Color {
get { self[SafariViewControlTintColorEnvironmentKey.self] }
set { self[SafariViewControlTintColorEnvironmentKey.self] = newValue }
}

/// Apply an bar tint color to the view
///
/// This property is equivelent to `SFSafariViewController`'s `.preferredBarTintColor` property
var safariViewBarTintColor: Color? {
get { self[SafariViewBarTintColorEnvironmentKey.self] }
set { self[SafariViewBarTintColorEnvironmentKey.self] = newValue }
}

/// Set the safari view's dismiss button style
///
/// This property is equivelent to `SFSafariViewController`'s `.dismissButtonStyle` property
var safariViewDismissButtonStyle: SafariView.DismissButtonStyle {
get { self[SafariViewDismissButtonStyleEnvironmentKey.self] }
set { self[SafariViewDismissButtonStyleEnvironmentKey.self] = newValue }
}

var safariViewIncludedActivities: SafariView.IncludedActivities {
get { self[SafariViewIncludedActivitiesEnvironmentKey.self] }
set { self[SafariViewIncludedActivitiesEnvironmentKey.self] = newValue }
}

var safariViewExcludedActivityTypes: SafariView.ExcludedActivityTypes {
get { self[SafariViewExcludedActivityTypesEnvironmentKey.self] }
set { self[SafariViewExcludedActivityTypesEnvironmentKey.self] = newValue }
}

}

private struct SafariViewConfigurationEnvironmentKey: EnvironmentKey {

typealias Value = SafariView.Configuration

static var defaultValue: SafariView.Configuration { .init() }

}

private struct SafariViewControlTintColorEnvironmentKey: EnvironmentKey {

// MARK: - EnvironmentKey

typealias Value = Color

static var defaultValue: Value { .accentColor }

}

private struct SafariViewBarTintColorEnvironmentKey: EnvironmentKey {

// MARK: - EnvironmentKey

typealias Value = Color?

static var defaultValue: Value { nil }

}

private struct SafariViewDismissButtonStyleEnvironmentKey: EnvironmentKey {

// MARK: - EnvironmentKey

typealias Value = SafariView.DismissButtonStyle

static var defaultValue: Value { .close }

}

private struct SafariViewIncludedActivitiesEnvironmentKey: EnvironmentKey {

// MARK: - EnvironmentKey

typealias Value = SafariView.IncludedActivities

static let defaultValue: Value = .default

}

private struct SafariViewExcludedActivityTypesEnvironmentKey: EnvironmentKey {

// MARK: - EnvironmentKey

typealias Value = SafariView.ExcludedActivityTypes

static let defaultValue: Value = .default

}
222 changes: 222 additions & 0 deletions Sources/SafariView/Modifiers.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
// SafariView
// Modifiers.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 SwiftUI

public extension View {

func safariConfiguration(_ configuration: SafariView.Configuration) -> some View {
let modifier = SafariViewConfigurationModifier(configuration: configuration)
return ModifiedContent(content: self, modifier: modifier)
}

func safariBarTintColor(_ color: Color?) -> some View {
let modifier = SafariViewBarTintColorModifier(safariViewBarTintColor: color)
return ModifiedContent(content: self, modifier: modifier)
}

func safariControlTintColor(_ color: Color) -> some View {
let modifier = SafariViewControlTintColorModifier(safariViewControlTintColor: color)
return ModifiedContent(content: self, modifier: modifier)
}

/// Set the safari view's dismiss button style
///
/// This property is equivelent to `SFSafariViewController`'s `.dismissButtonStyle` property
///
/// - Parameter dismissButtonStyle: The desired dismiss button style
/// - Returns: The modified content
func safariDismissButtonStyle(_ dismissButtonStyle: SafariView.DismissButtonStyle) -> some View {
let modifier = SafariViewDismissButtonStyleModifier(safariViewDismissButtonStyle: dismissButtonStyle)
return ModifiedContent(content: self, modifier: modifier)
}

func includedSafariActivities(_ includedActivities: SafariView.IncludedActivities) -> some View {
let modifier = SafariViewIncludedActivitiesModifier(includedActivities: includedActivities)
return ModifiedContent(content: self, modifier: modifier)
}

func includedSafariActivities(_ includedActivities: @escaping (_ url: URL, _ pageTitle: String?) -> [UIActivity]) -> some View {
let activities = SafariView.IncludedActivities(includedActivities)
let modifier = SafariViewIncludedActivitiesModifier(includedActivities: activities)
return ModifiedContent(content: self, modifier: modifier)
}

func includedSafariActivities(_ includedActivities: [UIActivity]) -> some View {
let activities = SafariView.IncludedActivities { _, _ in includedActivities }
let modifier = SafariViewIncludedActivitiesModifier(includedActivities: activities)
return ModifiedContent(content: self, modifier: modifier)
}

func excludedSafariActivityTypes(_ excludedActivityTypes: SafariView.ExcludedActivityTypes) -> some View {
let modifier = SafariViewExcludedActivityTypesModifier(excludedActivityTypes: excludedActivityTypes)
return ModifiedContent(content: self, modifier: modifier)
}

func excludedSafariActivityTypes(_ excludedActivityTypes: @escaping (_ url: URL, _ pageTitle: String?) -> [UIActivity.ActivityType]) -> some View {
let activityTypes = SafariView.ExcludedActivityTypes(excludedActivityTypes)
let modifier = SafariViewExcludedActivityTypesModifier(excludedActivityTypes: activityTypes)
return ModifiedContent(content: self, modifier: modifier)
}

func excludedSafariActivityTypes(_ excludedActivityTypes: [UIActivity.ActivityType]) -> some View {
let activityTypes = SafariView.ExcludedActivityTypes { _, _ in excludedActivityTypes }
let modifier = SafariViewExcludedActivityTypesModifier(excludedActivityTypes: activityTypes)
return ModifiedContent(content: self, modifier: modifier)
}

}

private struct SafariViewConfigurationModifier: ViewModifier {

// MARK: - Initializers

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

// MARK: - ViewModifier

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

// MARK: - Private

private let configuration: SafariView.Configuration

}

private struct SafariViewControlTintColorModifier: ViewModifier {

// MARK: - Initializers

init(safariViewControlTintColor: Color) {
self.safariViewControlTintColor = safariViewControlTintColor
}

// MARK: - ViewModifier

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

// MARK: - Private

private let safariViewControlTintColor: Color

}

private struct SafariViewBarTintColorModifier: ViewModifier {

// MARK: - Initializers

init(safariViewBarTintColor: Color?) {
self.safariViewBarTintColor = safariViewBarTintColor
}

// MARK: - ViewModifier

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

// MARK: - Private

private let safariViewBarTintColor: Color?

}

private struct SafariViewDismissButtonStyleModifier: ViewModifier {

// MARK: - Initializers

init(safariViewDismissButtonStyle: SafariView.DismissButtonStyle) {
self.safariViewDismissButtonStyle = safariViewDismissButtonStyle
}

// MARK: - ViewModifier

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

// MARK: - Private

private let safariViewDismissButtonStyle: SafariView.DismissButtonStyle

}

private struct SafariViewIncludedActivitiesModifier: ViewModifier {

// MARK: - Initializers

init(includedActivities: SafariView.IncludedActivities) {
self.includedActivities = includedActivities
}

// MARK: - ViewModifier

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

// MARK: - Private

private let includedActivities: SafariView.IncludedActivities

}

private struct SafariViewExcludedActivityTypesModifier: ViewModifier {

// MARK: - Initializers

init(excludedActivityTypes: SafariView.ExcludedActivityTypes) {
self.excludedActivityTypes = excludedActivityTypes
}

// MARK: - ViewBuilder

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

// MARK: - Private

private let excludedActivityTypes: SafariView.ExcludedActivityTypes

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SafariView
// ViewExtensions.swift
// Presentation.swift
//
// MIT License
//
Expand Down Expand Up @@ -134,9 +134,11 @@ public extension View {
onDismiss: (() -> Void)? = nil,
@ViewBuilder safariView: @escaping (Item) -> SafariView
) -> some View where Item: Identifiable {
let modifier = SafariView.ItemModitifer(item: item,
build: safariView,
onDismiss: onDismiss ?? {})
let modifier = SafariView.IdentifiableItemModitifer(
item: item,
build: safariView,
onDismiss: onDismiss ?? {}
)
return ModifiedContent(content: self, modifier: modifier)
}

Expand Down Expand Up @@ -199,7 +201,12 @@ public extension View {
onDismiss: (() -> Void)? = nil,
@ViewBuilder safariView: @escaping (Item) -> SafariView
) -> some View {
let modifier = SafariView.GenericItemModifier(item: item, id: id, onDismiss: onDismiss, safariView: safariView)
let modifier = SafariView.ItemModifier(
item: item,
id: id,
onDismiss: onDismiss,
safariView: safariView
)
return ModifiedContent(content: self, modifier: modifier)
}

Expand Down
Loading

0 comments on commit 3cad0ce

Please sign in to comment.