iOS: Tunnel detail: Reload runtime config every second

This commit is contained in:
Roopesh Chander 2019-01-31 19:34:44 +05:30
parent 26e6f23582
commit d2e14cfb19
1 changed files with 46 additions and 3 deletions

View File

@ -20,7 +20,8 @@ class TunnelDetailTableViewController: UITableViewController {
let peerFields: [TunnelViewModel.PeerField] = [ let peerFields: [TunnelViewModel.PeerField] = [
.publicKey, .preSharedKey, .endpoint, .publicKey, .preSharedKey, .endpoint,
.allowedIPs, .persistentKeepAlive .allowedIPs, .persistentKeepAlive,
.rxBytes, .txBytes, .lastHandshakeTime
] ]
let tunnelsManager: TunnelsManager let tunnelsManager: TunnelsManager
@ -30,6 +31,7 @@ class TunnelDetailTableViewController: UITableViewController {
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?
private var reloadRuntimeConfigurationTimer: Timer?
init(tunnelsManager: TunnelsManager, tunnel: TunnelContainer) { init(tunnelsManager: TunnelsManager, tunnel: TunnelContainer) {
self.tunnelsManager = tunnelsManager self.tunnelsManager = tunnelsManager
@ -38,8 +40,15 @@ class TunnelDetailTableViewController: UITableViewController {
super.init(style: .grouped) super.init(style: .grouped)
loadSections() loadSections()
statusObservationToken = tunnel.observe(\.status) { [weak self] _, _ in statusObservationToken = tunnel.observe(\.status) { [weak self] _, _ in
if let cell = self?.statusCell { guard let self = self else { return }
self?.updateStatus(statusCell: cell) 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) sections.append(.delete)
} }
override func viewWillAppear(_ animated: Bool) {
if tunnel.status == .active {
self.startUpdatingRuntimeConfiguration()
}
}
override func viewDidDisappear(_ animated: Bool) {
stopUpdatingRuntimeConfiguration()
}
@objc func editTapped() { @objc func editTapped() {
let editVC = TunnelEditTableViewController(tunnelsManager: tunnelsManager, tunnel: tunnel) let editVC = TunnelEditTableViewController(tunnelsManager: tunnelsManager, tunnel: tunnel)
editVC.delegate = self editVC.delegate = self
@ -121,6 +140,30 @@ class TunnelDetailTableViewController: UITableViewController {
} }
cell.isEnabled = status == .active || status == .inactive 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 { extension TunnelDetailTableViewController: TunnelEditTableViewControllerDelegate {