aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/WireGuard/UI/iOS/SharedViews/SwitchCell.swift
diff options
context:
space:
mode:
Diffstat (limited to 'WireGuard/WireGuard/UI/iOS/SharedViews/SwitchCell.swift')
-rw-r--r--WireGuard/WireGuard/UI/iOS/SharedViews/SwitchCell.swift48
1 files changed, 48 insertions, 0 deletions
diff --git a/WireGuard/WireGuard/UI/iOS/SharedViews/SwitchCell.swift b/WireGuard/WireGuard/UI/iOS/SharedViews/SwitchCell.swift
new file mode 100644
index 00000000..d0c29aac
--- /dev/null
+++ b/WireGuard/WireGuard/UI/iOS/SharedViews/SwitchCell.swift
@@ -0,0 +1,48 @@
+// SPDX-License-Identifier: MIT
+// Copyright © 2018 WireGuard LLC. All Rights Reserved.
+
+import UIKit
+
+class SwitchCell: UITableViewCell {
+ var message: String {
+ get { return textLabel?.text ?? "" }
+ set(value) { textLabel?.text = value }
+ }
+ var isOn: Bool {
+ get { return switchView.isOn }
+ set(value) { switchView.isOn = value }
+ }
+ var isEnabled: Bool {
+ get { return switchView.isEnabled }
+ set(value) {
+ switchView.isEnabled = value
+ textLabel?.textColor = value ? .black : .gray
+ }
+ }
+
+ var onSwitchToggled: ((Bool) -> Void)?
+
+ let switchView = UISwitch()
+
+ override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
+ super.init(style: .default, reuseIdentifier: reuseIdentifier)
+
+ accessoryView = switchView
+ switchView.addTarget(self, action: #selector(switchToggled), for: .valueChanged)
+ }
+
+ required init?(coder aDecoder: NSCoder) {
+ fatalError("init(coder:) has not been implemented")
+ }
+
+ @objc func switchToggled() {
+ onSwitchToggled?(switchView.isOn)
+ }
+
+ override func prepareForReuse() {
+ super.prepareForReuse()
+ isEnabled = true
+ message = ""
+ isOn = false
+ }
+}