Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Fix #7448, fix #7413: VPN Initialize and Connect issues #7525

Merged
merged 3 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,12 @@ struct VPNMenuButton: View {
}
.onReceive(NotificationCenter.default.publisher(for: .NEVPNStatusDidChange)) { _ in
isVPNEnabled = BraveVPN.isConnected
isVPNStatusChanging = BraveVPN.reconnectPending

if BraveVPN.isConnected {
isVPNStatusChanging = false
} else {
isVPNStatusChanging = BraveVPN.reconnectPending
}
}
}
}
6 changes: 4 additions & 2 deletions Sources/BraveVPN/BraveVPN.swift
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,10 @@ public class BraveVPN {
logAndStoreError("configureAndConnectVPN: \(error)")
}

reconnectPending = false

// Re-connected user should update last used region - detail is pulled
fetchLastUsedRegionDetail() { _, _ in
reconnectPending = false
DispatchQueue.main.async {
completion?(status == .success)
}
Expand All @@ -302,9 +303,10 @@ public class BraveVPN {
helper.ikev2VPNManager.removeFromPreferences()
}

reconnectPending = false

// First time user will connect automatic region - detail is pulled
fetchLastUsedRegionDetail() { _, _ in
reconnectPending = false
DispatchQueue.main.async {
completion?(success)
}
Expand Down
74 changes: 39 additions & 35 deletions Sources/BraveVPN/BuyVPNViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Preferences
import StoreKit
import os.log

class BuyVPNViewController: UIViewController {
class BuyVPNViewController: VPNSetupLoadingController {

let iapObserver: IAPObserver

Expand All @@ -29,40 +29,6 @@ class BuyVPNViewController: UIViewController {
view = View()
}

/// View to show when the vpn purchase is pending.
private var overlayView: UIView?

private var isLoading: Bool = false {
didSet {
overlayView?.removeFromSuperview()

// Toggle 'restore' button.
navigationItem.rightBarButtonItem?.isEnabled = !isLoading

// Prevent dismissing the modal by swipe when the VPN is being configured
navigationController?.isModalInPresentation = isLoading == true

if !isLoading { return }

let overlay = UIView().then {
$0.backgroundColor = UIColor.black.withAlphaComponent(0.5)
let activityIndicator = UIActivityIndicatorView().then { indicator in
indicator.startAnimating()
indicator.autoresizingMask = [.flexibleWidth, .flexibleHeight]
}

$0.addSubview(activityIndicator)
}

buyVPNView.addSubview(overlay)
overlay.snp.makeConstraints {
$0.edges.equalToSuperview()
}

overlayView = overlay
}
}

override func viewDidLoad() {
super.viewDidLoad()

Expand Down Expand Up @@ -175,3 +141,41 @@ extension BuyVPNViewController: IAPObserverDelegate {
}
}
}

class VPNSetupLoadingController: UIViewController {

private var overlayView: UIView?

var isLoading: Bool = false {
didSet {
overlayView?.removeFromSuperview()

// Disable Action bar button while loading
navigationItem.rightBarButtonItem?.isEnabled = !isLoading

// Prevent dismissing the modal by swipe
navigationController?.isModalInPresentation = isLoading == true

if !isLoading { return }

let overlay = UIView().then {
$0.backgroundColor = UIColor.black.withAlphaComponent(0.5)
let activityIndicator = UIActivityIndicatorView().then {
$0.startAnimating()
$0.autoresizingMask = [.flexibleWidth, .flexibleHeight]
$0.style = .large
$0.color = .white
}

$0.addSubview(activityIndicator)
}

view.addSubview(overlay)
overlay.snp.makeConstraints {
$0.edges.equalToSuperview()
}

overlayView = overlay
}
}
}
54 changes: 36 additions & 18 deletions Sources/BraveVPN/InstallVPNViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import Shared
import Lottie
import BraveUI

class InstallVPNViewController: UIViewController {
class InstallVPNViewController: VPNSetupLoadingController {

private var installVPNView: View {
return view as! View // swiftlint:disable:this force_cast
}

private var installVPNProfileRetryCount = 0

override func loadView() {
view = View()
Expand Down Expand Up @@ -48,33 +50,49 @@ class InstallVPNViewController: UIViewController {
}

@objc func installVPNAction() {
installVPNView.installVPNButton.isLoading = true
isLoading = true

BraveVPN.connectToVPN() { [weak self] status in
guard let self = self else { return }
installProfileAndConnectVPNFirstTime { [weak self] status in
guard let self else { return }

DispatchQueue.main.async {
self.installVPNView.installVPNButton.isLoading = false
}

if status {
self.dismiss(animated: true) {
self.showSuccessAlert()
}
} else {
let okAction = UIAlertAction(title: Strings.OKString, style: .default)

let message = Strings.VPN.vpnConfigGenericErrorBody
let alert = UIAlertController(title: Strings.VPN.vpnConfigGenericErrorTitle,
message: message,
preferredStyle: .alert)
alert.addAction(okAction)

DispatchQueue.main.async {
self.present(alert, animated: true)
// Retry installing profile twice if it fails
// Error generated is WireGuard capabilities are not yet enabled on this node
if self.installVPNProfileRetryCount < 2 {
installVPNAction()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.installVPNAction()

} else {
presentErrorVPNInstallProfile()
}
}
}

func presentErrorVPNInstallProfile() {
let alert = UIAlertController(title: Strings.VPN.vpnConfigGenericErrorTitle,
message: Strings.VPN.vpnConfigGenericErrorBody,
preferredStyle: .alert)

alert.addAction(UIAlertAction(title: Strings.OKString, style: .default))

DispatchQueue.main.async {
self.present(alert, animated: true)
}
}
}

private func installProfileAndConnectVPNFirstTime(completion: @escaping (Bool) -> Void) {
installVPNProfileRetryCount += 1

BraveVPN.connectToVPN() { [weak self] status in
DispatchQueue.main.async {
self?.isLoading = false
}

completion(status)
}
}

@objc private func contactSupportAction() {
Expand Down