Fix issues with VPN toggle

- Move rate limit to UI only

- Keep caption constant to "Enabled"
This commit is contained in:
Davide De Rosa 2022-04-25 23:05:14 +02:00
parent 92b9c4cd9a
commit ccde6a30cf
4 changed files with 14 additions and 28 deletions

View File

@ -140,7 +140,6 @@ class AppContext {
} }
} }
providerManager.rateLimitMilliseconds = Constants.RateLimit.providerManager providerManager.rateLimitMilliseconds = Constants.RateLimit.providerManager
vpnManager.rateLimitMilliseconds = Constants.RateLimit.vpnManager
vpnManager.isOnDemandRulesSupported = { vpnManager.isOnDemandRulesSupported = {
self.isEligibleForOnDemandRules() self.isEligibleForOnDemandRules()
} }

View File

@ -139,7 +139,7 @@ extension Constants {
enum RateLimit { enum RateLimit {
static let providerManager = 10000 static let providerManager = 10000
static let vpnManager = 500 static let vpnToggle = 500
} }
enum Log { enum Log {

View File

@ -44,6 +44,8 @@ extension ProfileView {
@State private var isLocallyEnabled = false @State private var isLocallyEnabled = false
@State private var canToggle = true
private var isEnabled: Binding<Bool> { private var isEnabled: Binding<Bool> {
.init { .init {
isLocallyEnabled isLocallyEnabled
@ -101,7 +103,7 @@ extension ProfileView {
isLocallyEnabled = currentVPNState.isEnabled isLocallyEnabled = currentVPNState.isEnabled
}.onChange(of: currentVPNState.isEnabled) { }.onChange(of: currentVPNState.isEnabled) {
isLocallyEnabled = $0 isLocallyEnabled = $0
}.disabled(vpnManager.isRateLimiting) }.disabled(!canToggle)
Text(L10n.Profile.Items.ConnectionStatus.caption) Text(L10n.Profile.Items.ConnectionStatus.caption)
.withTrailingText(currentVPNState.localizedStatusDescription( .withTrailingText(currentVPNState.localizedStatusDescription(
@ -112,8 +114,9 @@ extension ProfileView {
} }
private var vpnToggleString: String { private var vpnToggleString: String {
let V = L10n.Profile.Items.Vpn.self // let V = L10n.Profile.Items.Vpn.self
return currentVPNState.isEnabled ? V.TurnOff.caption : V.TurnOn.caption // return currentVPNState.isEnabled ? V.TurnOff.caption : V.TurnOn.caption
L10n.Global.Strings.enabled
} }
private var inactiveSubview: some View { private var inactiveSubview: some View {
@ -146,6 +149,12 @@ extension ProfileView {
guard vpnManager.toggle() else { guard vpnManager.toggle() else {
return 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 // eligibility: donate intents if eligible for Siri
if isEligibleForSiri { if isEligibleForSiri {

View File

@ -29,7 +29,7 @@ import TunnelKitManager
import TunnelKitOpenVPNManager import TunnelKitOpenVPNManager
@MainActor @MainActor
public class VPNManager: ObservableObject, RateLimited { public class VPNManager: ObservableObject {
// MARK: Initialization // MARK: Initialization
@ -81,20 +81,6 @@ public class VPNManager: ObservableObject, RateLimited {
} }
public func toggle() -> Bool { 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 { guard let configuration = vpnConfigurationWithCurrentProfile() else {
return false return false
} }
@ -141,14 +127,6 @@ public class VPNManager: ObservableObject, RateLimited {
public func debugLogURL(forProtocol vpnProtocol: VPNProtocolType) -> URL? { public func debugLogURL(forProtocol vpnProtocol: VPNProtocolType) -> URL? {
return strategy.debugLogURL(forProtocol: vpnProtocol) 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 // MARK: Observation