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
|
|
|
|
|
2018-12-14 23:12:59 +00:00
|
|
|
class SwitchCell: UITableViewCell {
|
2018-12-13 18:58:50 +00:00
|
|
|
var message: String {
|
|
|
|
get { return textLabel?.text ?? "" }
|
2018-12-14 23:12:59 +00:00
|
|
|
set(value) { textLabel?.text = value }
|
2018-12-13 18:58:50 +00:00
|
|
|
}
|
|
|
|
var isOn: Bool {
|
|
|
|
get { return switchView.isOn }
|
|
|
|
set(value) { switchView.isOn = value }
|
|
|
|
}
|
|
|
|
var isEnabled: Bool {
|
|
|
|
get { return switchView.isEnabled }
|
|
|
|
set(value) {
|
|
|
|
switchView.isEnabled = value
|
2019-10-14 21:43:56 +00:00
|
|
|
if #available(iOS 13.0, *) {
|
|
|
|
textLabel?.textColor = value ? .label : .secondaryLabel
|
|
|
|
} else {
|
|
|
|
textLabel?.textColor = value ? .black : .gray
|
|
|
|
}
|
2018-12-13 18:58:50 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
var onSwitchToggled: ((Bool) -> Void)?
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2019-02-03 07:07:57 +00:00
|
|
|
var observationToken: AnyObject?
|
|
|
|
|
2018-12-14 23:12:59 +00:00
|
|
|
let switchView = UISwitch()
|
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: .default, reuseIdentifier: reuseIdentifier)
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
accessoryView = switchView
|
|
|
|
switchView.addTarget(self, action: #selector(switchToggled), for: .valueChanged)
|
|
|
|
}
|
2018-12-14 23:12:59 +00:00
|
|
|
|
|
|
|
required init?(coder aDecoder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
@objc func switchToggled() {
|
|
|
|
onSwitchToggled?(switchView.isOn)
|
|
|
|
}
|
2018-12-21 22:34:56 +00:00
|
|
|
|
2018-12-13 18:58:50 +00:00
|
|
|
override func prepareForReuse() {
|
|
|
|
super.prepareForReuse()
|
2018-12-22 21:32:29 +00:00
|
|
|
onSwitchToggled = nil
|
2018-12-14 23:12:59 +00:00
|
|
|
isEnabled = true
|
2018-12-13 18:58:50 +00:00
|
|
|
message = ""
|
|
|
|
isOn = false
|
2019-02-03 07:07:57 +00:00
|
|
|
observationToken = nil
|
2018-12-13 18:58:50 +00:00
|
|
|
}
|
|
|
|
}
|