diff --git a/Sources/BraveVPN/BraveVPNProtocolPickerViewController.swift b/Sources/BraveVPN/BraveVPNProtocolPickerViewController.swift index a0c8872727cf..507fc0f347a3 100644 --- a/Sources/BraveVPN/BraveVPNProtocolPickerViewController.swift +++ b/Sources/BraveVPN/BraveVPNProtocolPickerViewController.swift @@ -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) } } diff --git a/Sources/BraveVPN/BraveVPNSettingsViewController.swift b/Sources/BraveVPN/BraveVPNSettingsViewController.swift index 77123d77c99f..7e8de07c5a99 100644 --- a/Sources/BraveVPN/BraveVPNSettingsViewController.swift +++ b/Sources/BraveVPN/BraveVPNSettingsViewController.swift @@ -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 @@ -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 } }