From cae4958013f1245000ca90d0309500e417c60e8f Mon Sep 17 00:00:00 2001 From: Diego Rey Mendez Date: Fri, 20 Dec 2024 10:31:59 -0300 Subject: [PATCH] Feature flagged for ALPHA builds, cleaned up code --- .../NetworkProtectionVPNSettingsView.swift | 2 ++ DuckDuckGo/VPNAutoShortcuts.swift | 2 ++ DuckDuckGo/VPNShortcutIntents.swift | 6 +++- Widgets/ControlWidgetVPNIntents.swift | 32 +++++++++++-------- Widgets/VPNControlWidget.swift | 2 ++ Widgets/WidgetVPNIntents.swift | 26 +++++++++++---- Widgets/Widgets.swift | 2 ++ 7 files changed, 50 insertions(+), 22 deletions(-) diff --git a/DuckDuckGo/NetworkProtectionVPNSettingsView.swift b/DuckDuckGo/NetworkProtectionVPNSettingsView.swift index 7e32e8693f..9b431d22a1 100644 --- a/DuckDuckGo/NetworkProtectionVPNSettingsView.swift +++ b/DuckDuckGo/NetworkProtectionVPNSettingsView.swift @@ -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", @@ -186,6 +187,7 @@ struct NetworkProtectionVPNSettingsView: View { .frame(width: 24, height: 24) }.daxBodyRegular() } + #endif } header: { Text(UserText.netPVPNShortcutsSectionHeader) } diff --git a/DuckDuckGo/VPNAutoShortcuts.swift b/DuckDuckGo/VPNAutoShortcuts.swift index b130f63dda..fac0970a47 100644 --- a/DuckDuckGo/VPNAutoShortcuts.swift +++ b/DuckDuckGo/VPNAutoShortcuts.swift @@ -20,6 +20,7 @@ import AppIntents import Foundation +#if ALPHA || DEBUG @available(iOS 17.0, *) struct VPNAutoShortcutsiOS17: AppShortcutsProvider { @@ -59,3 +60,4 @@ struct VPNAutoShortcutsiOS17: AppShortcutsProvider { systemImageName: "globe") } } +#endif diff --git a/DuckDuckGo/VPNShortcutIntents.swift b/DuckDuckGo/VPNShortcutIntents.swift index aaa5eb62c8..cf04dc6bd1 100644 --- a/DuckDuckGo/VPNShortcutIntents.swift +++ b/DuckDuckGo/VPNShortcutIntents.swift @@ -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) diff --git a/Widgets/ControlWidgetVPNIntents.swift b/Widgets/ControlWidgetVPNIntents.swift index d3c1afb7f4..1098aeca22 100644 --- a/Widgets/ControlWidgetVPNIntents.swift +++ b/Widgets/ControlWidgetVPNIntents.swift @@ -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 { @@ -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 diff --git a/Widgets/VPNControlWidget.swift b/Widgets/VPNControlWidget.swift index 39c20891e8..4b1048147b 100644 --- a/Widgets/VPNControlWidget.swift +++ b/Widgets/VPNControlWidget.swift @@ -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") @@ -44,3 +45,4 @@ public struct VPNControlWidget: ControlWidget { .description(Self.description) } } +#endif diff --git a/Widgets/WidgetVPNIntents.swift b/Widgets/WidgetVPNIntents.swift index 32609cd25c..b546dc5bbc 100644 --- a/Widgets/WidgetVPNIntents.swift +++ b/Widgets/WidgetVPNIntents.swift @@ -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 @@ -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 { @@ -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 @@ -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 diff --git a/Widgets/Widgets.swift b/Widgets/Widgets.swift index e12e774acb..6966e69c21 100644 --- a/Widgets/Widgets.swift +++ b/Widgets/Widgets.swift @@ -259,9 +259,11 @@ struct VPNBundle: WidgetBundle { VPNSnoozeLiveActivity() } + #if ALPHA || DEBUG if #available(iOS 18, *) { VPNControlWidget() } + #endif } }