aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/WireGuard/UI/iOS/TunnelDetail/TunnelDetailStatusCell.swift
diff options
context:
space:
mode:
Diffstat (limited to 'WireGuard/WireGuard/UI/iOS/TunnelDetail/TunnelDetailStatusCell.swift')
-rw-r--r--WireGuard/WireGuard/UI/iOS/TunnelDetail/TunnelDetailStatusCell.swift85
1 files changed, 85 insertions, 0 deletions
diff --git a/WireGuard/WireGuard/UI/iOS/TunnelDetail/TunnelDetailStatusCell.swift b/WireGuard/WireGuard/UI/iOS/TunnelDetail/TunnelDetailStatusCell.swift
new file mode 100644
index 0000000..855e3ed
--- /dev/null
+++ b/WireGuard/WireGuard/UI/iOS/TunnelDetail/TunnelDetailStatusCell.swift
@@ -0,0 +1,85 @@
+// SPDX-License-Identifier: MIT
+// Copyright © 2018 WireGuard LLC. All Rights Reserved.
+
+import UIKit
+
+class TunnelDetailStatusCell: UITableViewCell {
+ var tunnel: TunnelContainer? {
+ didSet(value) {
+ update(from: tunnel?.status)
+ statusObservervationToken = tunnel?.observe(\.status) { [weak self] tunnel, _ in
+ self?.update(from: tunnel.status)
+ }
+ }
+ }
+ var isSwitchInteractionEnabled: Bool {
+ get { return statusSwitch.isUserInteractionEnabled }
+ set(value) { statusSwitch.isUserInteractionEnabled = value }
+ }
+ var onSwitchToggled: ((Bool) -> Void)?
+ private var isOnSwitchToggledHandlerEnabled = true
+
+ let statusSwitch: UISwitch
+ private var statusObservervationToken: AnyObject?
+
+ override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
+ statusSwitch = UISwitch()
+ super.init(style: .default, reuseIdentifier: TunnelDetailKeyValueCell.reuseIdentifier)
+ accessoryView = statusSwitch
+
+ statusSwitch.addTarget(self, action: #selector(switchToggled), for: .valueChanged)
+ }
+
+ @objc func switchToggled() {
+ if isOnSwitchToggledHandlerEnabled {
+ onSwitchToggled?(statusSwitch.isOn)
+ }
+ }
+
+ private func update(from status: TunnelStatus?) {
+ guard let status = status else {
+ reset()
+ return
+ }
+ let text: String
+ switch status {
+ case .inactive:
+ text = "Inactive"
+ case .activating:
+ text = "Activating"
+ case .active:
+ text = "Active"
+ case .deactivating:
+ text = "Deactivating"
+ case .reasserting:
+ text = "Reactivating"
+ case .restarting:
+ text = "Restarting"
+ case .waiting:
+ text = "Waiting"
+ }
+ textLabel?.text = text
+ DispatchQueue.main.async { [weak statusSwitch] in
+ guard let statusSwitch = statusSwitch else { return }
+ statusSwitch.isOn = !(status == .deactivating || status == .inactive)
+ statusSwitch.isUserInteractionEnabled = (status == .inactive || status == .active)
+ }
+ textLabel?.textColor = (status == .active || status == .inactive) ? UIColor.black : UIColor.gray
+ }
+
+ required init?(coder aDecoder: NSCoder) {
+ fatalError("init(coder:) has not been implemented")
+ }
+
+ private func reset() {
+ textLabel?.text = "Invalid"
+ statusSwitch.isOn = false
+ textLabel?.textColor = UIColor.gray
+ statusSwitch.isUserInteractionEnabled = false
+ }
+
+ override func prepareForReuse() {
+ super.prepareForReuse()
+ reset()
+ }
+}