From 0fad01107cafc661febce17468eedcb28b1b79ed Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Wed, 24 Oct 2018 15:25:30 +0530 Subject: [PATCH] Tunnel detail: Don't show fields that have empty value Signed-off-by: Roopesh Chander --- WireGuard/WireGuard/UI/TunnelViewModel.swift | 28 +++++++++++++++ .../iOS/TunnelDetailTableViewController.swift | 34 +++++++++++++------ 2 files changed, 52 insertions(+), 10 deletions(-) diff --git a/WireGuard/WireGuard/UI/TunnelViewModel.swift b/WireGuard/WireGuard/UI/TunnelViewModel.swift index e7ab12d..cb678d1 100644 --- a/WireGuard/WireGuard/UI/TunnelViewModel.swift +++ b/WireGuard/WireGuard/UI/TunnelViewModel.swift @@ -17,6 +17,10 @@ class TunnelViewModel { case dns = "DNS servers" } + static let interfaceFieldsWithControl: Set = [ + .generateKeyPair, .copyPublicKey + ] + enum PeerField: String { case publicKey = "Public key" case preSharedKey = "Pre-shared key" @@ -27,6 +31,10 @@ class TunnelViewModel { case deletePeer = "Delete peer" } + static let peerFieldsWithControl: Set = [ + .excludePrivateIPs, .deletePeer + ] + static let keyLengthInBase64 = 44 class InterfaceData { @@ -153,6 +161,16 @@ class TunnelViewModel { validatedConfiguration = config return .saved(config) } + + func filterFieldsWithValueOrControl(interfaceFields: [InterfaceField]) -> [InterfaceField] { + return interfaceFields.filter { (field) -> Bool in + if (TunnelViewModel.interfaceFieldsWithControl.contains(field)) { + return true + } + return (!self[field].isEmpty) + } + // TODO: Cache this to avoid recomputing + } } class PeerData { @@ -263,6 +281,16 @@ class TunnelViewModel { validatedConfiguration = config return .saved(config) } + + func filterFieldsWithValueOrControl(peerFields: [PeerField]) -> [PeerField] { + return peerFields.filter { (field) -> Bool in + if (TunnelViewModel.peerFieldsWithControl.contains(field)) { + return true + } + return (!self[field].isEmpty) + } + // TODO: Cache this to avoid recomputing + } } enum SaveResult { diff --git a/WireGuard/WireGuard/UI/iOS/TunnelDetailTableViewController.swift b/WireGuard/WireGuard/UI/iOS/TunnelDetailTableViewController.swift index 0a1cb4d..69364b6 100644 --- a/WireGuard/WireGuard/UI/iOS/TunnelDetailTableViewController.swift +++ b/WireGuard/WireGuard/UI/iOS/TunnelDetailTableViewController.swift @@ -50,7 +50,10 @@ class TunnelDetailTableViewController: UITableViewController { extension TunnelDetailTableViewController { override func numberOfSections(in tableView: UITableView) -> Int { - let numberOfInterfaceSections = interfaceFieldsBySection.count + let interfaceData = tunnelViewModel.interfaceData + let numberOfInterfaceSections = (0 ..< interfaceFieldsBySection.count).filter { section in + (!interfaceData.filterFieldsWithValueOrControl(interfaceFields: interfaceFieldsBySection[section]).isEmpty) + }.count let numberOfPeerSections = peerFieldsBySection.count let numberOfPeers = tunnelViewModel.peersData.count @@ -58,17 +61,22 @@ extension TunnelDetailTableViewController { } override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { - let numberOfInterfaceSections = interfaceFieldsBySection.count + let interfaceData = tunnelViewModel.interfaceData + let numberOfInterfaceSections = (0 ..< interfaceFieldsBySection.count).filter { section in + (!interfaceData.filterFieldsWithValueOrControl(interfaceFields: interfaceFieldsBySection[section]).isEmpty) + }.count let numberOfPeerSections = peerFieldsBySection.count let numberOfPeers = tunnelViewModel.peersData.count if (section < numberOfInterfaceSections) { // Interface - return interfaceFieldsBySection[section].count + return interfaceData.filterFieldsWithValueOrControl(interfaceFields: interfaceFieldsBySection[section]).count } else if ((numberOfPeers > 0) && (section < (numberOfInterfaceSections + numberOfPeers * numberOfPeerSections))) { // Peer - let fieldIndex = (section - numberOfInterfaceSections) % numberOfPeerSections - return peerFieldsBySection[fieldIndex].count + let peerIndex = Int((section - numberOfInterfaceSections) / numberOfPeerSections) + let peerData = tunnelViewModel.peersData[peerIndex] + let peerSectionIndex = (section - numberOfInterfaceSections) % numberOfPeerSections + return peerData.filterFieldsWithValueOrControl(peerFields: peerFieldsBySection[peerSectionIndex]).count } else { // Add peer return 1 @@ -76,7 +84,10 @@ extension TunnelDetailTableViewController { } override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { - let numberOfInterfaceSections = interfaceFieldsBySection.count + let interfaceData = tunnelViewModel.interfaceData + let numberOfInterfaceSections = (0 ..< interfaceFieldsBySection.count).filter { section in + (!interfaceData.filterFieldsWithValueOrControl(interfaceFields: interfaceFieldsBySection[section]).isEmpty) + }.count let numberOfPeerSections = peerFieldsBySection.count let numberOfPeers = tunnelViewModel.peersData.count @@ -94,7 +105,10 @@ extension TunnelDetailTableViewController { } override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { - let numberOfInterfaceSections = interfaceFieldsBySection.count + let interfaceData = tunnelViewModel.interfaceData + let numberOfInterfaceSections = (0 ..< interfaceFieldsBySection.count).filter { section in + (!interfaceData.filterFieldsWithValueOrControl(interfaceFields: interfaceFieldsBySection[section]).isEmpty) + }.count let numberOfPeerSections = peerFieldsBySection.count let numberOfPeers = tunnelViewModel.peersData.count @@ -103,8 +117,7 @@ extension TunnelDetailTableViewController { if (section < numberOfInterfaceSections) { // Interface - let interfaceData = tunnelViewModel.interfaceData - let field = interfaceFieldsBySection[section][row] + let field = interfaceData.filterFieldsWithValueOrControl(interfaceFields: interfaceFieldsBySection[section])[row] if (field == .copyPublicKey) { let cell = tableView.dequeueReusableCell(withIdentifier: TunnelDetailTableViewButtonCell.id, for: indexPath) as! TunnelDetailTableViewButtonCell cell.buttonText = field.rawValue @@ -129,7 +142,8 @@ extension TunnelDetailTableViewController { let peerIndex = Int((section - numberOfInterfaceSections) / numberOfPeerSections) let peerSectionIndex = (section - numberOfInterfaceSections) % numberOfPeerSections let peerData = tunnelViewModel.peersData[peerIndex] - let field = peerFieldsBySection[peerSectionIndex][row] + let field = peerData.filterFieldsWithValueOrControl(peerFields: peerFieldsBySection[peerSectionIndex])[row] + let cell = tableView.dequeueReusableCell(withIdentifier: TunnelDetailTableViewKeyValueCell.id, for: indexPath) as! TunnelDetailTableViewKeyValueCell // Set key and value cell.key = field.rawValue