2018-12-13 18:58:50 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-01-02 00:56:33 +00:00
|
|
|
// Copyright © 2018-2019 WireGuard LLC. All Rights Reserved.
|
2018-12-13 18:58:50 +00:00
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
|
|
|
class TunnelListCell: UITableViewCell {
|
|
|
|
var tunnel: TunnelContainer? {
|
|
|
|
didSet(value) {
|
|
|
|
// Bind to the tunnel's name
|
|
|
|
nameLabel.text = tunnel?.name ?? ""
|
2018-12-18 14:30:01 +00:00
|
|
|
nameObservationToken = tunnel?.observe(\.name) { [weak self] tunnel, _ in
|
2018-12-13 18:58:50 +00:00
|
|
|
self?.nameLabel.text = tunnel.name
|
|
|
|
}
|
|
|
|
// Bind to the tunnel's status
|
|
|
|
update(from: tunnel?.status)
|
2018-12-18 14:30:01 +00:00
|
|
|
statusObservationToken = tunnel?.observe(\.status) { [weak self] tunnel, _ in
|
2018-12-13 18:58:50 +00:00
|
|
|
self?.update(from: tunnel.status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var onSwitchToggled: ((Bool) -> Void)?
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
let nameLabel: UILabel = {
|
|
|
|
let nameLabel = UILabel()
|
|
|
|
nameLabel.font = UIFont.preferredFont(forTextStyle: .body)
|
|
|
|
nameLabel.adjustsFontForContentSizeCategory = true
|
|
|
|
nameLabel.numberOfLines = 0
|
|
|
|
return nameLabel
|
|
|
|
}()
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
let busyIndicator: UIActivityIndicatorView = {
|
2019-10-14 21:43:56 +00:00
|
|
|
if #available(iOS 13.0, *) {
|
|
|
|
let busyIndicator = UIActivityIndicatorView(style: .medium)
|
|
|
|
busyIndicator.hidesWhenStopped = true
|
|
|
|
return busyIndicator
|
|
|
|
} else {
|
|
|
|
let busyIndicator = UIActivityIndicatorView(style: .gray)
|
|
|
|
busyIndicator.hidesWhenStopped = true
|
|
|
|
return busyIndicator
|
|
|
|
}
|
2018-12-13 18:58:50 +00:00
|
|
|
}()
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
let statusSwitch = UISwitch()
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-18 14:30:01 +00:00
|
|
|
private var statusObservationToken: AnyObject?
|
|
|
|
private var nameObservationToken: AnyObject?
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
|
|
|
|
super.init(style: style, reuseIdentifier: reuseIdentifier)
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
contentView.addSubview(statusSwitch)
|
|
|
|
statusSwitch.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
statusSwitch.centerYAnchor.constraint(equalTo: contentView.centerYAnchor),
|
2018-12-22 02:37:22 +00:00
|
|
|
contentView.trailingAnchor.constraint(equalTo: statusSwitch.trailingAnchor)
|
2018-12-14 23:12:59 +00:00
|
|
|
])
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
contentView.addSubview(busyIndicator)
|
|
|
|
busyIndicator.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
busyIndicator.centerYAnchor.constraint(equalTo: contentView.centerYAnchor),
|
2018-12-22 02:37:22 +00:00
|
|
|
statusSwitch.leadingAnchor.constraint(equalToSystemSpacingAfter: busyIndicator.trailingAnchor, multiplier: 1)
|
2018-12-14 23:12:59 +00:00
|
|
|
])
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
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
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
nameLabel.topAnchor.constraint(equalToSystemSpacingBelow: contentView.layoutMarginsGuide.topAnchor, multiplier: 1),
|
2018-12-22 02:37:22 +00:00
|
|
|
nameLabel.leadingAnchor.constraint(equalToSystemSpacingAfter: contentView.layoutMarginsGuide.leadingAnchor, multiplier: 1),
|
|
|
|
busyIndicator.leadingAnchor.constraint(equalToSystemSpacingAfter: nameLabel.trailingAnchor, multiplier: 1),
|
2018-12-13 18:58:50 +00:00
|
|
|
bottomAnchorConstraint
|
2018-12-14 23:12:59 +00:00
|
|
|
])
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
accessoryType = .disclosureIndicator
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
statusSwitch.addTarget(self, action: #selector(switchToggled), for: .valueChanged)
|
|
|
|
}
|
2018-12-22 04:30:35 +00:00
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
@objc func switchToggled() {
|
|
|
|
onSwitchToggled?(statusSwitch.isOn)
|
|
|
|
}
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
private func update(from status: TunnelStatus?) {
|
|
|
|
guard let status = status else {
|
|
|
|
reset()
|
|
|
|
return
|
|
|
|
}
|
2018-12-14 11:03:31 +00:00
|
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(200)) { [weak statusSwitch, weak busyIndicator] in
|
2018-12-13 18:58:50 +00:00
|
|
|
guard let statusSwitch = statusSwitch, let busyIndicator = busyIndicator else { return }
|
|
|
|
statusSwitch.isOn = !(status == .deactivating || status == .inactive)
|
|
|
|
statusSwitch.isUserInteractionEnabled = (status == .inactive || status == .active)
|
|
|
|
if status == .inactive || status == .active {
|
|
|
|
busyIndicator.stopAnimating()
|
|
|
|
} else {
|
|
|
|
busyIndicator.startAnimating()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
required init?(coder aDecoder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2019-03-18 12:47:34 +00:00
|
|
|
override func setEditing(_ editing: Bool, animated: Bool) {
|
|
|
|
super.setEditing(editing, animated: animated)
|
|
|
|
statusSwitch.isEnabled = !editing
|
|
|
|
}
|
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
private func reset() {
|
|
|
|
statusSwitch.isOn = false
|
|
|
|
statusSwitch.isUserInteractionEnabled = false
|
|
|
|
busyIndicator.stopAnimating()
|
|
|
|
}
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
override func prepareForReuse() {
|
|
|
|
super.prepareForReuse()
|
|
|
|
reset()
|
|
|
|
}
|
|
|
|
}
|