aboutsummaryrefslogtreecommitdiffstats
path: root/Sources/WireguardAppIntents/BuildPeerConfigurationUpdate.swift
blob: 090e52a5aa89b7d0cc0890acbc026e4b7a077567 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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 & ReturnsValue<AppIntentsPeer> {
        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)
    }
}