UI: TunnelsManager: Add setOnDemandEnabled() instance method

Signed-off-by: Roopesh Chander <roop@roopc.net>
This commit is contained in:
Roopesh Chander 2021-07-25 00:59:36 +05:30
parent 40f18de4d2
commit df9934a4b8
1 changed files with 30 additions and 0 deletions

View File

@ -342,6 +342,36 @@ class TunnelsManager {
} }
} }
func setOnDemandEnabled(_ isOnDemandEnabled: Bool, on tunnel: TunnelContainer, completionHandler: @escaping (TunnelsManagerError?) -> Void) {
let tunnelProviderManager = tunnel.tunnelProvider
guard tunnelProviderManager.isOnDemandEnabled != isOnDemandEnabled else {
completionHandler(nil)
return
}
let isActivatingOnDemand = !tunnelProviderManager.isOnDemandEnabled && isOnDemandEnabled
tunnelProviderManager.isOnDemandEnabled = isOnDemandEnabled
tunnelProviderManager.saveToPreferences { error in
if let error = error {
wg_log(.error, message: "Modify On-Demand: Saving configuration failed: \(error)")
completionHandler(TunnelsManagerError.systemErrorOnModifyTunnel(systemError: error))
return
}
if isActivatingOnDemand {
tunnelProviderManager.loadFromPreferences { error in
tunnel.isActivateOnDemandEnabled = tunnelProviderManager.isOnDemandEnabled
if let error = error {
wg_log(.error, message: "Modify On-Demand: Re-loading after saving configuration failed: \(error)")
completionHandler(TunnelsManagerError.systemErrorOnModifyTunnel(systemError: error))
} else {
completionHandler(nil)
}
}
} else {
completionHandler(nil)
}
}
}
func numberOfTunnels() -> Int { func numberOfTunnels() -> Int {
return tunnels.count return tunnels.count
} }