iOS: Tunnel detail: Keep track of visible fields with a [Bool] array

Signed-off-by: Roopesh Chander <roop@roopc.net>
This commit is contained in:
Roopesh Chander
2019-02-02 18:10:57 +05:30
parent 0c5739db82
commit 4134baced1
@@ -8,7 +8,7 @@ class TunnelDetailTableViewController: UITableViewController {
private enum Section { private enum Section {
case status case status
case interface case interface
case peer(_ peer: TunnelViewModel.PeerData) case peer(index: Int, peer: TunnelViewModel.PeerData)
case onDemand case onDemand
case delete case delete
} }
@@ -27,7 +27,11 @@ class TunnelDetailTableViewController: UITableViewController {
let tunnelsManager: TunnelsManager let tunnelsManager: TunnelsManager
let tunnel: TunnelContainer let tunnel: TunnelContainer
var tunnelViewModel: TunnelViewModel var tunnelViewModel: TunnelViewModel
private var sections = [Section]() private var sections = [Section]()
private var interfaceFieldIsVisible = [Bool]()
private var peerFieldIsVisible = [[Bool]]()
private weak var statusCell: SwitchCell? private weak var statusCell: SwitchCell?
private var onDemandStatusObservationToken: AnyObject? private var onDemandStatusObservationToken: AnyObject?
private var statusObservationToken: AnyObject? private var statusObservationToken: AnyObject?
@@ -39,6 +43,7 @@ class TunnelDetailTableViewController: UITableViewController {
tunnelViewModel = TunnelViewModel(tunnelConfiguration: tunnel.tunnelConfiguration) tunnelViewModel = TunnelViewModel(tunnelConfiguration: tunnel.tunnelConfiguration)
super.init(style: .grouped) super.init(style: .grouped)
loadSections() loadSections()
loadVisibleFields()
statusObservationToken = tunnel.observe(\.status) { [weak self] _, _ in statusObservationToken = tunnel.observe(\.status) { [weak self] _, _ in
guard let self = self else { return } guard let self = self else { return }
if let cell = self.statusCell { if let cell = self.statusCell {
@@ -76,11 +81,22 @@ class TunnelDetailTableViewController: UITableViewController {
sections.removeAll() sections.removeAll()
sections.append(.status) sections.append(.status)
sections.append(.interface) sections.append(.interface)
tunnelViewModel.peersData.forEach { sections.append(.peer($0)) } for (index, peer) in tunnelViewModel.peersData.enumerated() {
sections.append(.peer(index: index, peer: peer))
}
sections.append(.onDemand) sections.append(.onDemand)
sections.append(.delete) sections.append(.delete)
} }
private func loadVisibleFields() {
let visibleInterfaceFields = tunnelViewModel.interfaceData.filterFieldsWithValueOrControl(interfaceFields: interfaceFields)
interfaceFieldIsVisible = interfaceFields.map { visibleInterfaceFields.contains($0) }
peerFieldIsVisible = tunnelViewModel.peersData.map { peer in
let visiblePeerFields = peer.filterFieldsWithValueOrControl(peerFields: peerFields)
return peerFields.map { visiblePeerFields.contains($0) }
}
}
override func viewWillAppear(_ animated: Bool) { override func viewWillAppear(_ animated: Bool) {
if tunnel.status == .active { if tunnel.status == .active {
self.startUpdatingRuntimeConfiguration() self.startUpdatingRuntimeConfiguration()
@@ -189,9 +205,9 @@ extension TunnelDetailTableViewController {
case .status: case .status:
return 1 return 1
case .interface: case .interface:
return tunnelViewModel.interfaceData.filterFieldsWithValueOrControl(interfaceFields: interfaceFields).count return interfaceFieldIsVisible.filter { $0 }.count
case .peer(let peerData): case .peer(let peerIndex, _):
return peerData.filterFieldsWithValueOrControl(peerFields: peerFields).count return peerFieldIsVisible[peerIndex].filter { $0 }.count
case .onDemand: case .onDemand:
return 1 return 1
case .delete: case .delete:
@@ -220,8 +236,8 @@ extension TunnelDetailTableViewController {
return statusCell(for: tableView, at: indexPath) return statusCell(for: tableView, at: indexPath)
case .interface: case .interface:
return interfaceCell(for: tableView, at: indexPath) return interfaceCell(for: tableView, at: indexPath)
case .peer(let peer): case .peer(let index, let peer):
return peerCell(for: tableView, at: indexPath, with: peer) return peerCell(for: tableView, at: indexPath, with: peer, peerIndex: index)
case .onDemand: case .onDemand:
return onDemandCell(for: tableView, at: indexPath) return onDemandCell(for: tableView, at: indexPath)
case .delete: case .delete:
@@ -245,15 +261,17 @@ extension TunnelDetailTableViewController {
} }
private func interfaceCell(for tableView: UITableView, at indexPath: IndexPath) -> UITableViewCell { private func interfaceCell(for tableView: UITableView, at indexPath: IndexPath) -> UITableViewCell {
let field = tunnelViewModel.interfaceData.filterFieldsWithValueOrControl(interfaceFields: interfaceFields)[indexPath.row] let visibleInterfaceFields = interfaceFields.enumerated().filter { interfaceFieldIsVisible[$0.offset] }.map { $0.element }
let field = visibleInterfaceFields[indexPath.row]
let cell: KeyValueCell = tableView.dequeueReusableCell(for: indexPath) let cell: KeyValueCell = tableView.dequeueReusableCell(for: indexPath)
cell.key = field.localizedUIString cell.key = field.localizedUIString
cell.value = tunnelViewModel.interfaceData[field] cell.value = tunnelViewModel.interfaceData[field]
return cell return cell
} }
private func peerCell(for tableView: UITableView, at indexPath: IndexPath, with peerData: TunnelViewModel.PeerData) -> UITableViewCell { private func peerCell(for tableView: UITableView, at indexPath: IndexPath, with peerData: TunnelViewModel.PeerData, peerIndex: Int) -> UITableViewCell {
let field = peerData.filterFieldsWithValueOrControl(peerFields: peerFields)[indexPath.row] let visiblePeerFields = peerFields.enumerated().filter { peerFieldIsVisible[peerIndex][$0.offset] }.map { $0.element }
let field = visiblePeerFields[indexPath.row]
let cell: KeyValueCell = tableView.dequeueReusableCell(for: indexPath) let cell: KeyValueCell = tableView.dequeueReusableCell(for: indexPath)
cell.key = field.localizedUIString cell.key = field.localizedUIString
if field == .persistentKeepAlive { if field == .persistentKeepAlive {