Tunnel editing: Modifying a tunnel configuration

Signed-off-by: Roopesh Chander <roop@roopc.net>
This commit is contained in:
Roopesh Chander 2018-10-24 17:19:14 +05:30
parent 37f43759b8
commit 06c84a22ca
2 changed files with 35 additions and 9 deletions

View File

@ -20,7 +20,7 @@ class TunnelDetailTableViewController: UITableViewController {
let tunnelsManager: TunnelsManager
let tunnel: TunnelContainer
let tunnelViewModel: TunnelViewModel
var tunnelViewModel: TunnelViewModel
init(tunnelsManager tm: TunnelsManager, tunnel t: TunnelContainer) {
tunnelsManager = tm
@ -44,7 +44,19 @@ class TunnelDetailTableViewController: UITableViewController {
}
@objc func editTapped() {
print("Edit")
let editVC = TunnelEditTableViewController(tunnelsManager: tunnelsManager, tunnel: tunnel)
editVC.delegate = self
let editNC = UINavigationController(rootViewController: editVC)
present(editNC, animated: true)
}
}
// MARK: TunnelEditTableViewControllerDelegate
extension TunnelDetailTableViewController: TunnelEditTableViewControllerDelegate {
func saved(tunnel: TunnelContainer) {
tunnelViewModel = TunnelViewModel(tunnelConfiguration: tunnel.tunnelProvider.tunnelConfiguration)
self.tableView.reloadData()
}
}

View File

@ -63,13 +63,27 @@ class TunnelEditTableViewController: UITableViewController {
let erroringConfiguration = (tunnelViewModel.interfaceData.validatedConfiguration == nil) ? "Interface" : "Peer"
showErrorAlert(title: "Invalid \(erroringConfiguration)", message: errorMessage)
case .saved(let tunnelConfiguration):
tunnelsManager.add(tunnelConfiguration: tunnelConfiguration) { [weak self] (tunnel, error) in
if let error = error {
print("Could not save: \(error)")
self?.showErrorAlert(title: "Could not save", message: "Internal error")
} else {
self?.delegate?.saved(tunnel: tunnel)
self?.dismiss(animated: true, completion: nil)
if let tunnel = tunnel {
// We're modifying an existing tunnel
tunnelsManager.modify(tunnel: tunnel, with: tunnelConfiguration) { [weak self] (error) in
if let error = error {
print("Could not modify tunnel: \(error)")
self?.showErrorAlert(title: "Could not save", message: "Internal error")
} else {
self?.delegate?.saved(tunnel: tunnel)
self?.dismiss(animated: true, completion: nil)
}
}
} else {
// We're adding a new tunnel
tunnelsManager.add(tunnelConfiguration: tunnelConfiguration) { [weak self] (tunnel, error) in
if let error = error {
print("Could not add tunnel: \(error)")
self?.showErrorAlert(title: "Could not save", message: "Internal error")
} else {
self?.delegate?.saved(tunnel: tunnel)
self?.dismiss(animated: true, completion: nil)
}
}
}
}