From 61cf3f8a5b2734ea02d9b50ea23956b3248df334 Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Wed, 24 Oct 2018 17:09:34 +0530 Subject: [PATCH] Tunnel creation: After saving the new tunnel, show the detail view --- .../UI/iOS/TunnelDetailTableViewController.swift | 6 ++++-- .../UI/iOS/TunnelEditTableViewController.swift | 15 ++++++++++++--- .../UI/iOS/TunnelsListTableViewController.swift | 16 ++++++++++++++-- WireGuard/WireGuard/VPN/TunnelsManager.swift | 4 ++-- 4 files changed, 32 insertions(+), 9 deletions(-) diff --git a/WireGuard/WireGuard/UI/iOS/TunnelDetailTableViewController.swift b/WireGuard/WireGuard/UI/iOS/TunnelDetailTableViewController.swift index 69364b6..87e1b62 100644 --- a/WireGuard/WireGuard/UI/iOS/TunnelDetailTableViewController.swift +++ b/WireGuard/WireGuard/UI/iOS/TunnelDetailTableViewController.swift @@ -19,11 +19,13 @@ class TunnelDetailTableViewController: UITableViewController { ] let tunnelsManager: TunnelsManager + let tunnel: TunnelContainer let tunnelViewModel: TunnelViewModel - init(tunnelsManager tm: TunnelsManager, tunnelConfiguration: TunnelConfiguration) { + init(tunnelsManager tm: TunnelsManager, tunnel t: TunnelContainer) { tunnelsManager = tm - tunnelViewModel = TunnelViewModel(tunnelConfiguration: tunnelConfiguration) + tunnel = t + tunnelViewModel = TunnelViewModel(tunnelConfiguration: t.tunnelProvider.tunnelConfiguration) super.init(style: .grouped) } diff --git a/WireGuard/WireGuard/UI/iOS/TunnelEditTableViewController.swift b/WireGuard/WireGuard/UI/iOS/TunnelEditTableViewController.swift index e486706..6be42b8 100644 --- a/WireGuard/WireGuard/UI/iOS/TunnelEditTableViewController.swift +++ b/WireGuard/WireGuard/UI/iOS/TunnelEditTableViewController.swift @@ -3,10 +3,16 @@ import UIKit +protocol TunnelEditTableViewControllerDelegate: class { + func saved(tunnel: TunnelContainer) +} + // MARK: TunnelEditTableViewController class TunnelEditTableViewController: UITableViewController { + weak var delegate: TunnelEditTableViewControllerDelegate? = nil + let interfaceFieldsBySection: [[TunnelViewModel.InterfaceField]] = [ [.name], [.privateKey, .publicKey, .generateKeyPair], @@ -20,11 +26,13 @@ class TunnelEditTableViewController: UITableViewController { ] let tunnelsManager: TunnelsManager + let tunnel: TunnelContainer? let tunnelViewModel: TunnelViewModel - init(tunnelsManager tm: TunnelsManager) { + init(tunnelsManager tm: TunnelsManager, tunnel t: TunnelContainer? = nil) { tunnelsManager = tm - tunnelViewModel = TunnelViewModel(tunnelConfiguration: nil) + tunnel = t + tunnelViewModel = TunnelViewModel(tunnelConfiguration: t?.tunnelProvider.tunnelConfiguration) super.init(style: .grouped) self.modalPresentationStyle = .formSheet } @@ -55,11 +63,12 @@ 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] (error) in + 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) } } diff --git a/WireGuard/WireGuard/UI/iOS/TunnelsListTableViewController.swift b/WireGuard/WireGuard/UI/iOS/TunnelsListTableViewController.swift index ec8d381..abbf0ac 100644 --- a/WireGuard/WireGuard/UI/iOS/TunnelsListTableViewController.swift +++ b/WireGuard/WireGuard/UI/iOS/TunnelsListTableViewController.swift @@ -41,6 +41,7 @@ class TunnelsListTableViewController: UITableViewController { UIAlertAction(title: "Create from scratch", style: .default) { [weak self] (action) in if let s = self, let tunnelsManager = s.tunnelsManager { let editVC = TunnelEditTableViewController(tunnelsManager: tunnelsManager) + editVC.delegate = s let editNC = UINavigationController(rootViewController: editVC) 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 extension TunnelsListTableViewController { @@ -81,9 +93,9 @@ extension TunnelsListTableViewController { extension TunnelsListTableViewController { override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 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, - tunnelConfiguration: tunnelConfiguration) + tunnel: tunnel) showDetailViewController(tunnelDetailVC, sender: self) // Shall get propagated up to the split-vc } } diff --git a/WireGuard/WireGuard/VPN/TunnelsManager.swift b/WireGuard/WireGuard/VPN/TunnelsManager.swift index 936f59f..704506a 100644 --- a/WireGuard/WireGuard/VPN/TunnelsManager.swift +++ b/WireGuard/WireGuard/VPN/TunnelsManager.swift @@ -49,7 +49,7 @@ class TunnelsManager { 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) for tunnel in tunnels { tunnel.index = tunnel.index + 1 @@ -57,7 +57,7 @@ class TunnelsManager { let tunnel = TunnelContainer(tunnel: tunnelProvider, index: 0) tunnels.insert(tunnel, at: 0) delegate?.tunnelsAdded(atIndex: 0, numberOfTunnels: 1) - completionHandler(nil) + completionHandler(tunnel, nil) } func modify(tunnel: TunnelContainer, with tunnelConfiguration: TunnelConfiguration, completionHandler: @escaping (Error?) -> Void) {