From 0b2f004f620145d09916ec5d86c631aaf4c0b9dc Mon Sep 17 00:00:00 2001 From: Jeroen Leenarts Date: Sun, 27 May 2018 22:38:44 +0200 Subject: [PATCH] Wire up TunnelConfigurationTableViewController. --- WireGuard/Base.lproj/Main.storyboard | 50 +++++++++- WireGuard/Coordinators/AppCoordinator.swift | 6 +- ...nnelConfigurationTableViewController.swift | 98 ++++++++++++++++++- 3 files changed, 147 insertions(+), 7 deletions(-) diff --git a/WireGuard/Base.lproj/Main.storyboard b/WireGuard/Base.lproj/Main.storyboard index be9ffce..9c0b04f 100644 --- a/WireGuard/Base.lproj/Main.storyboard +++ b/WireGuard/Base.lproj/Main.storyboard @@ -117,6 +117,9 @@ + + + @@ -137,10 +140,13 @@ + + + @@ -160,6 +166,9 @@ + + + @@ -180,6 +189,9 @@ + + + @@ -197,6 +209,9 @@ + + + @@ -219,6 +234,9 @@ + + + @@ -236,6 +254,9 @@ + + + @@ -267,6 +288,9 @@ @@ -322,6 +346,9 @@ + + + @@ -339,6 +366,9 @@ + + + @@ -356,6 +386,9 @@ + + + @@ -376,6 +409,9 @@ + + + @@ -393,6 +429,9 @@ + + + @@ -419,9 +458,16 @@ - + + + + + + + + diff --git a/WireGuard/Coordinators/AppCoordinator.swift b/WireGuard/Coordinators/AppCoordinator.swift index 1bc9ee6..f6209a1 100644 --- a/WireGuard/Coordinators/AppCoordinator.swift +++ b/WireGuard/Coordinators/AppCoordinator.swift @@ -112,8 +112,7 @@ extension AppCoordinator: TunnelsTableViewControllerDelegate { func showTunnelConfigurationViewController(tunnel: Tunnel?, context: NSManagedObjectContext) { let tunnelConfigurationViewController = storyboard.instantiateViewController(type: TunnelConfigurationTableViewController.self) - tunnelConfigurationViewController.viewContext = context - tunnelConfigurationViewController.delegate = self + tunnelConfigurationViewController.configure(context: context, delegate: self, tunnel: tunnel) self.navigationController.pushViewController(tunnelConfigurationViewController, animated: true) } @@ -125,5 +124,8 @@ extension AppCoordinator: TunnelsTableViewControllerDelegate { } extension AppCoordinator: TunnelConfigurationTableViewControllerDelegate { + func didSave(tunnel: Tunnel, tunnelConfigurationTableViewController: TunnelConfigurationTableViewController) { + navigationController.popToRootViewController(animated: true) + } } diff --git a/WireGuard/ViewControllers/TunnelConfigurationTableViewController.swift b/WireGuard/ViewControllers/TunnelConfigurationTableViewController.swift index 271b7ae..64ad85d 100644 --- a/WireGuard/ViewControllers/TunnelConfigurationTableViewController.swift +++ b/WireGuard/ViewControllers/TunnelConfigurationTableViewController.swift @@ -9,13 +9,41 @@ import UIKit import CoreData import BNRCoreDataStack +import PromiseKit protocol TunnelConfigurationTableViewControllerDelegate: class { + func didSave(tunnel: Tunnel, tunnelConfigurationTableViewController: TunnelConfigurationTableViewController) } class TunnelConfigurationTableViewController: UITableViewController { - var viewContext: NSManagedObjectContext! - weak var delegate: TunnelConfigurationTableViewControllerDelegate? + + @IBOutlet weak var saveButton: UIBarButtonItem! + + private var viewContext: NSManagedObjectContext! + private weak var delegate: TunnelConfigurationTableViewControllerDelegate? + private var tunnel: Tunnel! + + func configure(context: NSManagedObjectContext, delegate: TunnelConfigurationTableViewControllerDelegate? = nil, tunnel: Tunnel? = nil) { + viewContext = context + self.delegate = delegate + self.tunnel = tunnel ?? generateNewTunnelConfig() + + } + + private func generateNewTunnelConfig() -> Tunnel { + var tunnel: Tunnel! = nil + + viewContext.performAndWait { + tunnel = Tunnel(context: viewContext) + tunnel.addToPeers(Peer(context: viewContext)) + + let interface = Interface(context: viewContext) + interface.addToAdresses(Address(context: viewContext)) + + tunnel.interface = interface + } + return tunnel + } override func numberOfSections(in tableView: UITableView) -> Int { return 3 @@ -24,7 +52,7 @@ class TunnelConfigurationTableViewController: UITableViewController { override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { switch section { case 1: - return 2 + return tunnel?.peers?.count ?? 1 default: return 1 } @@ -40,17 +68,81 @@ class TunnelConfigurationTableViewController: UITableViewController { return tableView.dequeueReusableCell(type: AddPeerTableViewCell.self, for: indexPath) } } + + @IBAction func saveTunnelConfiguration(_ sender: Any) { + Promise(resolver: { (seal) in + viewContext.perform({ + self.viewContext.saveContext({ (result) in + switch result { + case .success: + seal.fulfill(()) + case .failure(let error): + seal.reject(error) + } + }) + }) + }).then { () -> Promise in + self.delegate?.didSave(tunnel: self.tunnel, tunnelConfigurationTableViewController: self) + return Promise.value(()) + }.catch { error in + print("Error saving: \(error)") + } + } } class InterfaceTableViewCell: UITableViewCell { + var model: Interface! + @IBOutlet weak var nameField: UITextField! + @IBOutlet weak var privateKeyField: UITextField! + @IBOutlet weak var publicKeyField: UITextField! + @IBOutlet weak var addressesField: UITextField! + @IBOutlet weak var listenPortField: UITextField! + @IBOutlet weak var dnsField: UITextField! + @IBOutlet weak var mtuField: UITextField! + +} + +extension InterfaceTableViewCell: UITextFieldDelegate { + func textFieldDidBeginEditing(_ textField: UITextField) { + print("\(textField) \(textField.text)") + } + + func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { + print("\(string)") + return true + } } class PeerTableViewCell: UITableViewCell { + var peer: Peer! + + @IBOutlet weak var publicKeyField: UITextField! + @IBOutlet weak var preSharedKeyField: UITextField! + @IBOutlet weak var allowedIpsField: UITextField! + @IBOutlet weak var endpointField: UITextField! + @IBOutlet weak var persistentKeepaliveField: UITextField! } +extension PeerTableViewCell: UITextFieldDelegate { + func textFieldDidBeginEditing(_ textField: UITextField) { + print("\(textField) \(textField.text)") + } + + func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { + print("\(string)") + return true + } +} + class AddPeerTableViewCell: UITableViewCell { + var model: Interface? + + @IBAction func addPeer(_ sender: Any) { + //TODO implement + print("Implement add peer") + } }