diff options
author | Roopesh Chander <roop@roopc.net> | 2019-01-31 19:34:44 +0530 |
---|---|---|
committer | Roopesh Chander <roop@roopc.net> | 2019-02-01 15:27:17 +0530 |
commit | 1f51ff6b17a53055c2ca31717ae1aa68ba1a0b9b (patch) | |
tree | 99dc763c80803055c1aa534c7ae55a7cbc0c531d /WireGuard | |
parent | iOS: Tunnel detail: Refactor updation of status (diff) | |
download | wireguard-apple-1f51ff6b17a53055c2ca31717ae1aa68ba1a0b9b.tar.xz wireguard-apple-1f51ff6b17a53055c2ca31717ae1aa68ba1a0b9b.zip |
iOS: Tunnel detail: Reload runtime config every second
Signed-off-by: Roopesh Chander <roop@roopc.net>
Diffstat (limited to '')
-rw-r--r-- | WireGuard/WireGuard/UI/iOS/ViewController/TunnelDetailTableViewController.swift | 49 |
1 files changed, 46 insertions, 3 deletions
diff --git a/WireGuard/WireGuard/UI/iOS/ViewController/TunnelDetailTableViewController.swift b/WireGuard/WireGuard/UI/iOS/ViewController/TunnelDetailTableViewController.swift index f24c48a..9aeab35 100644 --- a/WireGuard/WireGuard/UI/iOS/ViewController/TunnelDetailTableViewController.swift +++ b/WireGuard/WireGuard/UI/iOS/ViewController/TunnelDetailTableViewController.swift @@ -20,7 +20,8 @@ class TunnelDetailTableViewController: UITableViewController { let peerFields: [TunnelViewModel.PeerField] = [ .publicKey, .preSharedKey, .endpoint, - .allowedIPs, .persistentKeepAlive + .allowedIPs, .persistentKeepAlive, + .rxBytes, .txBytes, .lastHandshakeTime ] let tunnelsManager: TunnelsManager @@ -30,6 +31,7 @@ class TunnelDetailTableViewController: UITableViewController { private weak var statusCell: SwitchCell? private var onDemandStatusObservationToken: AnyObject? private var statusObservationToken: AnyObject? + private var reloadRuntimeConfigurationTimer: Timer? init(tunnelsManager: TunnelsManager, tunnel: TunnelContainer) { self.tunnelsManager = tunnelsManager @@ -38,8 +40,15 @@ class TunnelDetailTableViewController: UITableViewController { super.init(style: .grouped) loadSections() statusObservationToken = tunnel.observe(\.status) { [weak self] _, _ in - if let cell = self?.statusCell { - self?.updateStatus(statusCell: cell) + guard let self = self else { return } + if let cell = self.statusCell { + self.updateStatus(statusCell: cell) + } + if tunnel.status == .active { + self.startUpdatingRuntimeConfiguration() + } else if tunnel.status == .inactive { + self.reloadRuntimeConfiguration() + self.stopUpdatingRuntimeConfiguration() } } } @@ -72,6 +81,16 @@ class TunnelDetailTableViewController: UITableViewController { sections.append(.delete) } + override func viewWillAppear(_ animated: Bool) { + if tunnel.status == .active { + self.startUpdatingRuntimeConfiguration() + } + } + + override func viewDidDisappear(_ animated: Bool) { + stopUpdatingRuntimeConfiguration() + } + @objc func editTapped() { let editVC = TunnelEditTableViewController(tunnelsManager: tunnelsManager, tunnel: tunnel) editVC.delegate = self @@ -121,6 +140,30 @@ class TunnelDetailTableViewController: UITableViewController { } cell.isEnabled = status == .active || status == .inactive } + + func startUpdatingRuntimeConfiguration() { + reloadRuntimeConfiguration() + reloadRuntimeConfigurationTimer?.invalidate() + let reloadTimer = Timer(timeInterval: 1 /* second */, repeats: true) { [weak self] _ in + self?.reloadRuntimeConfiguration() + } + reloadRuntimeConfigurationTimer = reloadTimer + RunLoop.main.add(reloadTimer, forMode: .common) + } + + func stopUpdatingRuntimeConfiguration() { + reloadRuntimeConfigurationTimer?.invalidate() + reloadRuntimeConfigurationTimer = nil + } + + private func reloadRuntimeConfiguration() { + tunnel.getRuntimeTunnelConfiguration(completionHandler: { + guard let tunnelConfiguration = $0 else { return } + self.tunnelViewModel = TunnelViewModel(tunnelConfiguration: tunnelConfiguration) + self.loadSections() + self.tableView.reloadData() + }) + } } extension TunnelDetailTableViewController: TunnelEditTableViewControllerDelegate { |