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";
"tunnelsListDeleteButtonTitle" = "Delete";
"tunnelsListSelectedTitle (%d)" = "%d selected";
"tunnelListCaptionOnDemand" = "On Demand";
// Tunnels list menu

View File

@ -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 {

View File

@ -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)
}
}
}
}