-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move configuration and styles to swiftui environment
- Loading branch information
1 parent
fcaae9c
commit 3cad0ce
Showing
6 changed files
with
639 additions
and
541 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.