From a115dd3bd92e8fe99efd30e46cdbba89f7158d38 Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Sun, 25 Jul 2021 01:34:03 +0530 Subject: UI: iOS: Tunnels list: Incorporate on-demand-ness in the switch Signed-off-by: Roopesh Chander --- .../WireGuardApp/Base.lproj/Localizable.strings | 1 + .../WireGuardApp/UI/iOS/View/TunnelListCell.swift | 39 ++++++++++++++++++---- .../TunnelsListTableViewController.swift | 14 ++++++-- 3 files changed, 44 insertions(+), 10 deletions(-) diff --git a/Sources/WireGuardApp/Base.lproj/Localizable.strings b/Sources/WireGuardApp/Base.lproj/Localizable.strings index 4533739..204b883 100644 --- a/Sources/WireGuardApp/Base.lproj/Localizable.strings +++ b/Sources/WireGuardApp/Base.lproj/Localizable.strings @@ -17,6 +17,7 @@ "tunnelsListSelectAllButtonTitle" = "Select All"; "tunnelsListDeleteButtonTitle" = "Delete"; "tunnelsListSelectedTitle (%d)" = "%d selected"; +"tunnelListCaptionOnDemand" = "On Demand"; // Tunnels list menu diff --git a/Sources/WireGuardApp/UI/iOS/View/TunnelListCell.swift b/Sources/WireGuardApp/UI/iOS/View/TunnelListCell.swift index dd5f3d6..ffccd3f 100644 --- a/Sources/WireGuardApp/UI/iOS/View/TunnelListCell.swift +++ b/Sources/WireGuardApp/UI/iOS/View/TunnelListCell.swift @@ -12,9 +12,16 @@ class TunnelListCell: UITableViewCell { self?.nameLabel.text = tunnel.name } // Bind to the tunnel's status - update(from: tunnel?.status, animated: false) + update(from: tunnel, animated: false) statusObservationToken = tunnel?.observe(\.status) { [weak self] tunnel, _ in - self?.update(from: tunnel.status, animated: true) + self?.update(from: tunnel, animated: true) + } + // Bind to tunnel's on-demand settings + isOnDemandEnabledObservationToken = tunnel?.observe(\.isActivateOnDemandEnabled) { [weak self] tunnel, _ in + self?.update(from: tunnel, animated: true) + } + hasOnDemandRulesObservationToken = tunnel?.observe(\.hasOnDemandRules) { [weak self] tunnel, _ in + self?.update(from: tunnel, animated: true) } } } @@ -41,8 +48,10 @@ class TunnelListCell: UITableViewCell { let statusSwitch = UISwitch() - private var statusObservationToken: NSKeyValueObservation? private var nameObservationToken: NSKeyValueObservation? + private var statusObservationToken: NSKeyValueObservation? + private var isOnDemandEnabledObservationToken: NSKeyValueObservation? + private var hasOnDemandRulesObservationToken: NSKeyValueObservation? override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) @@ -94,13 +103,29 @@ class TunnelListCell: UITableViewCell { onSwitchToggled?(statusSwitch.isOn) } - private func update(from status: TunnelStatus?, animated: Bool) { - guard let status = status else { + private func update(from tunnel: TunnelContainer?, animated: Bool) { + guard let tunnel = tunnel else { reset(animated: animated) return } - statusSwitch.setOn(!(status == .deactivating || status == .inactive), animated: animated) - statusSwitch.isUserInteractionEnabled = (status == .inactive || status == .active) + let status = tunnel.status + let isOnDemandEngaged = tunnel.isActivateOnDemandEnabled + + let isSwitchOn = (status == .activating || status == .active || isOnDemandEngaged) + statusSwitch.setOn(isSwitchOn, animated: true) + + if isOnDemandEngaged && !(status == .activating || status == .active) { + statusSwitch.onTintColor = UIColor.systemYellow + } else { + statusSwitch.onTintColor = UIColor.systemGreen + } + + if tunnel.hasOnDemandRules { + statusSwitch.isUserInteractionEnabled = true + } else { + statusSwitch.isUserInteractionEnabled = (status == .inactive || status == .active) + } + if status == .inactive || status == .active { busyIndicator.stopAnimating() } else { diff --git a/Sources/WireGuardApp/UI/iOS/ViewController/TunnelsListTableViewController.swift b/Sources/WireGuardApp/UI/iOS/ViewController/TunnelsListTableViewController.swift index 16e2adc..85e64ce 100644 --- a/Sources/WireGuardApp/UI/iOS/ViewController/TunnelsListTableViewController.swift +++ b/Sources/WireGuardApp/UI/iOS/ViewController/TunnelsListTableViewController.swift @@ -317,10 +317,18 @@ extension TunnelsListTableViewController: UITableViewDataSource { cell.tunnel = tunnel cell.onSwitchToggled = { [weak self] isOn in guard let self = self, let tunnelsManager = self.tunnelsManager else { return } - if isOn { - tunnelsManager.startActivation(of: tunnel) + if tunnel.hasOnDemandRules { + tunnelsManager.setOnDemandEnabled(isOn, on: tunnel) { error in + if error == nil && !isOn { + tunnelsManager.startDeactivation(of: tunnel) + } + } } else { - tunnelsManager.startDeactivation(of: tunnel) + if isOn { + tunnelsManager.startActivation(of: tunnel) + } else { + tunnelsManager.startDeactivation(of: tunnel) + } } } } -- cgit v1.2.3-59-g8ed1b