2018-05-23 19:49:10 +00:00
|
|
|
//
|
|
|
|
// AppCoordinator.swift
|
2018-05-24 18:13:21 +00:00
|
|
|
// WireGuard
|
2018-05-23 19:49:10 +00:00
|
|
|
//
|
|
|
|
// Created by Jeroen Leenarts on 23-05-18.
|
2018-05-24 18:13:21 +00:00
|
|
|
// Copyright © 2018 WireGuard. All rights reserved.
|
2018-05-23 19:49:10 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
import CoreData
|
|
|
|
import BNRCoreDataStack
|
|
|
|
|
|
|
|
class AppCoordinator: RootViewCoordinator {
|
|
|
|
|
2018-05-24 18:13:21 +00:00
|
|
|
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 {
|
|
|
|
return self.connectionsTableViewController
|
|
|
|
}
|
|
|
|
|
|
|
|
var connectionsTableViewController: ConnectionsTableViewController!
|
|
|
|
|
|
|
|
/// Window to manage
|
|
|
|
let window: UIWindow
|
|
|
|
|
|
|
|
let navigationController: UINavigationController = {
|
|
|
|
let navController = UINavigationController()
|
|
|
|
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
|
|
|
|
if let connectionsTableViewController = self?.storyboard.instantiateViewController(type: ConnectionsTableViewController.self) {
|
|
|
|
self?.connectionsTableViewController = connectionsTableViewController
|
|
|
|
self?.connectionsTableViewController.viewContext = self?.persistentContainer.viewContext
|
|
|
|
self?.connectionsTableViewController.delegate = self
|
|
|
|
self?.navigationController.viewControllers = [connectionsTableViewController]
|
|
|
|
do {
|
|
|
|
if let context = self?.persistentContainer.viewContext, try Profile.countInContext(context) == 0 {
|
|
|
|
print("No profiles ... yet")
|
|
|
|
}
|
|
|
|
} 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension AppCoordinator: ConnectionsTableViewControllerDelegate {
|
|
|
|
func addProvider(connectionsTableViewController: ConnectionsTableViewController) {
|
|
|
|
// TODO implement
|
2018-05-24 18:14:01 +00:00
|
|
|
print("Add provider")
|
2018-05-23 19:49:10 +00:00
|
|
|
}
|
|
|
|
|
2018-05-24 18:14:01 +00:00
|
|
|
func connect(profile: Profile, connectionsTableViewController: ConnectionsTableViewController) {
|
2018-05-23 19:49:10 +00:00
|
|
|
// TODO implement
|
2018-05-24 18:14:01 +00:00
|
|
|
print("connect profile \(profile)")
|
2018-05-23 19:49:10 +00:00
|
|
|
}
|
|
|
|
|
2018-05-24 18:14:01 +00:00
|
|
|
func configure(profile: Profile, connectionsTableViewController: ConnectionsTableViewController) {
|
2018-05-23 19:49:10 +00:00
|
|
|
// TODO implement
|
2018-05-24 18:14:01 +00:00
|
|
|
print("configure profile \(profile)")
|
2018-05-23 19:49:10 +00:00
|
|
|
}
|
|
|
|
|
2018-05-24 18:14:01 +00:00
|
|
|
func delete(profile: Profile, connectionsTableViewController: ConnectionsTableViewController) {
|
2018-05-23 19:49:10 +00:00
|
|
|
// TODO implement
|
2018-05-24 18:14:01 +00:00
|
|
|
print("delete profile \(profile)")
|
2018-05-23 19:49:10 +00:00
|
|
|
}
|
|
|
|
}
|