Notify intent updates to ServiceVC

Postpone notification until VPN updates.

Fixes the following scenario:

1. Use <foo> profile
2. Send to background
3. Connect to <foo> via shortcut
4. Toggle stays disabled (too early)
This commit is contained in:
Davide De Rosa 2019-03-09 08:58:01 +01:00
parent 5d2450b985
commit 77c40cb169
2 changed files with 30 additions and 5 deletions

View File

@ -29,6 +29,10 @@ import SwiftyBeaver
private let log = SwiftyBeaver.self
extension Notification.Name {
static let IntentDidUpdateService = Notification.Name("IntentDidUpdateService")
}
@available(iOS 12, *)
class InteractionsHandler {
private class Groups {
@ -141,18 +145,19 @@ class InteractionsHandler {
return
}
service.activateProfile(profile)
// FIXME: ServiceViewController is not updated
let configuration: VPNConfiguration
do {
configuration = try service.vpnConfiguration()
} catch let e {
log.error("Unable to build VPN configuration: \(e)")
notifyServiceController()
return
}
vpn.reconnect(configuration: configuration) { (error) in
notifyServiceController()
if let error = error {
log.error("Unable to connect to \(profileKey): \(error)")
return
@ -162,7 +167,9 @@ class InteractionsHandler {
}
private static func handleDisableVPN(_ intent: DisableVPNIntent, interaction: INInteraction) {
VPN.shared.disconnect(completionHandler: nil)
VPN.shared.disconnect { (error) in
notifyServiceController()
}
}
private static func handleMoveToLocation(_ intent: MoveToLocationIntent, interaction: INInteraction) {
@ -193,6 +200,8 @@ class InteractionsHandler {
}
vpn.reconnect(configuration: configuration) { (error) in
notifyServiceController()
if let error = error {
log.error("Unable to connect to \(providerId) @ [\(poolId)]: \(error)")
return
@ -229,16 +238,21 @@ class InteractionsHandler {
configuration = try service.vpnConfiguration()
} catch let e {
log.error("Unable to build VPN configuration: \(e)")
notifyServiceController()
return
}
let vpn = VPN.shared
switch vpn.status {
case .connected:
vpn.reconnect(configuration: configuration, completionHandler: nil)
vpn.reconnect(configuration: configuration) { (error) in
notifyServiceController()
}
default:
vpn.install(configuration: configuration, completionHandler: nil)
vpn.install(configuration: configuration) { (error) in
notifyServiceController()
}
}
}
@ -253,6 +267,12 @@ class InteractionsHandler {
log.debug("Removed profile \(profileKey) interactions")
}
}
//
private static func notifyServiceController() {
NotificationCenter.default.post(name: .IntentDidUpdateService, object: nil)
}
}
private extension INInteraction {

View File

@ -98,6 +98,7 @@ class ServiceViewController: UIViewController, TableModelHost {
nc.addObserver(self, selector: #selector(applicationDidBecomeActive), name: UIApplication.didBecomeActiveNotification, object: nil)
nc.addObserver(self, selector: #selector(vpnDidUpdate), name: .VPNDidChangeStatus, object: nil)
nc.addObserver(self, selector: #selector(vpnDidUpdate), name: .VPNDidReinstall, object: nil)
nc.addObserver(self, selector: #selector(intentDidUpdateService), name: .IntentDidUpdateService, object: nil)
// run this no matter what
// XXX: convenient here vs AppDelegate for updating table
@ -420,6 +421,10 @@ class ServiceViewController: UIViewController, TableModelHost {
}
}
@objc private func intentDidUpdateService() {
setProfile(profile)
}
@objc private func applicationDidBecomeActive() {
reloadVpnStatus()
}