]> git.ipfire.org Git - thirdparty/wireguard-apple.git/commitdiff
UI: iOS: Tunnels list: Incorporate on-demand-ness in the switch
authorRoopesh Chander <roop@roopc.net>
Sat, 24 Jul 2021 20:04:03 +0000 (01:34 +0530)
committerRoopesh Chander <roop@roopc.net>
Tue, 27 Jul 2021 21:48:01 +0000 (03:18 +0530)
Signed-off-by: Roopesh Chander <roop@roopc.net>
Sources/WireGuardApp/Base.lproj/Localizable.strings
Sources/WireGuardApp/UI/iOS/View/TunnelListCell.swift
Sources/WireGuardApp/UI/iOS/ViewController/TunnelsListTableViewController.swift

index 4533739f83bcb42350caa527a4c31a1a8d4cf43b..204b8831e8a62ad0eec1a97af9c25aa43504e6e3 100644 (file)
@@ -17,6 +17,7 @@
 "tunnelsListSelectAllButtonTitle" = "Select All";
 "tunnelsListDeleteButtonTitle" = "Delete";
 "tunnelsListSelectedTitle (%d)" = "%d selected";
+"tunnelListCaptionOnDemand" = "On Demand";
 
 // Tunnels list menu
 
index dd5f3d60197e4ee8b64172008e6522a03adb71f9..ffccd3f99a224de645c64334751bfdeb07aa7ca7 100644 (file)
@@ -12,9 +12,16 @@ class TunnelListCell: UITableViewCell {
                 self?.nameLabel.text = tunnel.name
             }
             // Bind to the tunnel's status
-            update(from: tunnel?.status, animated: false)
+            update(from: tunnel, animated: false)
             statusObservationToken = tunnel?.observe(\.status) { [weak self] tunnel, _ in
-                self?.update(from: tunnel.status, animated: true)
+                self?.update(from: tunnel, animated: true)
+            }
+            // Bind to tunnel's on-demand settings
+            isOnDemandEnabledObservationToken = tunnel?.observe(\.isActivateOnDemandEnabled) { [weak self] tunnel, _ in
+                self?.update(from: tunnel, animated: true)
+            }
+            hasOnDemandRulesObservationToken = tunnel?.observe(\.hasOnDemandRules) { [weak self] tunnel, _ in
+                self?.update(from: tunnel, animated: true)
             }
         }
     }
@@ -41,8 +48,10 @@ class TunnelListCell: UITableViewCell {
 
     let statusSwitch = UISwitch()
 
-    private var statusObservationToken: NSKeyValueObservation?
     private var nameObservationToken: NSKeyValueObservation?
+    private var statusObservationToken: NSKeyValueObservation?
+    private var isOnDemandEnabledObservationToken: NSKeyValueObservation?
+    private var hasOnDemandRulesObservationToken: NSKeyValueObservation?
 
     override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
         super.init(style: style, reuseIdentifier: reuseIdentifier)
@@ -94,13 +103,29 @@ class TunnelListCell: UITableViewCell {
         onSwitchToggled?(statusSwitch.isOn)
     }
 
-    private func update(from status: TunnelStatus?, animated: Bool) {
-        guard let status = status else {
+    private func update(from tunnel: TunnelContainer?, animated: Bool) {
+        guard let tunnel = tunnel else {
             reset(animated: animated)
             return
         }
-        statusSwitch.setOn(!(status == .deactivating || status == .inactive), animated: animated)
-        statusSwitch.isUserInteractionEnabled = (status == .inactive || status == .active)
+        let status = tunnel.status
+        let isOnDemandEngaged = tunnel.isActivateOnDemandEnabled
+
+        let isSwitchOn = (status == .activating || status == .active || isOnDemandEngaged)
+        statusSwitch.setOn(isSwitchOn, animated: true)
+
+        if isOnDemandEngaged && !(status == .activating || status == .active) {
+            statusSwitch.onTintColor = UIColor.systemYellow
+        } else {
+            statusSwitch.onTintColor = UIColor.systemGreen
+        }
+
+        if tunnel.hasOnDemandRules {
+            statusSwitch.isUserInteractionEnabled = true
+        } else {
+            statusSwitch.isUserInteractionEnabled = (status == .inactive || status == .active)
+        }
+
         if status == .inactive || status == .active {
             busyIndicator.stopAnimating()
         } else {
index 16e2adc5ffeda22cb0ff625d92b52d3400c28249..85e64cea2eb825fad5f6fcbfb10f880de8cf2af2 100644 (file)
@@ -317,10 +317,18 @@ extension TunnelsListTableViewController: UITableViewDataSource {
             cell.tunnel = tunnel
             cell.onSwitchToggled = { [weak self] isOn in
                 guard let self = self, let tunnelsManager = self.tunnelsManager else { return }
-                if isOn {
-                    tunnelsManager.startActivation(of: tunnel)
+                if tunnel.hasOnDemandRules {
+                    tunnelsManager.setOnDemandEnabled(isOn, on: tunnel) { error in
+                        if error == nil && !isOn {
+                            tunnelsManager.startDeactivation(of: tunnel)
+                        }
+                    }
                 } else {
-                    tunnelsManager.startDeactivation(of: tunnel)
+                    if isOn {
+                        tunnelsManager.startActivation(of: tunnel)
+                    } else {
+                        tunnelsManager.startDeactivation(of: tunnel)
+                    }
                 }
             }
         }