浏览代码

add default providers we have in desktop (#1726)

* add default providers we have in desktop

they were asked for permission as far as I know.

The checkmark on the custom cell is still a bit bugged,
it is only shown on opening the page, but not if it was set later,
hiding it when selecting another cell works,
I suspect the text field constraints.

* add "off" option

* Update deltachat-ios/Controller/Settings/SingleSettings/VideoChatViewController.swift

Co-authored-by: bjoern <r10s@b44t.com>

* fix whitespace

* some rewordings, show label in summary

Co-authored-by: bjoern <r10s@b44t.com>
Simon Laux 2 年之前
父节点
当前提交
8313cbc84c

+ 1 - 2
deltachat-ios/Controller/SettingsController.swift

@@ -122,7 +122,6 @@ internal final class SettingsViewController: UITableViewController, ProgressAler
         cell.tag = CellTags.videoChat.rawValue
         cell.textLabel?.text = String.localized("videochat_instance")
         cell.accessoryType = .disclosureIndicator
-        cell.detailTextLabel?.text = dcContext.getConfig("webrtc_instance")
         return cell
     }()
 
@@ -544,7 +543,7 @@ internal final class SettingsViewController: UITableViewController, ProgressAler
         mediaQualityCell.detailTextLabel?.text = MediaQualityController.getValString(val: dcContext.getConfigInt("media_quality"))
         downloadOnDemandCell.detailTextLabel?.text = DownloadOnDemandViewController.getValString(
             val: dcContext.getConfigInt("download_limit"))
-        videoChatInstanceCell.detailTextLabel?.text = dcContext.getConfig("webrtc_instance")
+        videoChatInstanceCell.detailTextLabel?.text = SettingsVideoChatViewController.getValString(val: dcContext.getConfig("webrtc_instance") ?? "")
         autodelCell.detailTextLabel?.text = autodelSummary()
         connectivityCell.detailTextLabel?.text = DcUtils.getConnectivityString(dcContext: dcContext,
                                                                                connectedString: String.localized("connectivity_connected"))

+ 135 - 8
deltachat-ios/Controller/SettingsVideoChatViewController.swift

@@ -1,21 +1,70 @@
 import UIKit
 import DcCore
-class SettingsVideoChatViewController: UITableViewController {
 
+private class PredefinedOption {
+    public var label: String
+    public var url: String
+
+    init(label: String, url: String) {
+        self.label = label
+        self.url = url
+    }
+}
+
+private class PredefinedOptionCell: UITableViewCell {
+    public var url: String
+
+    init(label: String, url: String) {
+        self.url = url
+        super.init(style: UITableViewCell.CellStyle.subtitle, reuseIdentifier: label)
+        self.textLabel?.text = label
+        self.detailTextLabel?.text = url
+        self.detailTextLabel?.textColor = .lightGray
+    }
+
+    required init?(coder: NSCoder) {
+        fatalError("init(coder:) has not been implemented")
+    }
+}
+
+class SettingsVideoChatViewController: UITableViewController {
     private var dcContext: DcContext
 
-    private lazy var videoInstanceCell: TextFieldCell = {
-        let cell = TextFieldCell.makeConfigCell(labelID: String.localized("videochat_instance"),
+    private static let predefinedOptions = [
+        PredefinedOption(label: "Jitsi", url: "https://meet.jit.si/$ROOM"),
+        PredefinedOption(label: "Systemli", url: "https://meet.systemli.org/$ROOM"),
+        PredefinedOption(label: "Autistici", url: "https://vc.autistici.org/$ROOM"),
+    ]
+
+    private lazy var offCell: UITableViewCell = {
+        let cell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "off")
+        cell.textLabel?.text = String.localized("off")
+        return cell
+    }()
+
+    private lazy var predefinedCells: [PredefinedOptionCell] = {
+        var ret: [PredefinedOptionCell] = []
+        for option in SettingsVideoChatViewController.predefinedOptions {
+            ret.append(PredefinedOptionCell(label: option.label, url: option.url))
+        }
+        return ret
+    }()
+
+    private lazy var customInstanceCell: TextFieldCell = {
+        let cell = TextFieldCell.makeConfigCell(labelID: String.localized("custom"),
                                                 placeholderID: String.localized("videochat_instance_placeholder"))
         cell.textField.autocapitalizationType = .none
         cell.textField.autocorrectionType = .no
         cell.textField.textContentType = .URL
+        cell.textField.addTarget(self, action: #selector(setCustom), for: .editingDidBegin)
+        cell.textField.addTarget(self, action: #selector(setCustom), for: .valueChanged)
         return cell
     }()
 
     init(dcContext: DcContext) {
         self.dcContext = dcContext
         super.init(style: .grouped)
+        self.updateSelected(selectedCustom: false)
         self.title = String.localized("videochat_instance")
         hidesBottomBarWhenPushed = true
     }
@@ -23,7 +72,16 @@ class SettingsVideoChatViewController: UITableViewController {
     required init?(coder aDecoder: NSCoder) {
         fatalError("init(coder:) has not been implemented")
     }
-    
+
+    static func getValString(val: String) -> String {
+        for option in predefinedOptions {
+            if option.url == val {
+                return option.label
+            }
+        }
+        return val
+    }
+
     // MARK: - Table view data source
 
     override func numberOfSections(in tableView: UITableView) -> Int {
@@ -31,16 +89,81 @@ class SettingsVideoChatViewController: UITableViewController {
     }
 
     override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
-        return 1
+        return self.predefinedCells.count + 2
     }
 
     override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
+        // selection changed, save value of custom field to ui config
+        dcContext.setConfig("ui.custom_webrtc_instance", customInstanceCell.getText())
+        
+        var newInstance: String?
+        var selectedCustom = false
+        if indexPath.row == 0 {
+            newInstance = ""
+            self.view.endEditing(true)
+        } else if indexPath.row <= self.predefinedCells.count {
+            newInstance = self.predefinedCells[indexPath.row-1].url
+            self.view.endEditing(true)
+        } else {
+            newInstance = customInstanceCell.getText()
+            selectedCustom = true
+        }
+        
+        dcContext.setConfig("webrtc_instance", newInstance)
+        updateSelected(selectedCustom: selectedCustom)
         tableView.deselectRow(at: indexPath, animated: true)
     }
 
+    // called when editing the value of the custom field or clicking on it
+    @objc private func setCustom(_ textField: UITextField) {
+        let newInstance = customInstanceCell.getText()
+        dcContext.setConfig("ui.custom_webrtc_instance", newInstance)
+        dcContext.setConfig("webrtc_instance", newInstance)
+        // force select custom
+        updateSelected(selectedCustom: true)
+    }
+
     override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
-        videoInstanceCell.textField.text = dcContext.getConfig("webrtc_instance")
-        return videoInstanceCell
+        if indexPath.row == 0 {
+            return offCell
+        } else if indexPath.row <= self.predefinedCells.count {
+            return self.predefinedCells[indexPath.row-1]
+        } else {
+            return customInstanceCell
+        }
+    }
+
+    var isCustom = false
+    func updateSelected(selectedCustom: Bool) {
+        self.isCustom = false
+        var notDefault = true
+        let currentUrl = dcContext.getConfig("webrtc_instance")
+        // set selection
+        for option in self.predefinedCells {
+            if option.url == currentUrl {
+                option.accessoryType = .checkmark
+                notDefault = false
+            } else {
+                option.accessoryType = .none
+            }
+        }
+
+        if notDefault {
+            if (currentUrl?.isEmpty) != nil || selectedCustom {
+                self.offCell.accessoryType = .none
+                customInstanceCell.accessoryType = .checkmark
+                customInstanceCell.textField.text = currentUrl
+                self.isCustom = true
+            } else {
+                self.offCell.accessoryType = .checkmark
+                customInstanceCell.accessoryType = .none
+                customInstanceCell.textField.text = dcContext.getConfig("ui.custom_webrtc_instance")
+            }
+        } else {
+            customInstanceCell.accessoryType = .none
+            customInstanceCell.textField.text = dcContext.getConfig("ui.custom_webrtc_instance")
+            self.offCell.accessoryType = .none
+        }
     }
 
     override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
@@ -48,6 +171,10 @@ class SettingsVideoChatViewController: UITableViewController {
     }
 
     override func viewWillDisappear(_ animated: Bool) {
-        dcContext.setConfig("webrtc_instance", videoInstanceCell.getText())
+        let customTextField = customInstanceCell.getText()
+        dcContext.setConfig("ui.custom_webrtc_instance", customTextField)
+        if self.isCustom {
+            dcContext.setConfig("webrtc_instance", customTextField)
+        }
     }
 }