From bdeb89a9e587380aa6ee91618b50b6577797ab68 Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Tue, 5 Mar 2019 18:37:53 +0530 Subject: on-demand: iOS: Add ability to add current SSID Signed-off-by: Roopesh Chander --- WireGuard/WireGuard/Base.lproj/Localizable.strings | 3 +- .../SSIDOptionEditTableViewController.swift | 76 ++++++++++++++++++++-- 2 files changed, 73 insertions(+), 6 deletions(-) diff --git a/WireGuard/WireGuard/Base.lproj/Localizable.strings b/WireGuard/WireGuard/Base.lproj/Localizable.strings index 41130bb..30b633c 100644 --- a/WireGuard/WireGuard/Base.lproj/Localizable.strings +++ b/WireGuard/WireGuard/Base.lproj/Localizable.strings @@ -95,7 +95,8 @@ "tunnelOnDemandSelectionViewTitle" = "Select SSIDs"; "tunnelOnDemandSectionTitleSelectedSSIDs" = "Selected SSIDs"; "tunnelOnDemandSectionTitleAddSSIDs" = "Add SSIDs"; -"tunnelOnDemandAddMessageAddNew" = "Add manually"; +"tunnelOnDemandAddMessageAddNewSSID" = "Add manually"; +"tunnelOnDemandAddMessageAddConnectedSSID (%@)" = "Connected: %@"; "tunnelOnDemandKey" = "Activate on demand"; "tunnelOnDemandOptionOff" = "Off"; diff --git a/WireGuard/WireGuard/UI/iOS/ViewController/SSIDOptionEditTableViewController.swift b/WireGuard/WireGuard/UI/iOS/ViewController/SSIDOptionEditTableViewController.swift index 7027d34..f83d478 100644 --- a/WireGuard/WireGuard/UI/iOS/ViewController/SSIDOptionEditTableViewController.swift +++ b/WireGuard/WireGuard/UI/iOS/ViewController/SSIDOptionEditTableViewController.swift @@ -2,6 +2,7 @@ // Copyright © 2018-2019 WireGuard LLC. All Rights Reserved. import UIKit +import SystemConfiguration.CaptiveNetwork protocol SSIDOptionEditTableViewControllerDelegate: class { func ssidOptionSaved(option: ActivateOnDemandViewModel.OnDemandSSIDOption, ssids: [String]) @@ -14,9 +15,15 @@ class SSIDOptionEditTableViewController: UITableViewController { case addSSIDs } + private enum AddSSIDRow { + case addConnectedSSID(connectedSSID: String) + case addNewSSID + } + weak var delegate: SSIDOptionEditTableViewControllerDelegate? private var sections = [Section]() + private var addSSIDRows = [AddSSIDRow]() let ssidOptionFields: [ActivateOnDemandViewModel.OnDemandSSIDOption] = [ .anySSID, @@ -26,12 +33,15 @@ class SSIDOptionEditTableViewController: UITableViewController { var selectedOption: ActivateOnDemandViewModel.OnDemandSSIDOption var selectedSSIDs: [String] + var connectedSSID: String? init(option: ActivateOnDemandViewModel.OnDemandSSIDOption, ssids: [String]) { selectedOption = option selectedSSIDs = ssids super.init(style: .grouped) + connectedSSID = getConnectedSSID() loadSections() + loadAddSSIDRows() } required init?(coder aDecoder: NSCoder) { @@ -63,6 +73,30 @@ class SSIDOptionEditTableViewController: UITableViewController { } } + func loadAddSSIDRows() { + addSSIDRows.removeAll() + if let connectedSSID = connectedSSID { + if !selectedSSIDs.contains(connectedSSID) { + addSSIDRows.append(.addConnectedSSID(connectedSSID: connectedSSID)) + } + } + addSSIDRows.append(.addNewSSID) + } + + func updateTableViewAddSSIDRows() { + guard let addSSIDSection = sections.firstIndex(of: .addSSIDs) else { return } + let numberOfAddSSIDRows = addSSIDRows.count + let numberOfAddSSIDRowsInTableView = tableView.numberOfRows(inSection: addSSIDSection) + switch (numberOfAddSSIDRowsInTableView, numberOfAddSSIDRows) { + case (1, 2): + tableView.insertRows(at: [IndexPath(row: 0, section: addSSIDSection)], with: .automatic) + case (2, 1): + tableView.deleteRows(at: [IndexPath(row: 0, section: addSSIDSection)], with: .automatic) + default: + break + } + } + override func viewWillDisappear(_ animated: Bool) { delegate?.ssidOptionSaved(option: selectedOption, ssids: selectedSSIDs) } @@ -80,7 +114,7 @@ extension SSIDOptionEditTableViewController { case .selectedSSIDs: return selectedSSIDs.count case .addSSIDs: - return 1 + return addSSIDRows.count } } @@ -143,6 +177,8 @@ extension SSIDOptionEditTableViewController { guard let self = self, let cell = cell else { return } if let row = self.tableView.indexPath(for: cell)?.row { self.selectedSSIDs[row] = text + self.loadAddSSIDRows() + self.updateTableViewAddSSIDRows() } } return cell @@ -150,7 +186,12 @@ extension SSIDOptionEditTableViewController { private func addSSIDCell(for tableView: UITableView, at indexPath: IndexPath) -> UITableViewCell { let cell: TextCell = tableView.dequeueReusableCell(for: indexPath) - cell.message = tr("tunnelOnDemandAddMessageAddNew") + switch addSSIDRows[indexPath.row] { + case .addConnectedSSID: + cell.message = tr(format: "tunnelOnDemandAddMessageAddConnectedSSID (%@)", connectedSSID!) + case .addNewSSID: + cell.message = tr("tunnelOnDemandAddMessageAddNewSSID") + } cell.isEditing = true return cell } @@ -169,10 +210,19 @@ extension SSIDOptionEditTableViewController { } else { tableView.deleteSections(IndexSet(integer: indexPath.section), with: .automatic) } + loadAddSSIDRows() + updateTableViewAddSSIDRows() case .addSSIDs: assert(editingStyle == .insert) let hasSelectedSSIDsSection = sections.contains(.selectedSSIDs) - selectedSSIDs.append("") + let newSSID: String + switch addSSIDRows[indexPath.row] { + case .addConnectedSSID(let connectedSSID): + newSSID = connectedSSID + case .addNewSSID: + newSSID = "" + } + selectedSSIDs.append(newSSID) loadSections() let selectedSSIDsSection = sections.firstIndex(of: .selectedSSIDs)! let indexPath = IndexPath(row: selectedSSIDs.count - 1, section: selectedSSIDsSection) @@ -181,8 +231,12 @@ extension SSIDOptionEditTableViewController { } else { tableView.insertRows(at: [indexPath], with: .automatic) } - if let selectedSSIDCell = tableView.cellForRow(at: indexPath) as? EditableTextCell { - selectedSSIDCell.beginEditing() + loadAddSSIDRows() + updateTableViewAddSSIDRows() + if newSSID.isEmpty { + if let selectedSSIDCell = tableView.cellForRow(at: indexPath) as? EditableTextCell { + selectedSSIDCell.beginEditing() + } } } } @@ -225,3 +279,15 @@ extension SSIDOptionEditTableViewController { } } } + +private func getConnectedSSID() -> String? { + guard let supportedInterfaces = CNCopySupportedInterfaces() as? [CFString] else { return nil } + for interface in supportedInterfaces { + if let networkInfo = CNCopyCurrentNetworkInfo(interface) { + if let ssid = (networkInfo as NSDictionary)[kCNNetworkInfoKeySSID as String] as? String { + return !ssid.isEmpty ? ssid : nil + } + } + } + return nil +} -- cgit v1.2.3-59-g8ed1b