UI: iOS: Tunnel detail: Incorporate on-demand-ness in 'Status'

Signed-off-by: Roopesh Chander <roop@roopc.net>
This commit is contained in:
Roopesh Chander 2021-07-26 16:56:03 +05:30
parent ac9f7b9f5e
commit 1e9e21bacf
3 changed files with 54 additions and 14 deletions

View File

@ -56,6 +56,8 @@
"tunnelStatusRestarting" = "Restarting"; "tunnelStatusRestarting" = "Restarting";
"tunnelStatusWaiting" = "Waiting"; "tunnelStatusWaiting" = "Waiting";
"tunnelStatusAddendumOnDemand" = " (On Demand)";
"macToggleStatusButtonActivate" = "Activate"; "macToggleStatusButtonActivate" = "Activate";
"macToggleStatusButtonActivating" = "Activating…"; "macToggleStatusButtonActivating" = "Activating…";
"macToggleStatusButtonDeactivate" = "Deactivate"; "macToggleStatusButtonDeactivate" = "Deactivate";

View File

@ -26,7 +26,9 @@ class SwitchCell: UITableViewCell {
var onSwitchToggled: ((Bool) -> Void)? var onSwitchToggled: ((Bool) -> Void)?
var observationToken: AnyObject? var statusObservationToken: AnyObject?
var isOnDemandEnabledObservationToken: AnyObject?
var hasOnDemandRulesObservationToken: AnyObject?
let switchView = UISwitch() let switchView = UISwitch()
@ -51,6 +53,8 @@ class SwitchCell: UITableViewCell {
isEnabled = true isEnabled = true
message = "" message = ""
isOn = false isOn = false
observationToken = nil statusObservationToken = nil
isOnDemandEnabledObservationToken = nil
hasOnDemandRulesObservationToken = nil
} }
} }

View File

@ -324,8 +324,22 @@ extension TunnelDetailTableViewController {
private func statusCell(for tableView: UITableView, at indexPath: IndexPath) -> UITableViewCell { private func statusCell(for tableView: UITableView, at indexPath: IndexPath) -> UITableViewCell {
let cell: SwitchCell = tableView.dequeueReusableCell(for: indexPath) let cell: SwitchCell = tableView.dequeueReusableCell(for: indexPath)
let statusUpdate: (SwitchCell, TunnelStatus) -> Void = { cell, status in func update(cell: SwitchCell?, with tunnel: TunnelContainer) {
let text: String guard let cell = cell else { return }
let status = tunnel.status
let isOnDemandEngaged = tunnel.isActivateOnDemandEnabled
let isSwitchOn = (status == .activating || status == .active || isOnDemandEngaged)
cell.switchView.setOn(isSwitchOn, animated: true)
if isOnDemandEngaged && !(status == .activating || status == .active) {
cell.switchView.onTintColor = UIColor.systemYellow
} else {
cell.switchView.onTintColor = UIColor.systemGreen
}
var text: String
switch status { switch status {
case .inactive: case .inactive:
text = tr("tunnelStatusInactive") text = tr("tunnelStatusInactive")
@ -342,26 +356,46 @@ extension TunnelDetailTableViewController {
case .waiting: case .waiting:
text = tr("tunnelStatusWaiting") text = tr("tunnelStatusWaiting")
} }
cell.textLabel?.text = text
cell.switchView.isOn = !(status == .deactivating || status == .inactive) if tunnel.hasOnDemandRules {
text += isOnDemandEngaged ? tr("tunnelStatusAddendumOnDemand") : ""
cell.switchView.isUserInteractionEnabled = true
cell.isEnabled = true
} else {
cell.switchView.isUserInteractionEnabled = (status == .inactive || status == .active) cell.switchView.isUserInteractionEnabled = (status == .inactive || status == .active)
cell.isEnabled = status == .active || status == .inactive cell.isEnabled = (status == .inactive || status == .active)
}
cell.textLabel?.text = text
} }
statusUpdate(cell, tunnel.status) update(cell: cell, with: tunnel)
cell.observationToken = tunnel.observe(\.status) { [weak cell] tunnel, _ in cell.statusObservationToken = tunnel.observe(\.status) { [weak cell] tunnel, _ in
guard let cell = cell else { return } update(cell: cell, with: tunnel)
statusUpdate(cell, tunnel.status) }
cell.isOnDemandEnabledObservationToken = tunnel.observe(\.isActivateOnDemandEnabled) { [weak cell] tunnel, _ in
update(cell: cell, with: tunnel)
}
cell.hasOnDemandRulesObservationToken = tunnel.observe(\.hasOnDemandRules) { [weak cell] tunnel, _ in
update(cell: cell, with: tunnel)
} }
cell.onSwitchToggled = { [weak self] isOn in cell.onSwitchToggled = { [weak self] isOn in
guard let self = self else { return } guard let self = self else { return }
if self.tunnel.hasOnDemandRules {
self.tunnelsManager.setOnDemandEnabled(isOn, on: self.tunnel) { error in
if error == nil && !isOn {
self.tunnelsManager.startDeactivation(of: self.tunnel)
}
}
} else {
if isOn { if isOn {
self.tunnelsManager.startActivation(of: self.tunnel) self.tunnelsManager.startActivation(of: self.tunnel)
} else { } else {
self.tunnelsManager.startDeactivation(of: self.tunnel) self.tunnelsManager.startDeactivation(of: self.tunnel)
} }
} }
}
return cell return cell
} }