diff options
author | Roopesh Chander <roop@roopc.net> | 2018-11-02 13:12:10 +0530 |
---|---|---|
committer | Roopesh Chander <roop@roopc.net> | 2018-11-02 13:12:10 +0530 |
commit | e7a1f142fb3c48080345c3d56108cd6b2d308098 (patch) | |
tree | 6ed322687c15e5d7f8d2e69f22128f856db6101f /WireGuard/WireGuard/UI | |
parent | Tunnel view model: If there's just one peer, populate the scratchpad on init (diff) | |
download | wireguard-apple-e7a1f142fb3c48080345c3d56108cd6b2d308098.tar.xz wireguard-apple-e7a1f142fb3c48080345c3d56108cd6b2d308098.zip |
Table edit: Show/hide 'Exclude Private IPs' instead of enable/disable
Signed-off-by: Roopesh Chander <roop@roopc.net>
Diffstat (limited to 'WireGuard/WireGuard/UI')
-rw-r--r-- | WireGuard/WireGuard/UI/iOS/TunnelEditTableViewController.swift | 52 |
1 files changed, 36 insertions, 16 deletions
diff --git a/WireGuard/WireGuard/UI/iOS/TunnelEditTableViewController.swift b/WireGuard/WireGuard/UI/iOS/TunnelEditTableViewController.swift index 7702b11..c7794e3 100644 --- a/WireGuard/WireGuard/UI/iOS/TunnelEditTableViewController.swift +++ b/WireGuard/WireGuard/UI/iOS/TunnelEditTableViewController.swift @@ -133,7 +133,10 @@ extension TunnelEditTableViewController { return interfaceFieldsBySection[section].count } else if ((numberOfPeerSections > 0) && (section < (numberOfInterfaceSections + numberOfPeerSections))) { // Peer - return peerFields.count + let peerIndex = (section - numberOfInterfaceSections) + let peerData = tunnelViewModel.peersData[peerIndex] + let peerFieldsToShow = peerData.shouldAllowExcludePrivateIPsControl ? peerFields : peerFields.filter { $0 != .excludePrivateIPs } + return peerFieldsToShow.count } else { // Add peer return 1 @@ -232,7 +235,8 @@ extension TunnelEditTableViewController { // Peer let peerIndex = (section - numberOfInterfaceSections) let peerData = tunnelViewModel.peersData[peerIndex] - let field = peerFields[row] + let peerFieldsToShow = peerData.shouldAllowExcludePrivateIPsControl ? peerFields : peerFields.filter { $0 != .excludePrivateIPs } + let field = peerFieldsToShow[row] if (field == .deletePeer) { let cell = tableView.dequeueReusableCell(withIdentifier: TunnelEditTableViewButtonCell.id, for: indexPath) as! TunnelEditTableViewButtonCell cell.buttonText = field.rawValue @@ -245,13 +249,18 @@ extension TunnelEditTableViewController { onConfirmed: { [weak s] in guard let s = s else { return } let removedSectionIndices = s.deletePeer(peer: peerData) - s.tableView.deleteSections(removedSectionIndices, with: .automatic) - if let row = s.peerFields.firstIndex(of: .excludePrivateIPs) { - let excludePrivateIPsIndexPaths = (0 ..< s.tunnelViewModel.peersData.count).map { - IndexPath(row: row, section: numberOfInterfaceSections + $0) + let shouldShowExcludePrivateIPs = (s.tunnelViewModel.peersData.count == 1 && + s.tunnelViewModel.peersData[0].shouldAllowExcludePrivateIPsControl) + tableView.performBatchUpdates({ + s.tableView.deleteSections(removedSectionIndices, with: .automatic) + if (shouldShowExcludePrivateIPs) { + if let row = s.peerFields.firstIndex(of: .excludePrivateIPs) { + let rowIndexPath = IndexPath(row: row, section: numberOfInterfaceSections /* First peer section */) + s.tableView.insertRows(at: [rowIndexPath], with: .automatic) + } + } - s.tableView.reloadRows(at: excludePrivateIPsIndexPaths, with: .none) - } + }) }) } return cell @@ -299,9 +308,16 @@ extension TunnelEditTableViewController { if (field == .allowedIPs) { cell.onValueBeingEdited = { [weak self, weak peerData] value in if let peerData = peerData, let s = self { + let oldValue = peerData.shouldAllowExcludePrivateIPsControl peerData[.allowedIPs] = value - if let row = s.peerFields.firstIndex(of: .excludePrivateIPs) { - s.tableView.reloadRows(at: [IndexPath(row: row, section: section)], with: .none) + if (oldValue != peerData.shouldAllowExcludePrivateIPsControl) { + if let row = s.peerFields.firstIndex(of: .excludePrivateIPs) { + if (peerData.shouldAllowExcludePrivateIPsControl) { + s.tableView.insertRows(at: [IndexPath(row: row, section: section)], with: .automatic) + } else { + s.tableView.deleteRows(at: [IndexPath(row: row, section: section)], with: .automatic) + } + } } } } @@ -315,14 +331,18 @@ extension TunnelEditTableViewController { cell.buttonText = "Add peer" cell.onTapped = { [weak self] in guard let s = self else { return } + let shouldHideExcludePrivateIPs = (s.tunnelViewModel.peersData.count == 1 && + s.tunnelViewModel.peersData[0].shouldAllowExcludePrivateIPsControl) let addedSectionIndices = s.appendEmptyPeer() - tableView.insertSections(addedSectionIndices, with: .automatic) - if let row = s.peerFields.firstIndex(of: .excludePrivateIPs) { - let excludePrivateIPsIndexPaths = (0 ..< s.tunnelViewModel.peersData.count).map { - IndexPath(row: row, section: numberOfInterfaceSections + $0) + tableView.performBatchUpdates({ + tableView.insertSections(addedSectionIndices, with: .automatic) + if (shouldHideExcludePrivateIPs) { + if let row = s.peerFields.firstIndex(of: .excludePrivateIPs) { + let rowIndexPath = IndexPath(row: row, section: numberOfInterfaceSections /* First peer section */) + s.tableView.deleteRows(at: [rowIndexPath], with: .automatic) + } } - s.tableView.reloadRows(at: excludePrivateIPsIndexPaths, with: .none) - } + }, completion: nil) } return cell } |