Tunnel creation: Saving a configuration

This commit is contained in:
Roopesh Chander 2018-10-23 17:23:46 +05:30
parent e0ee01db78
commit 0324cebe2b
2 changed files with 32 additions and 7 deletions

View File

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

View File

@ -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 editNC = UINavigationController(rootViewController: editVC) let editVC = TunnelEditTableViewController(tunnelsManager: tunnelsManager)
self.present(editNC, animated: true) { let editNC = UINavigationController(rootViewController: editVC)
print("Done") s.present(editNC, animated: true)
} }
} }
) )