wireguard-apple/WireGuard/Coordinators/AppCoordinator.swift

130 lines
4.6 KiB
Swift
Raw Normal View History

2018-05-23 19:49:10 +00:00
//
// AppCoordinator.swift
// WireGuard
2018-05-23 19:49:10 +00:00
//
// Created by Jeroen Leenarts on 23-05-18.
// Copyright © 2018 WireGuard. All rights reserved.
2018-05-23 19:49:10 +00:00
//
import Foundation
import CoreData
import BNRCoreDataStack
2018-05-26 07:17:36 +00:00
extension UINavigationController: Identifyable {}
2018-05-23 19:49:10 +00:00
class AppCoordinator: RootViewCoordinator {
let persistentContainer = NSPersistentContainer(name: "WireGuard")
2018-05-23 19:49:10 +00:00
let storyboard = UIStoryboard(name: "Main", bundle: nil)
// MARK: - Properties
var childCoordinators: [Coordinator] = []
var rootViewController: UIViewController {
2018-05-26 07:17:36 +00:00
return self.tunnelsTableViewController
2018-05-23 19:49:10 +00:00
}
2018-05-26 07:17:36 +00:00
var tunnelsTableViewController: TunnelsTableViewController!
2018-05-23 19:49:10 +00:00
/// Window to manage
let window: UIWindow
let navigationController: UINavigationController = {
2018-05-26 07:17:36 +00:00
let navController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(type: UINavigationController.self)
2018-05-23 19:49:10 +00:00
return navController
}()
// MARK: - Init
public init(window: UIWindow) {
self.window = window
self.window.rootViewController = self.navigationController
self.window.makeKeyAndVisible()
}
// MARK: - Functions
/// Starts the coordinator
public func start() {
persistentContainer.viewContext.automaticallyMergesChangesFromParent = true
persistentContainer.loadPersistentStores { [weak self] (_, error) in
if let error = error {
print("Unable to Load Persistent Store. \(error), \(error.localizedDescription)")
} else {
DispatchQueue.main.async {
//start
2018-05-26 07:17:36 +00:00
if let tunnelsTableViewController = self?.storyboard.instantiateViewController(type: TunnelsTableViewController.self) {
self?.tunnelsTableViewController = tunnelsTableViewController
self?.tunnelsTableViewController.viewContext = self?.persistentContainer.viewContext
self?.tunnelsTableViewController.delegate = self
self?.navigationController.viewControllers = [tunnelsTableViewController]
2018-05-23 19:49:10 +00:00
do {
2018-05-26 07:17:36 +00:00
if let context = self?.persistentContainer.viewContext, try Tunnel.countInContext(context) == 0 {
print("No tunnels ... yet")
2018-05-23 19:49:10 +00:00
}
} catch {
self?.showError(error)
}
}
}
}
}
}
public func showError(_ error: Error) {
showAlert(title: NSLocalizedString("Error", comment: "Error alert title"), message: error.localizedDescription)
}
private func showAlert(title: String, message: String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "OK button"), style: .default))
self.navigationController.present(alert, animated: true)
}
}
2018-05-26 07:17:36 +00:00
extension AppCoordinator: TunnelsTableViewControllerDelegate {
func addProvider(tunnelsTableViewController: TunnelsTableViewController) {
let addContext = persistentContainer.newBackgroundContext()
2018-05-26 07:17:36 +00:00
showTunnelConfigurationViewController(tunnel: nil, context: addContext)
2018-05-23 19:49:10 +00:00
}
2018-05-26 07:17:36 +00:00
func connect(tunnel: Tunnel, tunnelsTableViewController: TunnelsTableViewController) {
2018-05-23 19:49:10 +00:00
// TODO implement
2018-05-26 07:17:36 +00:00
print("connect tunnel \(tunnel)")
2018-05-23 19:49:10 +00:00
}
2018-05-26 07:17:36 +00:00
func configure(tunnel: Tunnel, tunnelsTableViewController: TunnelsTableViewController) {
2018-05-23 19:49:10 +00:00
// TODO implement
2018-05-26 07:17:36 +00:00
print("configure tunnel \(tunnel)")
let editContext = persistentContainer.newBackgroundContext()
2018-05-26 07:17:36 +00:00
var backgroundTunnel: Tunnel?
editContext.performAndWait {
2018-05-26 07:17:36 +00:00
backgroundTunnel = editContext.object(with: tunnel.objectID) as? Tunnel
}
2018-05-26 07:17:36 +00:00
showTunnelConfigurationViewController(tunnel: backgroundTunnel, context: editContext)
}
2018-05-26 07:17:36 +00:00
func showTunnelConfigurationViewController(tunnel: Tunnel?, context: NSManagedObjectContext) {
let tunnelConfigurationViewController = storyboard.instantiateViewController(type: TunnelConfigurationTableViewController.self)
2018-05-26 07:17:36 +00:00
tunnelConfigurationViewController.viewContext = context
tunnelConfigurationViewController.delegate = self
2018-05-26 07:17:36 +00:00
self.navigationController.pushViewController(tunnelConfigurationViewController, animated: true)
2018-05-23 19:49:10 +00:00
}
2018-05-26 07:17:36 +00:00
func delete(tunnel: Tunnel, tunnelsTableViewController: TunnelsTableViewController) {
2018-05-23 19:49:10 +00:00
// TODO implement
2018-05-26 07:17:36 +00:00
print("delete tunnel \(tunnel)")
2018-05-23 19:49:10 +00:00
}
}
2018-05-26 07:17:36 +00:00
extension AppCoordinator: TunnelConfigurationTableViewControllerDelegate {
}