diff options
author | Andrej Mihajlov <and@mullvad.net> | 2020-12-15 14:51:02 +0100 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2020-12-15 15:56:22 +0100 |
commit | be96dea04aae3a2c465bdfd8aadb1c781f562472 (patch) | |
tree | 088b3921d2fe1715a81485208f9ab30e7d4ecde2 | |
parent | WireGuardApp: Replace AnyObject with a concrete NSKeyValueObservation (diff) | |
download | wireguard-apple-be96dea04aae3a2c465bdfd8aadb1c781f562472.tar.xz wireguard-apple-be96dea04aae3a2c465bdfd8aadb1c781f562472.zip |
WireGuardApp: Refactor TunnelListCell
Signed-off-by: Andrej Mihajlov <and@mullvad.net>
-rw-r--r-- | Sources/WireGuardApp/UI/iOS/View/TunnelListCell.swift | 68 |
1 files changed, 33 insertions, 35 deletions
diff --git a/Sources/WireGuardApp/UI/iOS/View/TunnelListCell.swift b/Sources/WireGuardApp/UI/iOS/View/TunnelListCell.swift index e28b700..6015c8e 100644 --- a/Sources/WireGuardApp/UI/iOS/View/TunnelListCell.swift +++ b/Sources/WireGuardApp/UI/iOS/View/TunnelListCell.swift @@ -47,38 +47,50 @@ class TunnelListCell: UITableViewCell { override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) - contentView.addSubview(statusSwitch) - statusSwitch.translatesAutoresizingMaskIntoConstraints = false - NSLayoutConstraint.activate([ - statusSwitch.centerYAnchor.constraint(equalTo: contentView.centerYAnchor), - statusSwitch.trailingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.trailingAnchor) - ]) + accessoryType = .disclosureIndicator - contentView.addSubview(busyIndicator) - busyIndicator.translatesAutoresizingMaskIntoConstraints = false - NSLayoutConstraint.activate([ - busyIndicator.centerYAnchor.constraint(equalTo: contentView.centerYAnchor), - statusSwitch.leadingAnchor.constraint(equalToSystemSpacingAfter: busyIndicator.trailingAnchor, multiplier: 1) - ]) + for subview in [statusSwitch, busyIndicator, nameLabel] { + subview.translatesAutoresizingMaskIntoConstraints = false + contentView.addSubview(subview) + } - contentView.addSubview(nameLabel) - nameLabel.translatesAutoresizingMaskIntoConstraints = false nameLabel.setContentCompressionResistancePriority(.defaultLow, for: .horizontal) - let bottomAnchorConstraint = contentView.layoutMarginsGuide.bottomAnchor.constraint(equalToSystemSpacingBelow: nameLabel.bottomAnchor, multiplier: 1) - bottomAnchorConstraint.priority = .defaultLow + + let nameLabelBottomConstraint = + contentView.layoutMarginsGuide.bottomAnchor.constraint(equalToSystemSpacingBelow: nameLabel.bottomAnchor, multiplier: 1) + nameLabelBottomConstraint.priority = .defaultLow + NSLayoutConstraint.activate([ + statusSwitch.centerYAnchor.constraint(equalTo: contentView.centerYAnchor), + statusSwitch.trailingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.trailingAnchor), + statusSwitch.leadingAnchor.constraint(equalToSystemSpacingAfter: busyIndicator.trailingAnchor, multiplier: 1), + nameLabel.topAnchor.constraint(equalToSystemSpacingBelow: contentView.layoutMarginsGuide.topAnchor, multiplier: 1), + nameLabelBottomConstraint, nameLabel.leadingAnchor.constraint(equalToSystemSpacingAfter: contentView.layoutMarginsGuide.leadingAnchor, multiplier: 1), - busyIndicator.leadingAnchor.constraint(equalToSystemSpacingAfter: nameLabel.trailingAnchor, multiplier: 1), - bottomAnchorConstraint - ]) - accessoryType = .disclosureIndicator + busyIndicator.centerYAnchor.constraint(equalTo: contentView.centerYAnchor), + busyIndicator.leadingAnchor.constraint(equalToSystemSpacingAfter: nameLabel.trailingAnchor, multiplier: 1) + ]) statusSwitch.addTarget(self, action: #selector(switchToggled), for: .valueChanged) } - @objc func switchToggled() { + required init?(coder aDecoder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + override func prepareForReuse() { + super.prepareForReuse() + reset(animated: false) + } + + override func setEditing(_ editing: Bool, animated: Bool) { + super.setEditing(editing, animated: animated) + statusSwitch.isEnabled = !editing + } + + @objc private func switchToggled() { onSwitchToggled?(statusSwitch.isOn) } @@ -96,23 +108,9 @@ class TunnelListCell: UITableViewCell { } } - required init?(coder aDecoder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } - - override func setEditing(_ editing: Bool, animated: Bool) { - super.setEditing(editing, animated: animated) - statusSwitch.isEnabled = !editing - } - private func reset(animated: Bool) { statusSwitch.setOn(false, animated: animated) statusSwitch.isUserInteractionEnabled = false busyIndicator.stopAnimating() } - - override func prepareForReuse() { - super.prepareForReuse() - reset(animated: false) - } } |