Wire up TunnelConfigurationTableViewController.

This commit is contained in:
Jeroen Leenarts 2018-05-27 22:38:44 +02:00
parent a59215110b
commit 0b2f004f62
3 changed files with 147 additions and 7 deletions

View File

@ -117,6 +117,9 @@
<nil key="textColor"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits"/>
<connections>
<outlet property="delegate" destination="06N-KU-LSv" id="TuA-ac-TIy"/>
</connections>
</textField>
</subviews>
</stackView>
@ -137,10 +140,13 @@
<nil key="textColor"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits"/>
<connections>
<outlet property="delegate" destination="06N-KU-LSv" id="7YF-mU-HZT"/>
</connections>
</textField>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="C1M-bh-5mf">
<rect key="frame" x="240" y="0.0" width="103" height="30"/>
<state key="normal" title="Button"/>
<state key="normal" title="Generate"/>
</button>
</subviews>
</stackView>
@ -160,6 +166,9 @@
<nil key="textColor"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits"/>
<connections>
<outlet property="delegate" destination="06N-KU-LSv" id="apT-gO-mcr"/>
</connections>
</textField>
</subviews>
</stackView>
@ -180,6 +189,9 @@
<nil key="textColor"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits"/>
<connections>
<outlet property="delegate" destination="06N-KU-LSv" id="CUZ-nd-ETQ"/>
</connections>
</textField>
</subviews>
</stackView>
@ -197,6 +209,9 @@
<nil key="textColor"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits"/>
<connections>
<outlet property="delegate" destination="06N-KU-LSv" id="YZ0-LC-PjW"/>
</connections>
</textField>
</subviews>
</stackView>
@ -219,6 +234,9 @@
<nil key="textColor"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits"/>
<connections>
<outlet property="delegate" destination="06N-KU-LSv" id="YjV-8W-z5Q"/>
</connections>
</textField>
</subviews>
</stackView>
@ -236,6 +254,9 @@
<nil key="textColor"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits"/>
<connections>
<outlet property="delegate" destination="06N-KU-LSv" id="F1E-lK-AsD"/>
</connections>
</textField>
</subviews>
</stackView>
@ -267,6 +288,9 @@
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="fVu-Aa-9dn">
<rect key="frame" x="0.0" y="0.0" width="375" height="59.5"/>
<state key="normal" title="Add peer"/>
<connections>
<action selector="addPeer:" destination="RyR-s5-lBV" eventType="touchUpInside" id="Aug-zc-lQ6"/>
</connections>
</button>
</subviews>
<constraints>
@ -322,6 +346,9 @@
<nil key="textColor"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits"/>
<connections>
<outlet property="delegate" destination="gzz-88-0IG" id="bkQ-Nc-VH4"/>
</connections>
</textField>
</subviews>
</stackView>
@ -339,6 +366,9 @@
<nil key="textColor"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits"/>
<connections>
<outlet property="delegate" destination="gzz-88-0IG" id="oAu-VZ-2dn"/>
</connections>
</textField>
</subviews>
</stackView>
@ -356,6 +386,9 @@
<nil key="textColor"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits"/>
<connections>
<outlet property="delegate" destination="gzz-88-0IG" id="DXo-0s-9sc"/>
</connections>
</textField>
</subviews>
</stackView>
@ -376,6 +409,9 @@
<nil key="textColor"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits"/>
<connections>
<outlet property="delegate" destination="gzz-88-0IG" id="PLB-sa-SBV"/>
</connections>
</textField>
</subviews>
</stackView>
@ -393,6 +429,9 @@
<nil key="textColor"/>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits"/>
<connections>
<outlet property="delegate" destination="gzz-88-0IG" id="sbH-Sb-rcW"/>
</connections>
</textField>
</subviews>
</stackView>
@ -419,9 +458,16 @@
</connections>
</tableView>
<navigationItem key="navigationItem" title="Tunnel settings" id="PPu-rX-T38">
<barButtonItem key="rightBarButtonItem" systemItem="save" id="mft-1l-bWa"/>
<barButtonItem key="rightBarButtonItem" systemItem="save" id="mft-1l-bWa">
<connections>
<action selector="saveTunnelConfiguration:" destination="0VM-73-EPX" id="Hni-0w-hFF"/>
</connections>
</barButtonItem>
</navigationItem>
<simulatedNavigationBarMetrics key="simulatedTopBarMetrics" prompted="NO"/>
<connections>
<outlet property="saveButton" destination="mft-1l-bWa" id="Uif-KZ-isx"/>
</connections>
</tableViewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="j96-PK-ghN" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>

View File

@ -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)
}
}

View File

@ -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<Void>(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<Void> 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")
}
}