UI: iOS: Tunnels list: Incorporate on-demand-ness in the switch

Signed-off-by: Roopesh Chander <roop@roopc.net>
This commit is contained in:
Roopesh Chander 2021-07-25 01:34:03 +05:30
parent df9934a4b8
commit a115dd3bd9
3 changed files with 44 additions and 10 deletions

View File

@ -17,6 +17,7 @@
"tunnelsListSelectAllButtonTitle" = "Select All"; "tunnelsListSelectAllButtonTitle" = "Select All";
"tunnelsListDeleteButtonTitle" = "Delete"; "tunnelsListDeleteButtonTitle" = "Delete";
"tunnelsListSelectedTitle (%d)" = "%d selected"; "tunnelsListSelectedTitle (%d)" = "%d selected";
"tunnelListCaptionOnDemand" = "On Demand";
// Tunnels list menu // Tunnels list menu

View File

@ -12,9 +12,16 @@ class TunnelListCell: UITableViewCell {
self?.nameLabel.text = tunnel.name self?.nameLabel.text = tunnel.name
} }
// Bind to the tunnel's status // 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 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() let statusSwitch = UISwitch()
private var statusObservationToken: NSKeyValueObservation?
private var nameObservationToken: NSKeyValueObservation? private var nameObservationToken: NSKeyValueObservation?
private var statusObservationToken: NSKeyValueObservation?
private var isOnDemandEnabledObservationToken: NSKeyValueObservation?
private var hasOnDemandRulesObservationToken: NSKeyValueObservation?
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier) super.init(style: style, reuseIdentifier: reuseIdentifier)
@ -94,13 +103,29 @@ class TunnelListCell: UITableViewCell {
onSwitchToggled?(statusSwitch.isOn) onSwitchToggled?(statusSwitch.isOn)
} }
private func update(from status: TunnelStatus?, animated: Bool) { private func update(from tunnel: TunnelContainer?, animated: Bool) {
guard let status = status else { guard let tunnel = tunnel else {
reset(animated: animated) reset(animated: animated)
return return
} }
statusSwitch.setOn(!(status == .deactivating || status == .inactive), animated: animated) 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) statusSwitch.isUserInteractionEnabled = (status == .inactive || status == .active)
}
if status == .inactive || status == .active { if status == .inactive || status == .active {
busyIndicator.stopAnimating() busyIndicator.stopAnimating()
} else { } else {

View File

@ -317,6 +317,13 @@ extension TunnelsListTableViewController: UITableViewDataSource {
cell.tunnel = tunnel cell.tunnel = tunnel
cell.onSwitchToggled = { [weak self] isOn in cell.onSwitchToggled = { [weak self] isOn in
guard let self = self, let tunnelsManager = self.tunnelsManager else { return } guard let self = self, let tunnelsManager = self.tunnelsManager else { return }
if tunnel.hasOnDemandRules {
tunnelsManager.setOnDemandEnabled(isOn, on: tunnel) { error in
if error == nil && !isOn {
tunnelsManager.startDeactivation(of: tunnel)
}
}
} else {
if isOn { if isOn {
tunnelsManager.startActivation(of: tunnel) tunnelsManager.startActivation(of: tunnel)
} else { } else {
@ -324,6 +331,7 @@ extension TunnelsListTableViewController: UITableViewDataSource {
} }
} }
} }
}
return cell return cell
} }
} }