WireGuardApp: Animate switch control in TunnelListCell

Signed-off-by: Andrej Mihajlov <and@mullvad.net>
This commit is contained in:
Andrej Mihajlov 2020-12-15 14:21:21 +01:00 committed by Jason A. Donenfeld
parent 5d2a337332
commit 9a483a46fa
1 changed files with 9 additions and 9 deletions

View File

@ -5,16 +5,16 @@ import UIKit
class TunnelListCell: UITableViewCell { class TunnelListCell: UITableViewCell {
var tunnel: TunnelContainer? { var tunnel: TunnelContainer? {
didSet(value) { didSet {
// Bind to the tunnel's name // Bind to the tunnel's name
nameLabel.text = tunnel?.name ?? "" nameLabel.text = tunnel?.name ?? ""
nameObservationToken = tunnel?.observe(\.name) { [weak self] tunnel, _ in nameObservationToken = tunnel?.observe(\.name) { [weak self] tunnel, _ in
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) update(from: tunnel?.status, animated: false)
statusObservationToken = tunnel?.observe(\.status) { [weak self] tunnel, _ in statusObservationToken = tunnel?.observe(\.status) { [weak self] tunnel, _ in
self?.update(from: tunnel.status) self?.update(from: tunnel.status, animated: true)
} }
} }
} }
@ -82,12 +82,12 @@ class TunnelListCell: UITableViewCell {
onSwitchToggled?(statusSwitch.isOn) onSwitchToggled?(statusSwitch.isOn)
} }
private func update(from status: TunnelStatus?) { private func update(from status: TunnelStatus?, animated: Bool) {
guard let status = status else { guard let status = status else {
reset() reset(animated: animated)
return return
} }
statusSwitch.isOn = !(status == .deactivating || status == .inactive) statusSwitch.setOn(!(status == .deactivating || status == .inactive), animated: animated)
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()
@ -105,14 +105,14 @@ class TunnelListCell: UITableViewCell {
statusSwitch.isEnabled = !editing statusSwitch.isEnabled = !editing
} }
private func reset() { private func reset(animated: Bool) {
statusSwitch.isOn = false statusSwitch.setOn(false, animated: animated)
statusSwitch.isUserInteractionEnabled = false statusSwitch.isUserInteractionEnabled = false
busyIndicator.stopAnimating() busyIndicator.stopAnimating()
} }
override func prepareForReuse() { override func prepareForReuse() {
super.prepareForReuse() super.prepareForReuse()
reset() reset(animated: false)
} }
} }