Skip to content

Commit 9077988

Browse files
tgrapperonp4checo
authored andcommitted
Base OpenURL dependency on SwiftUI (#1714)
* Base `OpenURL` dependency on SwiftUI * indent (cherry picked from commit fbd70bf4d01284a9788d88cba4b6c70827f104e4)
1 parent 2642d93 commit 9077988

File tree

1 file changed

+8
-29
lines changed

1 file changed

+8
-29
lines changed

Sources/Dependencies/Dependencies/OpenURL.swift

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,31 @@
11
import Foundation
22
import XCTestDynamicOverlay
3-
4-
#if canImport(AppKit)
5-
import AppKit
6-
#endif
7-
#if canImport(UIKit)
8-
import UIKit
9-
#endif
103
#if canImport(SwiftUI)
114
import SwiftUI
12-
#endif
135

14-
#if canImport(AppKit) || canImport(UIKit) || canImport(SwiftUI)
156
extension DependencyValues {
167
/// A dependency that opens a URL.
17-
@available(iOS 13, macOS 10.15, tvOS 13, watchOS 7, *)
8+
@available(iOS 14, macOS 11, tvOS 14, watchOS 7, *)
189
public var openURL: OpenURLEffect {
1910
get { self[OpenURLKey.self] }
2011
set { self[OpenURLKey.self] = newValue }
2112
}
2213
}
2314

15+
@available(iOS 14, macOS 11, tvOS 14, watchOS 7, *)
2416
private enum OpenURLKey: DependencyKey {
2517
static let liveValue = OpenURLEffect { url in
2618
let stream = AsyncStream<Bool> { continuation in
2719
let task = Task { @MainActor in
28-
#if canImport(AppKit) && !targetEnvironment(macCatalyst)
29-
NSWorkspace.shared.open(url, configuration: .init()) { app, error in
30-
continuation.yield(app != nil && error == nil)
31-
continuation.finish()
32-
}
33-
#elseif canImport(UIKit) && !os(watchOS)
34-
UIApplication.shared.open(url) { canOpen in
20+
#if os(watchOS)
21+
EnvironmentValues().openURL(url)
22+
continuation.yield(true)
23+
continuation.finish()
24+
#else
25+
EnvironmentValues().openURL(url) { canOpen in
3526
continuation.yield(canOpen)
3627
continuation.finish()
3728
}
38-
#elseif canImport(SwiftUI)
39-
if #available(watchOS 7, *) {
40-
EnvironmentValues().openURL(url)
41-
continuation.yield(true)
42-
continuation.finish()
43-
} else {
44-
continuation.yield(false)
45-
continuation.finish()
46-
}
47-
#else
48-
continuation.yield(false)
49-
continuation.finish()
5029
#endif
5130
}
5231
continuation.onTermination = { @Sendable _ in

0 commit comments

Comments
 (0)