]> git.ipfire.org Git - thirdparty/wireguard-apple.git/commitdiff
UI: iOS: Tunnel detail: Incorporate on-demand-ness in 'Status'
authorRoopesh Chander <roop@roopc.net>
Mon, 26 Jul 2021 11:26:03 +0000 (16:56 +0530)
committerRoopesh Chander <roop@roopc.net>
Tue, 27 Jul 2021 21:48:02 +0000 (03:18 +0530)
Signed-off-by: Roopesh Chander <roop@roopc.net>
Sources/WireGuardApp/Base.lproj/Localizable.strings
Sources/WireGuardApp/UI/iOS/View/SwitchCell.swift
Sources/WireGuardApp/UI/iOS/ViewController/TunnelDetailTableViewController.swift

index 0ba003e2f3730842a80dfb138452462d8e871282..87935d1370e9e7df0ea39f4a1dd2841d964e1535 100644 (file)
@@ -56,6 +56,8 @@
 "tunnelStatusRestarting" = "Restarting";
 "tunnelStatusWaiting" = "Waiting";
 
+"tunnelStatusAddendumOnDemand" = " (On Demand)";
+
 "macToggleStatusButtonActivate" = "Activate";
 "macToggleStatusButtonActivating" = "Activating…";
 "macToggleStatusButtonDeactivate" = "Deactivate";
index 9bdc34578d3ef1a21344bd84a6b4421aa4f820cf..3952ffdcee828ba2de393dfb9b3df7aa471fdd5e 100644 (file)
@@ -26,7 +26,9 @@ class SwitchCell: UITableViewCell {
 
     var onSwitchToggled: ((Bool) -> Void)?
 
-    var observationToken: AnyObject?
+    var statusObservationToken: AnyObject?
+    var isOnDemandEnabledObservationToken: AnyObject?
+    var hasOnDemandRulesObservationToken: AnyObject?
 
     let switchView = UISwitch()
 
@@ -51,6 +53,8 @@ class SwitchCell: UITableViewCell {
         isEnabled = true
         message = ""
         isOn = false
-        observationToken = nil
+        statusObservationToken = nil
+        isOnDemandEnabledObservationToken = nil
+        hasOnDemandRulesObservationToken = nil
     }
 }
index a699d1977e6649d43b4582ab785b148e50a87c89..675adfdf2b3280cdfa66142f59e15825f03e06a1 100644 (file)
@@ -324,8 +324,22 @@ extension TunnelDetailTableViewController {
     private func statusCell(for tableView: UITableView, at indexPath: IndexPath) -> UITableViewCell {
         let cell: SwitchCell = tableView.dequeueReusableCell(for: indexPath)
 
-        let statusUpdate: (SwitchCell, TunnelStatus) -> Void = { cell, status in
-            let text: String
+        func update(cell: SwitchCell?, with tunnel: TunnelContainer) {
+            guard let cell = cell else { return }
+
+            let status = tunnel.status
+            let isOnDemandEngaged = tunnel.isActivateOnDemandEnabled
+
+            let isSwitchOn = (status == .activating || status == .active || isOnDemandEngaged)
+            cell.switchView.setOn(isSwitchOn, animated: true)
+
+            if isOnDemandEngaged && !(status == .activating || status == .active) {
+                cell.switchView.onTintColor = UIColor.systemYellow
+            } else {
+                cell.switchView.onTintColor = UIColor.systemGreen
+            }
+
+            var text: String
             switch status {
             case .inactive:
                 text = tr("tunnelStatusInactive")
@@ -342,24 +356,44 @@ extension TunnelDetailTableViewController {
             case .waiting:
                 text = tr("tunnelStatusWaiting")
             }
+
+            if tunnel.hasOnDemandRules {
+                text += isOnDemandEngaged ? tr("tunnelStatusAddendumOnDemand") : ""
+                cell.switchView.isUserInteractionEnabled = true
+                cell.isEnabled = true
+            } else {
+                cell.switchView.isUserInteractionEnabled = (status == .inactive || status == .active)
+                cell.isEnabled = (status == .inactive || status == .active)
+            }
             cell.textLabel?.text = text
-            cell.switchView.isOn = !(status == .deactivating || status == .inactive)
-            cell.switchView.isUserInteractionEnabled = (status == .inactive || status == .active)
-            cell.isEnabled = status == .active || status == .inactive
         }
 
-        statusUpdate(cell, tunnel.status)
-        cell.observationToken = tunnel.observe(\.status) { [weak cell] tunnel, _ in
-            guard let cell = cell else { return }
-            statusUpdate(cell, tunnel.status)
+        update(cell: cell, with: tunnel)
+        cell.statusObservationToken = tunnel.observe(\.status) { [weak cell] tunnel, _ in
+            update(cell: cell, with: tunnel)
+        }
+        cell.isOnDemandEnabledObservationToken = tunnel.observe(\.isActivateOnDemandEnabled) { [weak cell] tunnel, _ in
+            update(cell: cell, with: tunnel)
+        }
+        cell.hasOnDemandRulesObservationToken = tunnel.observe(\.hasOnDemandRules) { [weak cell] tunnel, _ in
+            update(cell: cell, with: tunnel)
         }
 
         cell.onSwitchToggled = { [weak self] isOn in
             guard let self = self else { return }
-            if isOn {
-                self.tunnelsManager.startActivation(of: self.tunnel)
+
+            if self.tunnel.hasOnDemandRules {
+                self.tunnelsManager.setOnDemandEnabled(isOn, on: self.tunnel) { error in
+                    if error == nil && !isOn {
+                        self.tunnelsManager.startDeactivation(of: self.tunnel)
+                    }
+                }
             } else {
-                self.tunnelsManager.startDeactivation(of: self.tunnel)
+                if isOn {
+                    self.tunnelsManager.startActivation(of: self.tunnel)
+                } else {
+                    self.tunnelsManager.startDeactivation(of: self.tunnel)
+                }
             }
         }
         return cell