From 34cc14fefa4c45789b0d419bb5c0936dbe310318 Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Wed, 24 Oct 2018 17:19:14 +0530 Subject: [PATCH] Tunnel editing: Modifying a tunnel configuration --- .../iOS/TunnelDetailTableViewController.swift | 16 +++++++++-- .../iOS/TunnelEditTableViewController.swift | 28 ++++++++++++++----- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/WireGuard/WireGuard/UI/iOS/TunnelDetailTableViewController.swift b/WireGuard/WireGuard/UI/iOS/TunnelDetailTableViewController.swift index 87e1b62..18a2fe3 100644 --- a/WireGuard/WireGuard/UI/iOS/TunnelDetailTableViewController.swift +++ b/WireGuard/WireGuard/UI/iOS/TunnelDetailTableViewController.swift @@ -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() } } diff --git a/WireGuard/WireGuard/UI/iOS/TunnelEditTableViewController.swift b/WireGuard/WireGuard/UI/iOS/TunnelEditTableViewController.swift index 6be42b8..2543444 100644 --- a/WireGuard/WireGuard/UI/iOS/TunnelEditTableViewController.swift +++ b/WireGuard/WireGuard/UI/iOS/TunnelEditTableViewController.swift @@ -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) + } } } }