Allow adding and deleting peers.

This commit is contained in:
Jeroen Leenarts 2018-05-29 22:47:52 +02:00
parent 42f278ff49
commit 25bcb708ae
2 changed files with 44 additions and 20 deletions

View File

@ -305,7 +305,7 @@
<rect key="frame" x="0.0" y="0.0" width="375" height="59.5"/> <rect key="frame" x="0.0" y="0.0" width="375" height="59.5"/>
<state key="normal" title="Add peer"/> <state key="normal" title="Add peer"/>
<connections> <connections>
<action selector="addPeer:" destination="RyR-s5-lBV" eventType="touchUpInside" id="Aug-zc-lQ6"/> <action selector="addPeer:" destination="0VM-73-EPX" eventType="touchUpInside" id="Udv-Os-ZWn"/>
</connections> </connections>
</button> </button>
</subviews> </subviews>
@ -345,6 +345,9 @@
<constraint firstAttribute="width" constant="40" id="Sj9-zg-bBf"/> <constraint firstAttribute="width" constant="40" id="Sj9-zg-bBf"/>
</constraints> </constraints>
<state key="normal" image="trash"/> <state key="normal" image="trash"/>
<connections>
<action selector="deletePeer:" destination="gzz-88-0IG" eventType="touchUpInside" id="PIg-AX-df7"/>
</connections>
</button> </button>
</subviews> </subviews>
</stackView> </stackView>

View File

@ -45,6 +45,17 @@ class TunnelConfigurationTableViewController: UITableViewController {
return tunnel return tunnel
} }
@IBAction func addPeer(_ sender: Any) {
if let moc = tunnel.managedObjectContext {
tableView.beginUpdates()
let insertedAt = IndexPath(row: tunnel.peers?.count ?? 0, section: 1)
tableView.insertRows(at: [insertedAt], with: .automatic)
tunnel.addToPeers(Peer(context: moc))
tableView.endUpdates()
tableView.scrollToRow(at: insertedAt, at: .middle, animated: true)
}
}
override func numberOfSections(in tableView: UITableView) -> Int { override func numberOfSections(in tableView: UITableView) -> Int {
return 3 return 3
} }
@ -73,9 +84,12 @@ class TunnelConfigurationTableViewController: UITableViewController {
tunnel.addToPeers(peer) tunnel.addToPeers(peer)
cell.peer = peer cell.peer = peer
} }
cell.delegate = self
return cell return cell
default: default:
return tableView.dequeueReusableCell(type: AddPeerTableViewCell.self, for: indexPath) let cell = tableView.dequeueReusableCell(type: AddPeerTableViewCell.self, for: indexPath)
cell.tunnel = tunnel
return cell
} }
} }
@ -100,6 +114,19 @@ class TunnelConfigurationTableViewController: UITableViewController {
} }
} }
extension TunnelConfigurationTableViewController: PeerTableViewCellDelegate {
func delete(peer: Peer) {
if let moc = tunnel.managedObjectContext {
tableView.beginUpdates()
let deletedAt = IndexPath(row: tunnel.peers?.index(of: peer) ?? 0, section: 1)
tableView.deleteRows(at: [deletedAt], with: .automatic)
tunnel.removeFromPeers(peer)
moc.delete(peer)
tableView.endUpdates()
}
}
}
class InterfaceTableViewCell: UITableViewCell { class InterfaceTableViewCell: UITableViewCell {
var model: Interface! var model: Interface!
@ -117,7 +144,6 @@ extension InterfaceTableViewCell: UITextFieldDelegate {
@IBAction @IBAction
func textfieldDidChange(_ sender: UITextField) { func textfieldDidChange(_ sender: UITextField) {
let string = sender.text let string = sender.text
print(string)
if sender == nameField { if sender == nameField {
model.tunnel?.title = string model.tunnel?.title = string
@ -142,16 +168,15 @@ extension InterfaceTableViewCell: UITextFieldDelegate {
} }
} }
} }
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { protocol PeerTableViewCellDelegate: class {
print("\(string)") func delete(peer: Peer)
return true
}
} }
class PeerTableViewCell: UITableViewCell { class PeerTableViewCell: UITableViewCell {
var peer: Peer! var peer: Peer!
weak var delegate: PeerTableViewCellDelegate?
@IBOutlet weak var publicKeyField: UITextField! @IBOutlet weak var publicKeyField: UITextField!
@IBOutlet weak var preSharedKeyField: UITextField! @IBOutlet weak var preSharedKeyField: UITextField!
@ -159,13 +184,15 @@ class PeerTableViewCell: UITableViewCell {
@IBOutlet weak var endpointField: UITextField! @IBOutlet weak var endpointField: UITextField!
@IBOutlet weak var persistentKeepaliveField: UITextField! @IBOutlet weak var persistentKeepaliveField: UITextField!
@IBAction func deletePeer(_ sender: Any) {
delegate?.delete(peer: peer)
}
} }
extension PeerTableViewCell: UITextFieldDelegate { extension PeerTableViewCell: UITextFieldDelegate {
@IBAction @IBAction
func textfieldDidChange(_ sender: UITextField) { func textfieldDidChange(_ sender: UITextField) {
let string = sender.text let string = sender.text
print(string)
if sender == publicKeyField { if sender == publicKeyField {
peer.publicKey = string peer.publicKey = string
@ -181,22 +208,16 @@ extension PeerTableViewCell: UITextFieldDelegate {
} }
} }
} }
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
print("\(string)")
return true
}
} }
class AddPeerTableViewCell: UITableViewCell { class AddPeerTableViewCell: UITableViewCell {
var model: Interface? var tunnel: Tunnel!
@IBAction func addPeer(_ sender: Any) { @IBAction func addPeer(_ sender: Any) {
//TODO implement if let moc = tunnel.managedObjectContext {
print("Implement add peer") tunnel.addToPeers(Peer(context: moc))
}
} }
} }
extension TunnelConfigurationTableViewController: Identifyable {} extension TunnelConfigurationTableViewController: Identifyable {}