diff options
Diffstat (limited to 'Sources/WireguardAppIntents/BuildPeerConfigurationUpdate.swift')
-rw-r--r-- | Sources/WireguardAppIntents/BuildPeerConfigurationUpdate.swift | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/Sources/WireguardAppIntents/BuildPeerConfigurationUpdate.swift b/Sources/WireguardAppIntents/BuildPeerConfigurationUpdate.swift new file mode 100644 index 0000000..d6c0032 --- /dev/null +++ b/Sources/WireguardAppIntents/BuildPeerConfigurationUpdate.swift @@ -0,0 +1,77 @@ +// SPDX-License-Identifier: MIT +// Copyright © 2018-2021 WireGuard LLC. All Rights Reserved. + +import AppIntents + +let kEndpointConfigurationUpdateDictionaryKey = "Endpoint" + +@available(iOS 16.0, macOS 13.0, watchOS 9.0, tvOS 16.0, *) +struct BuildPeerConfigurationUpdate: AppIntent { + + static var title = LocalizedStringResource("buildPeerConfigurationUpdateIntentName", table: "AppIntents") + static var description = IntentDescription( + LocalizedStringResource("buildPeerConfigurationUpdateIntentDescription", table: "AppIntents") + ) + + @Parameter( + title: LocalizedStringResource("buildPeerConfigurationUpdateIntentPublicKeyParameterTitle", table: "AppIntents") + ) + var publicKey: String + + @Parameter( + title: LocalizedStringResource("buildPeerConfigurationUpdateIntentEndpointParameterTitle", table: "AppIntents") + ) + var endpoint: String + + func perform() async throws -> some IntentResult { + let peerConfigurationUpdate = AppIntentsPeer() + peerConfigurationUpdate.publicKey = publicKey + peerConfigurationUpdate.endpoint = endpoint + + return .result(value: peerConfigurationUpdate) + } + + static var parameterSummary: some ParameterSummary { + Summary("buildPeerConfigurationUpdateIntentSummary \(\.$publicKey)", table: "AppIntents") { + \.$endpoint + } + } +} + +@available(iOS 16.0, macOS 13.0, watchOS 9.0, tvOS 16.0, *) +struct AppIntentsPeer: TransientAppEntity { + + static var typeDisplayRepresentation = TypeDisplayRepresentation( + name: LocalizedStringResource("peerConfigurationUpdateEntityName", table: "AppIntents") + ) + + @Property( + title: LocalizedStringResource("peerConfigurationUpdateEntityPropertyPublicKeyTitle", table: "AppIntents") + ) + var publicKey: String + + @Property( + title: LocalizedStringResource("peerConfigurationUpdateEntityPropertyEndpointTitle", table: "AppIntents") + ) + var endpoint: String? + + var displayRepresentation: DisplayRepresentation { + var dictionary: [String: [String: String]] = [:] + dictionary[publicKey] = [:] + + if let endpoint { + dictionary[publicKey]?.updateValue(endpoint, forKey: kEndpointConfigurationUpdateDictionaryKey) + } + + let jsonData: Data + do { + jsonData = try JSONSerialization.data(withJSONObject: dictionary) + } catch { + return DisplayRepresentation(stringLiteral: error.localizedDescription) + } + + let jsonString = String(data: jsonData, encoding: .utf8)! + + return DisplayRepresentation(stringLiteral: jsonString) + } +} |