Tunnel creation: After saving the new tunnel, show the detail view

Signed-off-by: Roopesh Chander <roop@roopc.net>
This commit is contained in:
Roopesh Chander 2018-10-24 17:09:34 +05:30
parent 0fad01107c
commit 37f43759b8
4 changed files with 32 additions and 9 deletions

View File

@ -19,11 +19,13 @@ class TunnelDetailTableViewController: UITableViewController {
] ]
let tunnelsManager: TunnelsManager let tunnelsManager: TunnelsManager
let tunnel: TunnelContainer
let tunnelViewModel: TunnelViewModel let tunnelViewModel: TunnelViewModel
init(tunnelsManager tm: TunnelsManager, tunnelConfiguration: TunnelConfiguration) { init(tunnelsManager tm: TunnelsManager, tunnel t: TunnelContainer) {
tunnelsManager = tm tunnelsManager = tm
tunnelViewModel = TunnelViewModel(tunnelConfiguration: tunnelConfiguration) tunnel = t
tunnelViewModel = TunnelViewModel(tunnelConfiguration: t.tunnelProvider.tunnelConfiguration)
super.init(style: .grouped) super.init(style: .grouped)
} }

View File

@ -3,10 +3,16 @@
import UIKit import UIKit
protocol TunnelEditTableViewControllerDelegate: class {
func saved(tunnel: TunnelContainer)
}
// MARK: TunnelEditTableViewController // MARK: TunnelEditTableViewController
class TunnelEditTableViewController: UITableViewController { class TunnelEditTableViewController: UITableViewController {
weak var delegate: TunnelEditTableViewControllerDelegate? = nil
let interfaceFieldsBySection: [[TunnelViewModel.InterfaceField]] = [ let interfaceFieldsBySection: [[TunnelViewModel.InterfaceField]] = [
[.name], [.name],
[.privateKey, .publicKey, .generateKeyPair], [.privateKey, .publicKey, .generateKeyPair],
@ -20,11 +26,13 @@ class TunnelEditTableViewController: UITableViewController {
] ]
let tunnelsManager: TunnelsManager let tunnelsManager: TunnelsManager
let tunnel: TunnelContainer?
let tunnelViewModel: TunnelViewModel let tunnelViewModel: TunnelViewModel
init(tunnelsManager tm: TunnelsManager) { init(tunnelsManager tm: TunnelsManager, tunnel t: TunnelContainer? = nil) {
tunnelsManager = tm tunnelsManager = tm
tunnelViewModel = TunnelViewModel(tunnelConfiguration: nil) tunnel = t
tunnelViewModel = TunnelViewModel(tunnelConfiguration: t?.tunnelProvider.tunnelConfiguration)
super.init(style: .grouped) super.init(style: .grouped)
self.modalPresentationStyle = .formSheet self.modalPresentationStyle = .formSheet
} }
@ -55,11 +63,12 @@ class TunnelEditTableViewController: UITableViewController {
let erroringConfiguration = (tunnelViewModel.interfaceData.validatedConfiguration == nil) ? "Interface" : "Peer" let erroringConfiguration = (tunnelViewModel.interfaceData.validatedConfiguration == nil) ? "Interface" : "Peer"
showErrorAlert(title: "Invalid \(erroringConfiguration)", message: errorMessage) showErrorAlert(title: "Invalid \(erroringConfiguration)", message: errorMessage)
case .saved(let tunnelConfiguration): case .saved(let tunnelConfiguration):
tunnelsManager.add(tunnelConfiguration: tunnelConfiguration) { [weak self] (error) in tunnelsManager.add(tunnelConfiguration: tunnelConfiguration) { [weak self] (tunnel, error) in
if let error = error { if let error = error {
print("Could not save: \(error)") print("Could not save: \(error)")
self?.showErrorAlert(title: "Could not save", message: "Internal error") self?.showErrorAlert(title: "Could not save", message: "Internal error")
} else { } else {
self?.delegate?.saved(tunnel: tunnel)
self?.dismiss(animated: true, completion: nil) self?.dismiss(animated: true, completion: nil)
} }
} }

View File

@ -41,6 +41,7 @@ class TunnelsListTableViewController: UITableViewController {
UIAlertAction(title: "Create from scratch", style: .default) { [weak self] (action) in UIAlertAction(title: "Create from scratch", style: .default) { [weak self] (action) in
if let s = self, let tunnelsManager = s.tunnelsManager { if let s = self, let tunnelsManager = s.tunnelsManager {
let editVC = TunnelEditTableViewController(tunnelsManager: tunnelsManager) let editVC = TunnelEditTableViewController(tunnelsManager: tunnelsManager)
editVC.delegate = s
let editNC = UINavigationController(rootViewController: editVC) let editNC = UINavigationController(rootViewController: editVC)
s.present(editNC, animated: true) s.present(editNC, animated: true)
} }
@ -55,6 +56,17 @@ class TunnelsListTableViewController: UITableViewController {
} }
} }
// MARK: TunnelEditTableViewControllerDelegate
extension TunnelsListTableViewController: TunnelEditTableViewControllerDelegate {
func saved(tunnel: TunnelContainer) {
guard let tunnelsManager = tunnelsManager else { return }
let tunnelDetailVC = TunnelDetailTableViewController(tunnelsManager: tunnelsManager,
tunnel: tunnel)
showDetailViewController(tunnelDetailVC, sender: self) // Shall get propagated up to the split-vc
}
}
// MARK: UITableViewDataSource // MARK: UITableViewDataSource
extension TunnelsListTableViewController { extension TunnelsListTableViewController {
@ -81,9 +93,9 @@ extension TunnelsListTableViewController {
extension TunnelsListTableViewController { extension TunnelsListTableViewController {
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let tunnelsManager = tunnelsManager else { return } guard let tunnelsManager = tunnelsManager else { return }
let tunnelConfiguration = tunnelsManager.tunnel(at: indexPath.row).tunnelProvider.tunnelConfiguration let tunnel = tunnelsManager.tunnel(at: indexPath.row)
let tunnelDetailVC = TunnelDetailTableViewController(tunnelsManager: tunnelsManager, let tunnelDetailVC = TunnelDetailTableViewController(tunnelsManager: tunnelsManager,
tunnelConfiguration: tunnelConfiguration) tunnel: tunnel)
showDetailViewController(tunnelDetailVC, sender: self) // Shall get propagated up to the split-vc showDetailViewController(tunnelDetailVC, sender: self) // Shall get propagated up to the split-vc
} }
} }

View File

@ -49,7 +49,7 @@ class TunnelsManager {
completionHandler(TunnelsManager(tunnelProviders: [])) completionHandler(TunnelsManager(tunnelProviders: []))
} }
func add(tunnelConfiguration: TunnelConfiguration, completionHandler: @escaping (Error?) -> Void) { func add(tunnelConfiguration: TunnelConfiguration, completionHandler: @escaping (TunnelContainer, Error?) -> Void) {
let tunnelProvider = TunnelProviderManager(tunnelConfiguration: tunnelConfiguration) let tunnelProvider = TunnelProviderManager(tunnelConfiguration: tunnelConfiguration)
for tunnel in tunnels { for tunnel in tunnels {
tunnel.index = tunnel.index + 1 tunnel.index = tunnel.index + 1
@ -57,7 +57,7 @@ class TunnelsManager {
let tunnel = TunnelContainer(tunnel: tunnelProvider, index: 0) let tunnel = TunnelContainer(tunnel: tunnelProvider, index: 0)
tunnels.insert(tunnel, at: 0) tunnels.insert(tunnel, at: 0)
delegate?.tunnelsAdded(atIndex: 0, numberOfTunnels: 1) delegate?.tunnelsAdded(atIndex: 0, numberOfTunnels: 1)
completionHandler(nil) completionHandler(tunnel, nil)
} }
func modify(tunnel: TunnelContainer, with tunnelConfiguration: TunnelConfiguration, completionHandler: @escaping (Error?) -> Void) { func modify(tunnel: TunnelContainer, with tunnelConfiguration: TunnelConfiguration, completionHandler: @escaping (Error?) -> Void) {