Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
diegoreymendez committed Dec 17, 2024
1 parent b798fb0 commit 348021b
Show file tree
Hide file tree
Showing 22 changed files with 595 additions and 450 deletions.
74 changes: 49 additions & 25 deletions DuckDuckGo.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

12 changes: 10 additions & 2 deletions DuckDuckGo/VPNAutoShortcuts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,21 @@

import AppIntents
import Foundation
import VPNAppIntents
/*
@available(iOS 17.0, *)
public struct VPNAppIntents: AppIntentsPackage {
public static var includedPackages: [any AppIntentsPackage.Type] {
[VPNAppIntents.self]
}
}*/

@available(iOS 17.0, *)
struct VPNAutoShortcutsiOS17: AppShortcutsProvider {

@AppShortcutsBuilder
static var appShortcuts: [AppShortcut] {
AppShortcut(intent: EnableVPNAppIntent(),
AppShortcut(intent: EnableVPNIntent(),
phrases: [
"Connect \(.applicationName) VPN",
"Connect the \(.applicationName) VPN",
Expand All @@ -42,7 +50,7 @@ struct VPNAutoShortcutsiOS17: AppShortcutsProvider {
"Protect my connection with \(.applicationName)"
],
systemImageName: "globe")
AppShortcut(intent: DisableVPNAppIntent(),
AppShortcut(intent: DisableVPNIntent(),
phrases: [
"Disconnect \(.applicationName) VPN",
"Disconnect the \(.applicationName) VPN",
Expand Down
63 changes: 0 additions & 63 deletions DuckDuckGo/VPNIntentTunnelController.swift

This file was deleted.

23 changes: 15 additions & 8 deletions DuckDuckGo/VPNAppIntents.swift → DuckDuckGo/VPNIntents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,18 @@ import NetworkExtension
import NetworkProtection
import WidgetKit
import Core
import VPNWidgetSupport

// MARK: - Enable & Disable

/// App intent to disable the VPN
///
/// This is used in App Shortcuts, for things like Shortcuts.app, Spotlight and Siri.
/// This is very similar to ``WidgetVPNDisableIntent``, but this runs in-app, allows continuation in the app if needed,
/// This is very similar to ``WidgetDisableVPNIntent``, but this runs in-app, allows continuation in the app if needed,
/// and provides a result dialog.
///
@available(iOS 17.0, *)
struct DisableVPNAppIntent: AppIntent {
struct DisableVPNIntent: AppIntent {

private enum DisableAttemptFailure: CustomNSError {
case cancelled
Expand All @@ -49,12 +50,15 @@ struct DisableVPNAppIntent: AppIntent {
do {
DailyPixel.fireDailyAndCount(pixel: .networkProtectionWidgetDisconnectAttempt)

let controller = VPNIntentTunnelController()
let controller = VPNWidgetTunnelController()
try await controller.stop()

await VPNSnoozeLiveActivityManager().endSnoozeActivity()
VPNReloadStatusWidgets()

DailyPixel.fireDailyAndCount(pixel: .networkProtectionWidgetDisconnectSuccess)
return .result(dialog: "DuckDuckGo VPN is disconnecting...")
} catch VPNIntentTunnelController.StopFailure.vpnNotConfigured {
} catch VPNWidgetTunnelController.StopFailure.vpnNotConfigured {
DailyPixel.fireDailyAndCount(pixel: .networkProtectionWidgetDisconnectCancelled)
return .result(dialog: "The DuckDuckGo VPN is not connected")
} catch {
Expand All @@ -67,12 +71,12 @@ struct DisableVPNAppIntent: AppIntent {
/// App intent to enable the VPN
///
/// This is used in App Shortcuts, for things like Shortcuts.app, Spotlight and Siri.
/// This is very similar to ``VPNWidgetEnableIntent``, but this runs in-app, allows continuation in the app if needed,
/// This is very similar to ``WidgetEnableVPNIntent``, but this runs in-app, allows continuation in the app if needed,
/// and provides a result dialog.
///
@available(iOS 17.0, *)
@available(iOSApplicationExtension, unavailable)
struct EnableVPNAppIntent: ForegroundContinuableIntent {
struct EnableVPNIntent: ForegroundContinuableIntent {
static let title: LocalizedStringResource = "Enable DuckDuckGo VPN"
static let description: LocalizedStringResource = "Enables the DuckDuckGo VPN"
static let openAppWhenRun: Bool = false
Expand All @@ -84,14 +88,17 @@ struct EnableVPNAppIntent: ForegroundContinuableIntent {
do {
DailyPixel.fireDailyAndCount(pixel: .networkProtectionWidgetConnectAttempt)

let controller = VPNIntentTunnelController()
let controller = VPNWidgetTunnelController()
try await controller.start()

await VPNSnoozeLiveActivityManager().endSnoozeActivity()
VPNReloadStatusWidgets()

DailyPixel.fireDailyAndCount(pixel: .networkProtectionWidgetConnectSuccess)
return .result(dialog: "DuckDuckGo VPN is connecting...")
} catch {
switch error {
case VPNIntentTunnelController.StartFailure.vpnNotConfigured:
case VPNWidgetTunnelController.StartFailure.vpnNotConfigured:
DailyPixel.fireDailyAndCount(pixel: .networkProtectionWidgetConnectCancelled)

let dialog = IntentDialog(stringLiteral: UserText.vpnNeedsToBeEnabledFromApp)
Expand Down
1 change: 0 additions & 1 deletion DuckDuckGo/VPNSnoozeActivityAttributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import Foundation
import ActivityKit
import SwiftUI

struct VPNSnoozeActivityAttributes: ActivityAttributes {
struct ContentState: Codable & Hashable {
Expand Down
8 changes: 8 additions & 0 deletions LocalPackages/VPNiOS/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.DS_Store
/.build
/Packages
xcuserdata/
DerivedData/
.swiftpm/configuration/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
32 changes: 32 additions & 0 deletions LocalPackages/VPNiOS/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// swift-tools-version: 6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "VPNiOS",
platforms: [
.iOS(.v15)
],
products: [
// Products define the executables and libraries a package produces, making them visible to other packages.
.library(
name: "VPNWidgetSupport",
targets: ["VPNWidgetSupport"]),
// Products define the executables and libraries a package produces, making them visible to other packages.
.library(
name: "VPNAppIntents",
targets: ["VPNAppIntents"]),
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
.target(
name: "VPNWidgetSupport"
),
.target(
name: "VPNAppIntents",
dependencies: ["VPNWidgetSupport"]
),
]
)
92 changes: 92 additions & 0 deletions LocalPackages/VPNiOS/Sources/VPNAppIntents/VPNAppIntents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
//

Check failure on line 1 in LocalPackages/VPNiOS/Sources/VPNAppIntents/VPNAppIntents.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Header comments should be consistent with project patterns (file_header)
// VPNAppIntents.swift
// VPNiOS
//
// Created by ddg on 12/17/24.
//

// MARK: - Toggle

/// `ForegroundContinuableIntent` isn't available for extensions, which makes it impossible to call
/// from extensions. This is the recommended workaround from:
/// https://mastodon.social/@mgorbach/110812347476671807
///

import AppIntents
import VPNWidgetSupport
import WidgetKit

public struct VPNAppIntents: AppIntentsPackage { }
/*
@available(iOS 17.0, *)
public struct ControlWidgetToggleVPNIntent: SetValueIntent {
public static let title: LocalizedStringResource = "Toggle DuckDuckGo VPN from the Control Center Widget"
public static let description: LocalizedStringResource = "Toggles the DuckDuckGo VPN from the Control Center widget"
public static let isDiscoverable = false
public static let openAppWhenRun = false

@Parameter(title: "Enabled")
public var value: Bool

public init() {}

public func perform() async throws -> some IntentResult {
if value {
try await startVPN()
} else {
try await stopVPN()
}

return .result()
}

private func startVPN() async throws {
do {
//DailyPixel.fireDailyAndCount(pixel: .vpnControlCenterConnectAttempt)

let controller = VPNWidgetTunnelController()
try await controller.start()

WidgetCenter.shared.reloadAllTimelines()

//await VPNSnoozeLiveActivityManager().endSnoozeActivity()
//VPNReloadStatusWidgets()

//DailyPixel.fireDailyAndCount(pixel: .vpnControlCenterConnectSuccess)
} catch {
switch error {
case VPNWidgetTunnelController.StartFailure.vpnNotConfigured:
//DailyPixel.fireDailyAndCount(pixel: .vpnControlCenterConnectCancelled)
throw error
default:
//DailyPixel.fireDailyAndCount(pixel: .vpnControlCenterConnectFailure, error: error)
throw error
}
}
}

private func stopVPN() async throws {
do {
//DailyPixel.fireDailyAndCount(pixel: .vpnControlCenterDisconnectAttempt)

let controller = VPNWidgetTunnelController()
try await controller.stop()

WidgetCenter.shared.reloadAllTimelines()
//await VPNSnoozeLiveActivityManager().endSnoozeActivity()
//VPNReloadStatusWidgets()

//DailyPixel.fireDailyAndCount(pixel: .vpnControlCenterDisconnectSuccess)
} catch {
switch error {
case VPNWidgetTunnelController.StopFailure.vpnNotConfigured:
//DailyPixel.fireDailyAndCount(pixel: .vpnControlCenterDisconnectCancelled)
throw error
default:
//DailyPixel.fireDailyAndCount(pixel: .vpnControlCenterDisconnectFailure, error: error)
throw error
}
}
}
}
*/
Loading

0 comments on commit 348021b

Please sign in to comment.