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 3, 2023
1 parent fcaae9c commit ad169e9
Show file tree
Hide file tree
Showing 5 changed files with 435 additions and 520 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
58 changes: 58 additions & 0 deletions Sources/SafariView/Environment.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// 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 }
}

var safariViewStyle: SafariView.Style {
get { self[SafariViewStyleEvironmentKey.self] }
set { self[SafariViewStyleEvironmentKey.self] = newValue }
}

}

struct SafariViewConfigurationEnvironmentKey: EnvironmentKey {

// MARK: - EnvironmentKey

typealias Value = SafariView.Configuration

static var defaultValue: Value { .init() }

}

private struct SafariViewStyleEvironmentKey: EnvironmentKey {

typealias Value = SafariView.Style

static var defaultValue: Value { .init() }

}
84 changes: 84 additions & 0 deletions Sources/SafariView/Modifiers.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// 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 safariStyle(_ style: SafariView.Style) -> some View {
let modifier = SafariViewStyleModifier(style: style)
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 SafariViewStyleModifier: ViewModifier {

// MARK: - Initializers

init(style: SafariView.Style) {
self.style = style
}

// MARK: - ViewModifier

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

// MARK: - Private

private let style: SafariView.Style

}
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 ad169e9

Please sign in to comment.