From 01604dd8d14de79689a7d3fd454f41c4e0d2452f Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Tue, 5 Mar 2019 19:25:17 +0530 Subject: on-demand: iOS: Tunnel detail: Show SSID info Signed-off-by: Roopesh Chander --- .../TunnelDetailTableViewController.swift | 40 +++++++++++++++++++--- .../TunnelEditTableViewController.swift | 5 ++- 2 files changed, 37 insertions(+), 8 deletions(-) (limited to 'WireGuard/WireGuard/UI/iOS/ViewController') diff --git a/WireGuard/WireGuard/UI/iOS/ViewController/TunnelDetailTableViewController.swift b/WireGuard/WireGuard/UI/iOS/ViewController/TunnelDetailTableViewController.swift index 1f1a24f..19d66e7 100644 --- a/WireGuard/WireGuard/UI/iOS/ViewController/TunnelDetailTableViewController.swift +++ b/WireGuard/WireGuard/UI/iOS/ViewController/TunnelDetailTableViewController.swift @@ -24,21 +24,28 @@ class TunnelDetailTableViewController: UITableViewController { .rxBytes, .txBytes, .lastHandshakeTime ] + static let onDemandFields: [ActivateOnDemandViewModel.OnDemandField] = [ + .onDemand, .ssid + ] + let tunnelsManager: TunnelsManager let tunnel: TunnelContainer var tunnelViewModel: TunnelViewModel + var onDemandViewModel: ActivateOnDemandViewModel private var sections = [Section]() private var interfaceFieldIsVisible = [Bool]() private var peerFieldIsVisible = [[Bool]]() private var statusObservationToken: AnyObject? + private var onDemandObservationToken: AnyObject? private var reloadRuntimeConfigurationTimer: Timer? init(tunnelsManager: TunnelsManager, tunnel: TunnelContainer) { self.tunnelsManager = tunnelsManager self.tunnel = tunnel tunnelViewModel = TunnelViewModel(tunnelConfiguration: tunnel.tunnelConfiguration) + onDemandViewModel = ActivateOnDemandViewModel(setting: tunnel.activateOnDemandSetting) super.init(style: .grouped) loadSections() loadVisibleFields() @@ -51,6 +58,11 @@ class TunnelDetailTableViewController: UITableViewController { self.stopUpdatingRuntimeConfiguration() } } + onDemandObservationToken = tunnel.observe(\.isActivateOnDemandEnabled) { [weak self] tunnel, _ in + // Handle On-Demand getting turned on/off outside of the app + self?.onDemandViewModel = ActivateOnDemandViewModel(setting: tunnel.activateOnDemandSetting) + self?.updateActivateOnDemandFields() + } } required init?(coder aDecoder: NSCoder) { @@ -242,11 +254,27 @@ class TunnelDetailTableViewController: UITableViewController { self.applyTunnelConfiguration(tunnelConfiguration: tunnelConfiguration) } } + + private func updateActivateOnDemandFields() { + guard let onDemandSection = sections.firstIndex(where: { if case .onDemand = $0 { return true } else { return false } }) else { return } + let numberOfTableViewOnDemandRows = tableView.numberOfRows(inSection: onDemandSection) + let ssidRowIndexPath = IndexPath(row: 1, section: onDemandSection) + switch (numberOfTableViewOnDemandRows, onDemandViewModel.isWiFiInterfaceEnabled) { + case (1, true): + tableView.insertRows(at: [ssidRowIndexPath], with: .automatic) + case (2, false): + tableView.deleteRows(at: [ssidRowIndexPath], with: .automatic) + default: + break + } + tableView.reloadSections(IndexSet(integer: onDemandSection), with: .automatic) + } } extension TunnelDetailTableViewController: TunnelEditTableViewControllerDelegate { func tunnelSaved(tunnel: TunnelContainer) { tunnelViewModel = TunnelViewModel(tunnelConfiguration: tunnel.tunnelConfiguration) + onDemandViewModel = ActivateOnDemandViewModel(setting: tunnel.activateOnDemandSetting) loadSections() loadVisibleFields() title = tunnel.name @@ -272,7 +300,7 @@ extension TunnelDetailTableViewController { case .peer(let peerIndex, _): return peerFieldIsVisible[peerIndex].filter { $0 }.count case .onDemand: - return 1 + return onDemandViewModel.isWiFiInterfaceEnabled ? 2 : 1 case .delete: return 1 } @@ -380,10 +408,12 @@ extension TunnelDetailTableViewController { private func onDemandCell(for tableView: UITableView, at indexPath: IndexPath) -> UITableViewCell { let cell: KeyValueCell = tableView.dequeueReusableCell(for: indexPath) - cell.key = tr("tunnelOnDemandKey") - cell.value = TunnelViewModel.activateOnDemandDetailText(for: tunnel.activateOnDemandSetting) - cell.observationToken = tunnel.observe(\.isActivateOnDemandEnabled) { [weak cell] tunnel, _ in - cell?.value = TunnelViewModel.activateOnDemandDetailText(for: tunnel.activateOnDemandSetting) + let field = TunnelDetailTableViewController.onDemandFields[indexPath.row] + cell.key = field.localizedUIString + if field == .onDemand { + cell.value = onDemandViewModel.localizedInterfaceDescription + } else if field == .ssid { + cell.value = onDemandViewModel.ssidOption.localizedUIString } return cell } diff --git a/WireGuard/WireGuard/UI/iOS/ViewController/TunnelEditTableViewController.swift b/WireGuard/WireGuard/UI/iOS/ViewController/TunnelEditTableViewController.swift index ef7fc60..6f0444e 100644 --- a/WireGuard/WireGuard/UI/iOS/ViewController/TunnelEditTableViewController.swift +++ b/WireGuard/WireGuard/UI/iOS/ViewController/TunnelEditTableViewController.swift @@ -46,7 +46,7 @@ class TunnelEditTableViewController: UITableViewController { let onDemandFields: [ActivateOnDemandViewModel.OnDemandField] = [ .nonWiFiInterface, .wiFiInterface, - .ssidEdit + .ssid ] let tunnelsManager: TunnelsManager @@ -60,8 +60,7 @@ class TunnelEditTableViewController: UITableViewController { self.tunnelsManager = tunnelsManager self.tunnel = tunnel tunnelViewModel = TunnelViewModel(tunnelConfiguration: tunnel.tunnelConfiguration) - let onDemandOption = tunnel.activateOnDemandSetting.isActivateOnDemandEnabled ? tunnel.activateOnDemandSetting.activateOnDemandOption : .none - onDemandViewModel = ActivateOnDemandViewModel(from: onDemandOption) + onDemandViewModel = ActivateOnDemandViewModel(setting: tunnel.activateOnDemandSetting) super.init(style: .grouped) loadSections() } -- cgit v1.2.3-59-g8ed1b