Tunnel creation: Saving a configuration
Signed-off-by: Roopesh Chander <roop@roopc.net>
This commit is contained in:
parent
722b3f6c7b
commit
05efb453f3
|
@ -25,9 +25,11 @@ class TunnelEditTableViewController: UITableViewController {
|
||||||
.deletePeer]
|
.deletePeer]
|
||||||
]
|
]
|
||||||
|
|
||||||
|
let tunnelsManager: TunnelsManager
|
||||||
let tunnelViewModel: TunnelViewModel
|
let tunnelViewModel: TunnelViewModel
|
||||||
|
|
||||||
init() {
|
init(tunnelsManager tm: TunnelsManager) {
|
||||||
|
tunnelsManager = tm
|
||||||
tunnelViewModel = TunnelViewModel(tunnelConfiguration: nil)
|
tunnelViewModel = TunnelViewModel(tunnelConfiguration: nil)
|
||||||
super.init(style: .grouped)
|
super.init(style: .grouped)
|
||||||
self.modalPresentationStyle = .formSheet
|
self.modalPresentationStyle = .formSheet
|
||||||
|
@ -52,12 +54,35 @@ class TunnelEditTableViewController: UITableViewController {
|
||||||
}
|
}
|
||||||
|
|
||||||
@objc func saveTapped() {
|
@objc func saveTapped() {
|
||||||
print("Save")
|
self.tableView.endEditing(false)
|
||||||
|
let tunnelSaveResult = tunnelViewModel.save()
|
||||||
|
switch (tunnelSaveResult) {
|
||||||
|
case .error(let errorMessage):
|
||||||
|
let erroringConfiguration = (tunnelViewModel.interfaceData.validatedConfiguration == nil) ? "Interface" : "Peer"
|
||||||
|
showErrorAlert(title: "Invalid \(erroringConfiguration)", message: errorMessage)
|
||||||
|
case .saved(let tunnelConfiguration):
|
||||||
|
tunnelsManager.add(tunnelConfiguration: tunnelConfiguration) { [weak self] (error) in
|
||||||
|
if let error = error {
|
||||||
|
print("Could not save: \(error)")
|
||||||
|
self?.showErrorAlert(title: "Could not save", message: "Internal error")
|
||||||
|
} else {
|
||||||
|
self?.dismiss(animated: true, completion: nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@objc func cancelTapped() {
|
@objc func cancelTapped() {
|
||||||
dismiss(animated: true, completion: nil)
|
dismiss(animated: true, completion: nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func showErrorAlert(title: String, message: String) {
|
||||||
|
let okAction = UIAlertAction(title: "Ok", style: .default)
|
||||||
|
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
|
||||||
|
alert.addAction(okAction)
|
||||||
|
|
||||||
|
self.present(alert, animated: true, completion: nil)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: UITableViewDataSource
|
// MARK: UITableViewDataSource
|
||||||
|
|
|
@ -40,11 +40,11 @@ class TunnelsListTableViewController: UITableViewController {
|
||||||
message: "Add a tunnel",
|
message: "Add a tunnel",
|
||||||
preferredStyle: .actionSheet)
|
preferredStyle: .actionSheet)
|
||||||
alert.addAction(
|
alert.addAction(
|
||||||
UIAlertAction(title: "Create from scratch", style: .default) { (action) in
|
UIAlertAction(title: "Create from scratch", style: .default) { [weak self] (action) in
|
||||||
let editVC = TunnelEditTableViewController()
|
if let s = self, let tunnelsManager = s.tunnelsManager {
|
||||||
|
let editVC = TunnelEditTableViewController(tunnelsManager: tunnelsManager)
|
||||||
let editNC = UINavigationController(rootViewController: editVC)
|
let editNC = UINavigationController(rootViewController: editVC)
|
||||||
self.present(editNC, animated: true) {
|
s.present(editNC, animated: true)
|
||||||
print("Done")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue