Skip to content

Commit

Permalink
Add new VPN intents.
Browse files Browse the repository at this point in the history
  • Loading branch information
samsymons committed Jul 21, 2024
1 parent a29a5fd commit 2d10357
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DuckDuckGo/NetworkProtectionStatusView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct NetworkProtectionStatusView: View {
toggle()
locationDetails()

if statusModel.isNetPEnabled && statusModel.hasServerInfo && !statusModel.isSnoozing && statusModel.ipAddress != nil {
if statusModel.isNetPEnabled && statusModel.hasServerInfo && !statusModel.isSnoozing {
connectionDetails()
}

Expand Down
64 changes: 64 additions & 0 deletions DuckDuckGo/VPNIntents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@

import AppIntents
import NetworkExtension
import NetworkProtection
import WidgetKit
import Core

// MARK: - Enable & Disable

@available(iOS 17.0, *)
struct DisableVPNIntent: AppIntent {

Expand Down Expand Up @@ -109,3 +112,64 @@ struct EnableVPNIntent: AppIntent {
}

}

// MARK: - Snooze

@available(iOS 17.0, *)
struct StartSnoozeVPNIntent: AppIntent {

static let title: LocalizedStringResource = "Snooze VPN"
static let description: LocalizedStringResource = "Snoozes the DuckDuckGo VPN"
static let openAppWhenRun: Bool = false
static let isDiscoverable: Bool = false

@MainActor
func perform() async throws -> some IntentResult {
do {
let managers = try await NETunnelProviderManager.loadAllFromPreferences()

guard let manager = managers.first, let session = manager.connection as? NETunnelProviderSession else {
return .result()
}

let defaultDuration: TimeInterval = .minutes(1) + .seconds(1) // TODO: Change to 20 mins, 1 min is only used for testing
try? await session.sendProviderMessage(.startSnooze(defaultDuration))

WidgetCenter.shared.reloadTimelines(ofKind: "VPNStatusWidget")

return .result()
} catch {
return .result()
}
}

}

@available(iOS 17.0, *)
struct CancelSnoozeVPNIntent: AppIntent {

static let title: LocalizedStringResource = "Snooze VPN"
static let description: LocalizedStringResource = "Snoozes the DuckDuckGo VPN"
static let openAppWhenRun: Bool = false
static let isDiscoverable: Bool = false

@MainActor
func perform() async throws -> some IntentResult {
do {
let managers = try await NETunnelProviderManager.loadAllFromPreferences()

guard let manager = managers.first, let session = manager.connection as? NETunnelProviderSession else {
return .result()
}

try? await session.sendProviderMessage(.cancelSnooze)

WidgetCenter.shared.reloadTimelines(ofKind: "VPNStatusWidget")

return .result()
} catch {
return .result()
}
}

}

0 comments on commit 2d10357

Please sign in to comment.