From 8648f808772989806eae177afa117a101d3853cf Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Sun, 10 Mar 2019 19:43:27 +0530 Subject: [PATCH] TunnelsManager: Support for removing multiple tunnels at a time --- .../WireGuard/Tunnel/TunnelsManager.swift | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/WireGuard/WireGuard/Tunnel/TunnelsManager.swift b/WireGuard/WireGuard/Tunnel/TunnelsManager.swift index a94d63d..3f7d9f5 100644 --- a/WireGuard/WireGuard/Tunnel/TunnelsManager.swift +++ b/WireGuard/WireGuard/Tunnel/TunnelsManager.swift @@ -240,8 +240,7 @@ class TunnelsManager { completionHandler(TunnelsManagerError.systemErrorOnRemoveTunnel(systemError: error!)) return } - if let self = self { - let index = self.tunnels.firstIndex(of: tunnel)! + if let self = self, let index = self.tunnels.firstIndex(of: tunnel) { self.tunnels.remove(at: index) self.tunnelsListDelegate?.tunnelRemoved(at: index, tunnel: tunnel) } @@ -249,6 +248,27 @@ class TunnelsManager { } } + func removeMultiple(tunnels: [TunnelContainer], completionHandler: @escaping (TunnelsManagerError?) -> Void) { + removeMultiple(tunnels: ArraySlice(tunnels), completionHandler: completionHandler) + } + + private func removeMultiple(tunnels: ArraySlice, completionHandler: @escaping (TunnelsManagerError?) -> Void) { + guard let head = tunnels.first else { + completionHandler(nil) + return + } + let tail = tunnels.dropFirst() + remove(tunnel: head) { [weak self, tail] error in + DispatchQueue.main.async { + if let error = error { + completionHandler(error) + } else { + self?.removeMultiple(tunnels: tail, completionHandler: completionHandler) + } + } + } + } + func numberOfTunnels() -> Int { return tunnels.count }