|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +@_exported import Intents |
| 14 | +import Foundation |
| 15 | + |
| 16 | +#if os(iOS) || os(watchOS) |
| 17 | + |
| 18 | +@available(iOS 12.0, watchOS 5.0, *) |
| 19 | +public enum INShortcut : ReferenceConvertible { |
| 20 | + public typealias ReferenceType = INShortcutReference |
| 21 | + |
| 22 | + case intent(INIntent) |
| 23 | + case userActivity(NSUserActivity) |
| 24 | + |
| 25 | + init(from objcShortcut: INShortcutReference) { |
| 26 | + if let intent = objcShortcut.intent { |
| 27 | + self = .intent(intent) |
| 28 | + } else if let userActivity = objcShortcut.userActivity { |
| 29 | + self = .userActivity(userActivity) |
| 30 | + } else { |
| 31 | + fatalError("INShortcutReference object must have either intent or userActivity") |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// Convenience initializers, to mimic the ObjC initializer API |
| 37 | +@available(iOS 12.0, watchOS 5.0, *) |
| 38 | +public extension INShortcut { |
| 39 | + public init?(intent: INIntent) { |
| 40 | + // use the ObjC initializer, to re-use its validation of the intent |
| 41 | + guard let ref = INShortcutReference(intent: intent) else { return nil } |
| 42 | + self.init(from: ref) |
| 43 | + } |
| 44 | + public init(userActivity: NSUserActivity) { |
| 45 | + self = .userActivity(userActivity) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// Convenience properties, to mimic the ObjC API |
| 50 | +@available(iOS 12.0, watchOS 5.0, *) |
| 51 | +public extension INShortcut { |
| 52 | + public var intent: INIntent? { |
| 53 | + guard case let .intent(intent) = self else { return nil } |
| 54 | + return intent |
| 55 | + } |
| 56 | + public var userActivity: NSUserActivity? { |
| 57 | + guard case let .userActivity(userActivity) = self else { return nil } |
| 58 | + return userActivity |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +@available(iOS 12.0, watchOS 5.0, *) |
| 63 | +extension INShortcut : CustomStringConvertible { |
| 64 | + public var description: String { |
| 65 | + return reference.description |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +@available(iOS 12.0, watchOS 5.0, *) |
| 70 | +extension INShortcut : CustomDebugStringConvertible { |
| 71 | + public var debugDescription: String { |
| 72 | + return reference.debugDescription |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +@available(iOS 12.0, watchOS 5.0, *) |
| 77 | +extension INShortcut : Hashable { |
| 78 | + public func hash(into hasher: inout Hasher) { |
| 79 | + reference.hash(into: &hasher) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +@available(iOS 12.0, watchOS 5.0, *) |
| 84 | +extension INShortcut : Equatable {} |
| 85 | + |
| 86 | +@available(iOS 12.0, watchOS 5.0, *) |
| 87 | +private extension INShortcut { |
| 88 | + fileprivate var reference: INShortcutReference { |
| 89 | + switch self { |
| 90 | + case .intent(let intent): |
| 91 | + return INShortcutReference(intent: intent)! |
| 92 | + case .userActivity(let userActivity): |
| 93 | + return INShortcutReference(userActivity: userActivity) |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +@available(iOS 12.0, watchOS 5.0, *) |
| 99 | +extension INShortcut : _ObjectiveCBridgeable { |
| 100 | + @_semantics("convertToObjectiveC") |
| 101 | + public func _bridgeToObjectiveC() -> INShortcutReference { |
| 102 | + return self.reference |
| 103 | + } |
| 104 | + |
| 105 | + public static func _forceBridgeFromObjectiveC(_ source: INShortcutReference, result: inout INShortcut?) { |
| 106 | + if !_conditionallyBridgeFromObjectiveC(source, result: &result) { |
| 107 | + fatalError("Unable to bridge \(_ObjectiveCType.self) to \(self)") |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + public static func _conditionallyBridgeFromObjectiveC(_ source: INShortcutReference, result: inout INShortcut?) -> Bool { |
| 112 | + result = INShortcut(from: source) |
| 113 | + return true |
| 114 | + } |
| 115 | + |
| 116 | + public static func _unconditionallyBridgeFromObjectiveC(_ source: INShortcutReference?) -> INShortcut { |
| 117 | + guard let src = source else { fatalError("Missing source") } |
| 118 | + return INShortcut(from: src) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +#endif |
0 commit comments