Make sure to refresh provider managers after saving.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jeroen Leenarts 2018-09-05 20:10:09 +02:00
parent 56c543f1fc
commit b7755b97a3
1 changed files with 37 additions and 25 deletions

View File

@ -10,6 +10,7 @@ import Foundation
import NetworkExtension import NetworkExtension
import os.log import os.log
import ZIPFoundation import ZIPFoundation
import PromiseKit
import CoreData import CoreData
import BNRCoreDataStack import BNRCoreDataStack
@ -64,36 +65,44 @@ class AppCoordinator: RootViewCoordinator {
/// Starts the coordinator /// Starts the coordinator
public func start() { public func start() {
NETunnelProviderManager.loadAllFromPreferences { [weak self] (managers, error) in _ = refreshProviderManagers().then { () -> Promise<Void> in
if let error = error { self.persistentContainer.viewContext.automaticallyMergesChangesFromParent = true
os_log("Unable to load provider managers: %{public}@", log: Log.general, type: .error, error.localizedDescription) self.persistentContainer.loadPersistentStores { [weak self] (_, error) in
} if let error = error {
self?.providerManagers = managers print("Unable to Load Persistent Store. \(error), \(error.localizedDescription)")
}
persistentContainer.viewContext.automaticallyMergesChangesFromParent = true } else {
persistentContainer.loadPersistentStores { [weak self] (_, error) in DispatchQueue.main.async {
if let error = error { //start
print("Unable to Load Persistent Store. \(error), \(error.localizedDescription)") if let tunnelsTableViewController = self?.storyboard.instantiateViewController(type: TunnelsTableViewController.self) {
self?.tunnelsTableViewController = tunnelsTableViewController
} else { self?.tunnelsTableViewController.viewContext = self?.persistentContainer.viewContext
DispatchQueue.main.async { self?.tunnelsTableViewController.delegate = self
//start self?.navigationController.viewControllers = [tunnelsTableViewController]
if let tunnelsTableViewController = self?.storyboard.instantiateViewController(type: TunnelsTableViewController.self) { do {
self?.tunnelsTableViewController = tunnelsTableViewController if let context = self?.persistentContainer.viewContext, try Tunnel.countInContext(context) == 0 {
self?.tunnelsTableViewController.viewContext = self?.persistentContainer.viewContext print("No tunnels ... yet")
self?.tunnelsTableViewController.delegate = self }
self?.navigationController.viewControllers = [tunnelsTableViewController] } catch {
do { self?.showError(error)
if let context = self?.persistentContainer.viewContext, try Tunnel.countInContext(context) == 0 {
print("No tunnels ... yet")
} }
} catch {
self?.showError(error)
} }
} }
} }
} }
return Promise.value(())
}
}
func refreshProviderManagers() -> Promise<Void> {
return Promise { (resolver) in
NETunnelProviderManager.loadAllFromPreferences { [weak self] (managers, error) in
if let error = error {
os_log("Unable to load provider managers: %{public}@", log: Log.general, type: .error, error.localizedDescription)
}
self?.providerManagers = managers
resolver.fulfill(())
}
} }
} }
@ -458,7 +467,10 @@ extension AppCoordinator: TunnelsTableViewControllerDelegate {
os_log("saved preferences", log: Log.general, type: .info) os_log("saved preferences", log: Log.general, type: .info)
} }
navigationController.popToRootViewController(animated: true) _ = refreshProviderManagers().then { () -> Promise<Void> in
self.navigationController.popToRootViewController(animated: true)
return Promise.value(())
}
} }
} }