Implement GetPeers AppIntent
Signed-off-by: Alessio Nossa <alessio.nossa@gmail.com>
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
import UIKit
|
||||
import os.log
|
||||
import Intents
|
||||
import AppIntents
|
||||
|
||||
@UIApplicationMain
|
||||
class AppDelegate: UIResponder, UIApplicationDelegate {
|
||||
@@ -47,6 +48,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
|
||||
|
||||
tunnelsManager.activationDelegate = self.mainVC
|
||||
|
||||
if #available(iOS 16.0, *) {
|
||||
AppDependencyManager.shared.add(dependency: tunnelsManager)
|
||||
}
|
||||
|
||||
NotificationCenter.default.post(name: AppDelegate.tunnelsManagerReadyNotificationName,
|
||||
object: self,
|
||||
userInfo: nil)
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// Copyright © 2018-2021 WireGuard LLC. All Rights Reserved.
|
||||
|
||||
// App Intents Common
|
||||
"wireguardAppIntentsWrongTunnelError %@" = "Cannot find a tunnel with the name \"%1$@\"";
|
||||
"wireguardAppIntentsMissingConfigurationError" = "The selected tunnel has no configuration.";
|
||||
|
||||
// Get peers Intent
|
||||
"getPeersIntentName" = "Get Peers";
|
||||
"getPeersIntentDescription" = "Get list of public keys of peers in the selected configuration";
|
||||
"getPeersIntentTunnelParameterTitle" = "Tunnel";
|
||||
"getPeersIntentSummary ${tunnelName}" = "Get peers of ${tunnelName}";
|
||||
@@ -0,0 +1,55 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// Copyright © 2018-2021 WireGuard LLC. All Rights Reserved.
|
||||
|
||||
import Foundation
|
||||
import AppIntents
|
||||
|
||||
@available(iOS 16.0, macOS 13.0, watchOS 9.0, tvOS 16.0, *)
|
||||
struct GetPeers: AppIntent {
|
||||
|
||||
static var title = LocalizedStringResource("getPeersIntentName", table: "AppIntents")
|
||||
static var description = IntentDescription(
|
||||
LocalizedStringResource("getPeersIntentDescription", table: "AppIntents")
|
||||
)
|
||||
|
||||
@Parameter(
|
||||
title: LocalizedStringResource("getPeersIntentTunnelParameterTitle", table: "AppIntents"),
|
||||
optionsProvider: TunnelsOptionsProvider()
|
||||
)
|
||||
var tunnelName: String
|
||||
|
||||
@Dependency
|
||||
var tunnelsManager: TunnelsManager
|
||||
|
||||
func perform() async throws -> some ReturnsValue {
|
||||
guard let tunnelContainer = tunnelsManager.tunnel(named: tunnelName) else {
|
||||
throw GetPeersIntentError.wrongTunnel(name: tunnelName)
|
||||
}
|
||||
|
||||
guard let tunnelConfiguration = tunnelContainer.tunnelConfiguration else {
|
||||
throw GetPeersIntentError.missingConfiguration
|
||||
}
|
||||
|
||||
let publicKeys = tunnelConfiguration.peers.map { $0.publicKey.base64Key }
|
||||
return .result(value: publicKeys)
|
||||
}
|
||||
|
||||
static var parameterSummary: some ParameterSummary {
|
||||
Summary("getPeersIntentSummary \(\.$tunnelName)", table: "AppIntents")
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 16.0, macOS 13.0, watchOS 9.0, tvOS 16.0, *)
|
||||
enum GetPeersIntentError: Swift.Error, CustomLocalizedStringResourceConvertible {
|
||||
case wrongTunnel(name: String)
|
||||
case missingConfiguration
|
||||
|
||||
var localizedStringResource: LocalizedStringResource {
|
||||
switch self {
|
||||
case .wrongTunnel(let name):
|
||||
return LocalizedStringResource("wireguardAppIntentsWrongTunnelError \(name)", table: "AppIntents")
|
||||
case .missingConfiguration:
|
||||
return LocalizedStringResource("wireguardAppIntentsMissingConfigurationError", table: "AppIntents")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// Copyright © 2018-2021 WireGuard LLC. All Rights Reserved.
|
||||
|
||||
import AppIntents
|
||||
|
||||
@available(iOS 16.0, macOS 13.0, watchOS 9.0, tvOS 16.0, *)
|
||||
struct TunnelsOptionsProvider: DynamicOptionsProvider {
|
||||
@Dependency
|
||||
var tunnelsManager: TunnelsManager
|
||||
|
||||
func results() async throws -> [String] {
|
||||
let tunnelsNames = tunnelsManager.mapTunnels { $0.name }
|
||||
return tunnelsNames
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user