Skip to content

Commit

Permalink
Feature flagged for ALPHA builds, cleaned up code
Browse files Browse the repository at this point in the history
  • Loading branch information
diegoreymendez committed Dec 20, 2024
1 parent 091f90d commit cae4958
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 22 deletions.
2 changes: 2 additions & 0 deletions DuckDuckGo/NetworkProtectionVPNSettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ struct NetworkProtectionVPNSettingsView: View {
}.daxBodyRegular()
}

#if ALPHA || DEBUG
if #available(iOS 18.0, *) {
NavigationLink {
ControlCenterWidgetEducationView(navBarTitle: "Add DuckDuckGo VPN Shortcut to Your Control Center",
Expand All @@ -186,6 +187,7 @@ struct NetworkProtectionVPNSettingsView: View {
.frame(width: 24, height: 24)
}.daxBodyRegular()
}
#endif
} header: {
Text(UserText.netPVPNShortcutsSectionHeader)
}
Expand Down
2 changes: 2 additions & 0 deletions DuckDuckGo/VPNAutoShortcuts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import AppIntents
import Foundation

#if ALPHA || DEBUG
@available(iOS 17.0, *)
struct VPNAutoShortcutsiOS17: AppShortcutsProvider {

Expand Down Expand Up @@ -59,3 +60,4 @@ struct VPNAutoShortcutsiOS17: AppShortcutsProvider {
systemImageName: "globe")
}
}
#endif
6 changes: 5 additions & 1 deletion DuckDuckGo/VPNShortcutIntents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,11 @@ struct EnableVPNIntent: ForegroundContinuableIntent {
return .result(dialog: "DuckDuckGo VPN is connecting...")
} catch {
switch error {
case VPNWidgetTunnelController.StartFailure.vpnNotConfigured:
case VPNWidgetTunnelController.StartFailure.vpnNotConfigured,
// On update the VPN configuration becomes disabled, until started manually from
// the app.
NEVPNError.configurationDisabled:

DailyPixel.fireDailyAndCount(pixel: .vpnShortcutConnectCancelled)

let dialog = IntentDialog(stringLiteral: UserText.vpnNeedsToBeEnabledFromApp)
Expand Down
32 changes: 18 additions & 14 deletions Widgets/ControlWidgetVPNIntents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,27 @@ import VPNWidgetSupport

// 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
///
//@available(iOS 17.0, *)
//@available(iOSApplicationExtension, unavailable)
//extension ControlWidgetToggleVPNIntent: ForegroundContinuableIntent {}

@available(iOS 17.0, *)
struct ControlWidgetToggleVPNIntent: SetValueIntent {

private enum EnableAttemptFailure: CustomNSError, LocalizedError {
case cancelled

var errorDescription: String? {
switch self {
case .cancelled:
return UserText.vpnNeedsToBeEnabledFromApp
}
}
}

static let title: LocalizedStringResource = "Toggle DuckDuckGo VPN from the Control Center Widget"
static let description: LocalizedStringResource = "Toggles the DuckDuckGo VPN from the Control Center widget"
static let isDiscoverable = false

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

@available(iOS 17.0, *)
@available(iOSApplicationExtension, unavailable)
extension ControlWidgetToggleVPNIntent: ForegroundContinuableIntent {
@MainActor
func perform() async throws -> some IntentResult {
if value {
Expand All @@ -72,9 +72,13 @@ extension ControlWidgetToggleVPNIntent: ForegroundContinuableIntent {
DailyPixel.fireDailyAndCount(pixel: .vpnControlCenterConnectSuccess)
} catch {
switch error {
case VPNWidgetTunnelController.StartFailure.vpnNotConfigured:
case VPNWidgetTunnelController.StartFailure.vpnNotConfigured,
// On update the VPN configuration becomes disabled, until started manually from
// the app.
NEVPNError.configurationDisabled:

DailyPixel.fireDailyAndCount(pixel: .vpnControlCenterConnectCancelled)
throw needsToContinueInForegroundError("Continue in foreground to enable VPN")
throw EnableAttemptFailure.cancelled
default:
DailyPixel.fireDailyAndCount(pixel: .vpnControlCenterConnectFailure, error: error)
throw error
Expand Down
2 changes: 2 additions & 0 deletions Widgets/VPNControlWidget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import Foundation
import SwiftUI
import WidgetKit

#if ALPHA || DEBUG
@available(iOSApplicationExtension 18.0, *)
public struct VPNControlWidget: ControlWidget {
static let displayName = LocalizedStringResource(stringLiteral: "DuckDuckGo\nVPN")
Expand All @@ -44,3 +45,4 @@ public struct VPNControlWidget: ControlWidget {
.description(Self.description)
}
}
#endif
26 changes: 19 additions & 7 deletions Widgets/WidgetVPNIntents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ import VPNWidgetSupport
@available(iOS 17.0, *)
struct WidgetDisableVPNIntent: AppIntent {

private enum DisableAttemptFailure: CustomNSError {
case cancelled
}

static let title: LocalizedStringResource = "Disable DuckDuckGo VPN"
static let description: LocalizedStringResource = "Disables the DuckDuckGo VPN"
static let openAppWhenRun: Bool = false
Expand All @@ -58,7 +54,9 @@ struct WidgetDisableVPNIntent: AppIntent {

DailyPixel.fireDailyAndCount(pixel: .networkProtectionWidgetDisconnectSuccess)
return .result()
} catch VPNWidgetTunnelController.StopFailure.vpnNotConfigured {
} catch VPNWidgetTunnelController.StopFailure.vpnNotConfigured,
NEVPNError.configurationDisabled {

DailyPixel.fireDailyAndCount(pixel: .networkProtectionWidgetDisconnectCancelled)
return .result()
} catch {
Expand All @@ -76,6 +74,18 @@ struct WidgetDisableVPNIntent: AppIntent {
///
@available(iOS 17.0, *)
struct WidgetEnableVPNIntent: AppIntent {

private enum EnableAttemptFailure: CustomNSError, LocalizedError {
case cancelled

var errorDescription: String? {
switch self {
case .cancelled:
return UserText.vpnNeedsToBeEnabledFromApp
}
}
}

static let title: LocalizedStringResource = "Enable DuckDuckGo VPN"
static let description: LocalizedStringResource = "Enables the DuckDuckGo VPN"
static let openAppWhenRun: Bool = false
Expand All @@ -97,9 +107,11 @@ struct WidgetEnableVPNIntent: AppIntent {
return .result()
} catch {
switch error {
case VPNWidgetTunnelController.StartFailure.vpnNotConfigured:
case VPNWidgetTunnelController.StartFailure.vpnNotConfigured,
NEVPNError.configurationDisabled:

DailyPixel.fireDailyAndCount(pixel: .networkProtectionWidgetConnectCancelled)
throw error
throw EnableAttemptFailure.cancelled
default:
DailyPixel.fireDailyAndCount(pixel: .networkProtectionWidgetConnectFailure, error: error)
throw error
Expand Down
2 changes: 2 additions & 0 deletions Widgets/Widgets.swift
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,11 @@ struct VPNBundle: WidgetBundle {
VPNSnoozeLiveActivity()
}

#if ALPHA || DEBUG
if #available(iOS 18, *) {
VPNControlWidget()
}
#endif
}
}

Expand Down

0 comments on commit cae4958

Please sign in to comment.