aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrej Mihajlov <and@mullvad.net>2021-03-29 12:40:06 +0200
committerAndrej Mihajlov <and@mullvad.net>2021-03-29 12:40:06 +0200
commitb1d017b6709813012dc8d0ed52d5f158d8ce6190 (patch)
treebd713b30fadce3f43c4fd87a9b53a947049fcec2
parentUI: show "on-demand is active" for tunnels with the active on-demand (diff)
downloadwireguard-apple-am/toggle-on-demand-on-activation.tar.xz
wireguard-apple-am/toggle-on-demand-on-activation.zip
UI: update tunnel provider properties and trigger tunnelModified when changing the tunnel settings on activation or deactivationam/toggle-on-demand-on-activation
Signed-off-by: Andrej Mihajlov <and@mullvad.net>
-rw-r--r--Sources/WireGuardApp/Tunnel/TunnelsManager.swift31
-rw-r--r--Sources/WireGuardApp/UI/iOS/View/TunnelListCell.swift1
-rw-r--r--Sources/WireGuardApp/UI/iOS/ViewController/TunnelsListTableViewController.swift8
3 files changed, 31 insertions, 9 deletions
diff --git a/Sources/WireGuardApp/Tunnel/TunnelsManager.swift b/Sources/WireGuardApp/Tunnel/TunnelsManager.swift
index cca102e7..61883d10 100644
--- a/Sources/WireGuardApp/Tunnel/TunnelsManager.swift
+++ b/Sources/WireGuardApp/Tunnel/TunnelsManager.swift
@@ -394,7 +394,11 @@ class TunnelsManager {
#if targetEnvironment(simulator)
tunnel.status = .active
#else
- tunnel.startActivation(activationDelegate: activationDelegate)
+ tunnel.startActivation(didSaveToPreferencesCompletion: { [weak self] in
+ guard let self = self else { return }
+
+ self.tunnelsListDelegate?.tunnelModified(at: self.tunnels.firstIndex(of: tunnel)!)
+ }, activationDelegate: activationDelegate)
#endif
#if os(iOS)
@@ -409,7 +413,12 @@ class TunnelsManager {
#if targetEnvironment(simulator)
tunnel.status = .inactive
#else
- tunnel.startDeactivation()
+ tunnel.startDeactivation { [weak self] error in
+ guard let self = self else { return }
+ if error == nil {
+ self.tunnelsListDelegate?.tunnelModified(at: self.tunnels.firstIndex(of: tunnel)!)
+ }
+ }
#endif
}
@@ -578,7 +587,7 @@ class TunnelContainer: NSObject {
status = TunnelStatus(from: tunnelProvider.connection.status)
}
- fileprivate func startActivation(recursionCount: UInt = 0, lastError: Error? = nil, activationDelegate: TunnelsManagerActivationDelegate?) {
+ fileprivate func startActivation(recursionCount: UInt = 0, lastError: Error? = nil, didSaveToPreferencesCompletion: @escaping () -> Void, activationDelegate: TunnelsManagerActivationDelegate?) {
if recursionCount >= 8 {
wg_log(.error, message: "startActivation: Failed after 8 attempts. Giving up with \(lastError!)")
activationDelegate?.tunnelActivationAttemptFailed(tunnel: self, error: .failedBecauseOfTooManyErrors(lastSystemError: lastError!))
@@ -597,13 +606,14 @@ class TunnelContainer: NSObject {
tunnelProvider.isOnDemandEnabled = onDemandOption != .off
tunnelProvider.saveToPreferences { [weak self] error in
guard let self = self else { return }
- if error != nil {
- wg_log(.error, message: "Error saving tunnel after re-enabling: \(error!)")
- activationDelegate?.tunnelActivationAttemptFailed(tunnel: self, error: .failedWhileSaving(systemError: error!))
+ if let error = error {
+ wg_log(.error, message: "Error saving tunnel after re-enabling: \(error)")
+ activationDelegate?.tunnelActivationAttemptFailed(tunnel: self, error: .failedWhileSaving(systemError: error))
return
}
+ didSaveToPreferencesCompletion()
wg_log(.debug, staticMessage: "startActivation: Tunnel saved after re-enabling, invoking startActivation")
- self.startActivation(recursionCount: recursionCount + 1, lastError: NEVPNError(NEVPNError.configurationUnknown), activationDelegate: activationDelegate)
+ self.startActivation(recursionCount: recursionCount + 1, lastError: NEVPNError(NEVPNError.configurationUnknown), didSaveToPreferencesCompletion: didSaveToPreferencesCompletion, activationDelegate: activationDelegate)
}
return
}
@@ -642,12 +652,12 @@ class TunnelContainer: NSObject {
return
}
wg_log(.debug, staticMessage: "startActivation: Tunnel reloaded, invoking startActivation")
- self.startActivation(recursionCount: recursionCount + 1, lastError: systemError, activationDelegate: activationDelegate)
+ self.startActivation(recursionCount: recursionCount + 1, lastError: systemError, didSaveToPreferencesCompletion: didSaveToPreferencesCompletion, activationDelegate: activationDelegate)
}
}
}
- fileprivate func startDeactivation() {
+ fileprivate func startDeactivation(completionHandler: @escaping (Error?) -> Void) {
wg_log(.debug, message: "startDeactivation: Tunnel: \(name)")
if tunnelProvider.isOnDemandEnabled {
@@ -657,10 +667,13 @@ class TunnelContainer: NSObject {
wg_log(.error, message: "startDeactivation: Error disabling on-demand: \(error.localizedDescription)")
}
+ self?.updateTunnelProviderDependentProperties()
(self?.tunnelProvider.connection as? NETunnelProviderSession)?.stopTunnel()
+ completionHandler(error)
}
} else {
(self.tunnelProvider.connection as? NETunnelProviderSession)?.stopTunnel()
+ completionHandler(nil)
}
}
diff --git a/Sources/WireGuardApp/UI/iOS/View/TunnelListCell.swift b/Sources/WireGuardApp/UI/iOS/View/TunnelListCell.swift
index 8807c2c6..3b9d8c89 100644
--- a/Sources/WireGuardApp/UI/iOS/View/TunnelListCell.swift
+++ b/Sources/WireGuardApp/UI/iOS/View/TunnelListCell.swift
@@ -135,6 +135,7 @@ class TunnelListCell: UITableViewCell {
subTitleLabelBottomConstraint?.isActive = false
nameLabelBottomConstraint?.isActive = true
}
+ contentView.layoutIfNeeded()
}
private func update(from tunnel: TunnelContainer?, animated: Bool) {
diff --git a/Sources/WireGuardApp/UI/iOS/ViewController/TunnelsListTableViewController.swift b/Sources/WireGuardApp/UI/iOS/ViewController/TunnelsListTableViewController.swift
index 1cf4a07b..1f581c03 100644
--- a/Sources/WireGuardApp/UI/iOS/ViewController/TunnelsListTableViewController.swift
+++ b/Sources/WireGuardApp/UI/iOS/ViewController/TunnelsListTableViewController.swift
@@ -378,20 +378,28 @@ extension TunnelsListTableViewController: UITableViewDelegate {
extension TunnelsListTableViewController: TunnelsManagerListDelegate {
func tunnelAdded(at index: Int) {
+ tableView.beginUpdates()
tableView.insertRows(at: [IndexPath(row: index, section: 0)], with: .automatic)
centeredAddButton.isHidden = (tunnelsManager?.numberOfTunnels() ?? 0 > 0)
+ tableView.endUpdates()
}
func tunnelModified(at index: Int) {
+ tableView.beginUpdates()
tableView.reloadRows(at: [IndexPath(row: index, section: 0)], with: .automatic)
+ tableView.endUpdates()
}
func tunnelMoved(from oldIndex: Int, to newIndex: Int) {
+ tableView.beginUpdates()
tableView.moveRow(at: IndexPath(row: oldIndex, section: 0), to: IndexPath(row: newIndex, section: 0))
+ tableView.endUpdates()
}
func tunnelRemoved(at index: Int, tunnel: TunnelContainer) {
+ tableView.beginUpdates()
tableView.deleteRows(at: [IndexPath(row: index, section: 0)], with: .automatic)
+ tableView.endUpdates()
centeredAddButton.isHidden = tunnelsManager?.numberOfTunnels() ?? 0 > 0
if detailDisplayedTunnel == tunnel, let splitViewController = splitViewController {
if splitViewController.isCollapsed != false {