Skip to content

Commit

Permalink
Complete the missing part of Link support
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle-Ye committed Nov 29, 2023
1 parent fb29a73 commit a254960
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,32 @@ extension EnvironmentValues {
/// For example, a view that uses the action declared above
/// receives `true` when calling the action, because the
/// handler always returns ``OpenURLAction/Result/handled``.
// MARK: - TODO
public var openURL: OpenURLAction {
get { _openURL }
set { _openURL = newValue }
}

public var _openURL: OpenURLAction {
get { self[OpenURLActionKey.self] }
get {
let action = self[OpenURLActionKey.self]
let hostingAction = self[HostingViewOpenURLActionKey.self]
guard let hostingAction else {
return action
}
guard !action.isDefault else {
return hostingAction
}
guard case let .custom(customResultBlock, _) = action.handler,
case let .system(systemHandler) = hostingAction.handler else {
return action
}
return OpenURLAction(
handler: .custom(customResultBlock, fallback: systemHandler),
isDefault: false
)
}
set { self[OpenURLActionKey.self] = newValue }
}
// MARK: TODO -

public var _openSensitiveURL: OpenURLAction {
get { self[OpenSensitiveURLActionKey.self] }
Expand All @@ -102,14 +117,14 @@ struct OpenURLActionKey: EnvironmentKey {
#if os(iOS) || os(tvOS)
UIApplication.shared.open(url, options: [:], completionHandler: completion)
#elseif os(macOS)
NSWorkspace.shared.open(url, configuration: .init()) { application, error in
NSWorkspace.shared.open(url, configuration: .init()) { _, error in
completion(error != nil)
}
#else
fatalError("Unimplemented")
#endif
},
isDefault: false
isDefault: true
)
}

Expand All @@ -129,13 +144,10 @@ struct OpenSensitiveURLActionKey: EnvironmentKey {
fatalError("Unimplemented")
#endif
},
isDefault: false
isDefault: true
)
}

struct HostingViewOpenURLActionKey: EnvironmentKey {
static let defaultValue = OpenURLAction(
handler: .custom({ .systemAction($0)}, fallback: nil),
isDefault: true
)
static let defaultValue: OpenURLAction? = nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ import Foundation
public struct OpenURLAction {
enum Handler {
case system((URL, @escaping (Bool) -> Void) -> Void)
case custom((URL) -> Result, fallback: ((URL, (Bool) -> Void) -> Void)?)
case custom((URL) -> Result, fallback: ((URL, @escaping (Bool) -> Void) -> Void)?)
}

let handler: Handler
Expand Down Expand Up @@ -257,15 +257,12 @@ extension OpenURLAction {
} else {
systemHandler(url, completion)
}
case let .custom(customHandler, fallback: fallbackHandler):
let result = customHandler(url)
case let .custom(customResultBlock, fallback: fallbackHandler):
let result = customResultBlock(url)
switch result.actionResult {
case let .systemAction(optionalURL):
if let fallbackHandler {
fallbackHandler(optionalURL ?? url, completion)
} else {
OpenURLActionKey.defaultValue._open(optionalURL ?? url, completion: completion)
}
let handler = fallbackHandler ?? OpenURLActionKey.defaultValue._open(_:completion:)
handler(optionalURL ?? url, completion)
case .handled:
completion(true)
case .discarded:
Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenSwiftUI/Views/Controls/Link/Link.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// OpenSwiftUI
//
// Created by Kyle on 2023/11/28.
// Lastest Version: iOS 15.0
// Lastest Version: iOS 15.5
// Status: Blocked by Text

import Foundation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// OpenSwiftUI
//
// Created by Kyle on 2023/11/28.
// Lastest Version: iOS 15.0
// Lastest Version: iOS 15.5
// Status: Complete
// ID: 85AAA54365DC4DDF1DC8F2FECEF4501A

Expand Down

0 comments on commit a254960

Please sign in to comment.