Skip to content

Commit

Permalink
Fix brave/brave-ios#7606: VPN toggle switch is blinking twice when en…
Browse files Browse the repository at this point in the history
…abling it (brave/brave-ios#7629)
  • Loading branch information
soner-yuksel authored Jun 23, 2023
1 parent 9db1833 commit 1d85308
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
18 changes: 10 additions & 8 deletions Sources/BraveVPN/BraveVPNProtocolPickerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,17 @@ extension BraveVPNProtocolPickerViewController: UITableViewDelegate, UITableView
BraveVPN.changePreferredTransportProtocol(with: tunnelProtocol) { [weak self] success in
guard let self else { return }

self.isLoading = false

if success {
self.dismiss(animated: true) {
self.showSuccessAlert(text: Strings.VPN.protocolSwitchSuccessPopupText)
DispatchQueue.main.async {
self.isLoading = false

if success {
self.dismiss(animated: true) {
self.showSuccessAlert(text: Strings.VPN.protocolSwitchSuccessPopupText)
}
} else {
self.showErrorAlert(title: Strings.VPN.protocolPickerErrorTitle,
message: Strings.VPN.protocolPickerErrorMessage)
}
} else {
self.showErrorAlert(title: Strings.VPN.protocolPickerErrorTitle,
message: Strings.VPN.protocolPickerErrorMessage)
}
}

Expand Down
22 changes: 19 additions & 3 deletions Sources/BraveVPN/BraveVPNSettingsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public class BraveVPNSettingsViewController: TableViewController {
super.viewDidLoad()

title = Strings.VPN.vpnName
NotificationCenter.default.addObserver(self, selector: #selector(vpnConfigChanged),
NotificationCenter.default.addObserver(self, selector: #selector(vpnConfigChanged(_:)),
name: .NEVPNStatusDidChange, object: nil)

let switchView = SwitchAccessoryView(initialValue: BraveVPN.isConnected, valueChange: { vpnOn in
Expand Down Expand Up @@ -304,7 +304,23 @@ public class BraveVPNSettingsViewController: TableViewController {
present(alert, animated: true)
}

@objc func vpnConfigChanged() {
vpnConnectionStatusSwitch?.isOn = BraveVPN.isConnected
@objc func vpnConfigChanged(_ notification: NSNotification) {
guard let vpnConnection = notification.object as? NEVPNConnection else {
return
}

switch vpnConnection.status {
case.connecting, .disconnecting, .reasserting:
isLoading = true
case .invalid:
vpnConnectionStatusSwitch?.isOn = false
case .connected, .disconnected:
vpnConnectionStatusSwitch?.isOn = BraveVPN.isConnected
@unknown default:
assertionFailure()
break
}

isLoading = false
}
}

0 comments on commit 1d85308

Please sign in to comment.