From 08e5d650450435e2feb610df37454b5b3f22b283 Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Thu, 31 Jan 2019 18:39:24 +0530 Subject: iOS: Tunnel detail: Refactor updation of status Signed-off-by: Roopesh Chander --- .../TunnelDetailTableViewController.swift | 68 +++++++++++----------- 1 file changed, 35 insertions(+), 33 deletions(-) (limited to 'WireGuard/WireGuard') diff --git a/WireGuard/WireGuard/UI/iOS/ViewController/TunnelDetailTableViewController.swift b/WireGuard/WireGuard/UI/iOS/ViewController/TunnelDetailTableViewController.swift index f1639f5..f24c48a 100644 --- a/WireGuard/WireGuard/UI/iOS/ViewController/TunnelDetailTableViewController.swift +++ b/WireGuard/WireGuard/UI/iOS/ViewController/TunnelDetailTableViewController.swift @@ -27,6 +27,7 @@ class TunnelDetailTableViewController: UITableViewController { let tunnel: TunnelContainer var tunnelViewModel: TunnelViewModel private var sections = [Section]() + private weak var statusCell: SwitchCell? private var onDemandStatusObservationToken: AnyObject? private var statusObservationToken: AnyObject? @@ -36,6 +37,11 @@ class TunnelDetailTableViewController: UITableViewController { tunnelViewModel = TunnelViewModel(tunnelConfiguration: tunnel.tunnelConfiguration) super.init(style: .grouped) loadSections() + statusObservationToken = tunnel.observe(\.status) { [weak self] _, _ in + if let cell = self?.statusCell { + self?.updateStatus(statusCell: cell) + } + } } required init?(coder aDecoder: NSCoder) { @@ -88,6 +94,33 @@ class TunnelDetailTableViewController: UITableViewController { present(alert, animated: true, completion: nil) } + + func updateStatus(statusCell cell: SwitchCell) { + let status = tunnel.status + let text: String + switch status { + case .inactive: + text = tr("tunnelStatusInactive") + case .activating: + text = tr("tunnelStatusActivating") + case .active: + text = tr("tunnelStatusActive") + case .deactivating: + text = tr("tunnelStatusDeactivating") + case .reasserting: + text = tr("tunnelStatusReasserting") + case .restarting: + text = tr("tunnelStatusRestarting") + case .waiting: + text = tr("tunnelStatusWaiting") + } + cell.textLabel?.text = text + DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(200)) { [weak cell] in + cell?.switchView.isOn = !(status == .deactivating || status == .inactive) + cell?.switchView.isUserInteractionEnabled = (status == .inactive || status == .active) + } + cell.isEnabled = status == .active || status == .inactive + } } extension TunnelDetailTableViewController: TunnelEditTableViewControllerDelegate { @@ -155,39 +188,7 @@ extension TunnelDetailTableViewController { private func statusCell(for tableView: UITableView, at indexPath: IndexPath) -> UITableViewCell { let cell: SwitchCell = tableView.dequeueReusableCell(for: indexPath) - - let statusUpdate: (SwitchCell, TunnelStatus) -> Void = { cell, status in - let text: String - switch status { - case .inactive: - text = tr("tunnelStatusInactive") - case .activating: - text = tr("tunnelStatusActivating") - case .active: - text = tr("tunnelStatusActive") - case .deactivating: - text = tr("tunnelStatusDeactivating") - case .reasserting: - text = tr("tunnelStatusReasserting") - case .restarting: - text = tr("tunnelStatusRestarting") - case .waiting: - text = tr("tunnelStatusWaiting") - } - cell.textLabel?.text = text - DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(200)) { [weak cell] in - cell?.switchView.isOn = !(status == .deactivating || status == .inactive) - cell?.switchView.isUserInteractionEnabled = (status == .inactive || status == .active) - } - cell.isEnabled = status == .active || status == .inactive - } - - statusUpdate(cell, tunnel.status) - statusObservationToken = tunnel.observe(\.status) { [weak cell] tunnel, _ in - guard let cell = cell else { return } - statusUpdate(cell, tunnel.status) - } - + updateStatus(statusCell: cell) cell.onSwitchToggled = { [weak self] isOn in guard let self = self else { return } if isOn { @@ -196,6 +197,7 @@ extension TunnelDetailTableViewController { self.tunnelsManager.startDeactivation(of: self.tunnel) } } + self.statusCell = cell return cell } -- cgit v1.2.3-59-g8ed1b