From ccde6a30cf3ae443a30d7e1545f56206b996d072 Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Mon, 25 Apr 2022 23:05:14 +0200 Subject: [PATCH] Fix issues with VPN toggle - Move rate limit to UI only - Keep caption constant to "Enabled" --- Passepartout/App/Constants/AppContext.swift | 1 - .../App/Constants/Constants+Extensions.swift | 2 +- Passepartout/App/Views/ProfileView+VPN.swift | 15 +++++++++--- .../Managers/VPNManager.swift | 24 +------------------ 4 files changed, 14 insertions(+), 28 deletions(-) diff --git a/Passepartout/App/Constants/AppContext.swift b/Passepartout/App/Constants/AppContext.swift index 6c93a574..dbd019ea 100644 --- a/Passepartout/App/Constants/AppContext.swift +++ b/Passepartout/App/Constants/AppContext.swift @@ -140,7 +140,6 @@ class AppContext { } } providerManager.rateLimitMilliseconds = Constants.RateLimit.providerManager - vpnManager.rateLimitMilliseconds = Constants.RateLimit.vpnManager vpnManager.isOnDemandRulesSupported = { self.isEligibleForOnDemandRules() } diff --git a/Passepartout/App/Constants/Constants+Extensions.swift b/Passepartout/App/Constants/Constants+Extensions.swift index b17697ba..7174ce52 100644 --- a/Passepartout/App/Constants/Constants+Extensions.swift +++ b/Passepartout/App/Constants/Constants+Extensions.swift @@ -139,7 +139,7 @@ extension Constants { enum RateLimit { static let providerManager = 10000 - static let vpnManager = 500 + static let vpnToggle = 500 } enum Log { diff --git a/Passepartout/App/Views/ProfileView+VPN.swift b/Passepartout/App/Views/ProfileView+VPN.swift index 702e1c85..2f1d139a 100644 --- a/Passepartout/App/Views/ProfileView+VPN.swift +++ b/Passepartout/App/Views/ProfileView+VPN.swift @@ -44,6 +44,8 @@ extension ProfileView { @State private var isLocallyEnabled = false + @State private var canToggle = true + private var isEnabled: Binding { .init { isLocallyEnabled @@ -101,7 +103,7 @@ extension ProfileView { isLocallyEnabled = currentVPNState.isEnabled }.onChange(of: currentVPNState.isEnabled) { isLocallyEnabled = $0 - }.disabled(vpnManager.isRateLimiting) + }.disabled(!canToggle) Text(L10n.Profile.Items.ConnectionStatus.caption) .withTrailingText(currentVPNState.localizedStatusDescription( @@ -112,8 +114,9 @@ extension ProfileView { } private var vpnToggleString: String { - let V = L10n.Profile.Items.Vpn.self - return currentVPNState.isEnabled ? V.TurnOff.caption : V.TurnOn.caption +// let V = L10n.Profile.Items.Vpn.self +// return currentVPNState.isEnabled ? V.TurnOff.caption : V.TurnOn.caption + L10n.Global.Strings.enabled } private var inactiveSubview: some View { @@ -146,6 +149,12 @@ extension ProfileView { guard vpnManager.toggle() else { return } + + // rate limit toggle actions + canToggle = false + DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(Constants.RateLimit.vpnToggle)) { + canToggle = true + } // eligibility: donate intents if eligible for Siri if isEligibleForSiri { diff --git a/PassepartoutCore/Sources/PassepartoutCore/Managers/VPNManager.swift b/PassepartoutCore/Sources/PassepartoutCore/Managers/VPNManager.swift index 2305b314..2424af85 100644 --- a/PassepartoutCore/Sources/PassepartoutCore/Managers/VPNManager.swift +++ b/PassepartoutCore/Sources/PassepartoutCore/Managers/VPNManager.swift @@ -29,7 +29,7 @@ import TunnelKitManager import TunnelKitOpenVPNManager @MainActor -public class VPNManager: ObservableObject, RateLimited { +public class VPNManager: ObservableObject { // MARK: Initialization @@ -81,20 +81,6 @@ public class VPNManager: ObservableObject, RateLimited { } public func toggle() -> Bool { - guard !isRateLimited("") else { - return false - } - lastActionDate[""] = Date() - - // signal rate limiting flag (e.g. for UI) - if let rateLimitMilliseconds = rateLimitMilliseconds { - isRateLimiting = true - Task { - try await Task.sleep(nanoseconds: UInt64(rateLimitMilliseconds) * NSEC_PER_MSEC) - isRateLimiting = false - } - } - guard let configuration = vpnConfigurationWithCurrentProfile() else { return false } @@ -141,14 +127,6 @@ public class VPNManager: ObservableObject, RateLimited { public func debugLogURL(forProtocol vpnProtocol: VPNProtocolType) -> URL? { return strategy.debugLogURL(forProtocol: vpnProtocol) } - - // MARK: RateLimited - - public var lastActionDate: [String: Date] = [:] - - public var rateLimitMilliseconds: Int? - - @Published public private(set) var isRateLimiting = false } // MARK: Observation