Browse Source

streamline SecuritySettingsController: don't rely on string identifiers, localize values, replace back/reset buttons by cancel/ok

cyberta 5 years ago
parent
commit
cbd114012c

+ 55 - 92
deltachat-ios/Controller/AccountSetup/SecuritySettingsController.swift

@@ -2,32 +2,41 @@ import UIKit
 
 class SecuritySettingsController: UITableViewController {
 
-    private var options: [String]
-    private var selectedIndex: Int {
-        didSet {
-            print(selectedIndex)
-        }
-    }
+    private var options: [Int]
 
-    private var backupIndex: Int
+    private var selectedIndex: Int
 
-    var onDismiss: ((String) -> Void)?
+    private var securityType: SecurityType
 
-    private var resetButton: UIBarButtonItem!
+    private var okButton: UIBarButtonItem {
+        let button =  UIBarButtonItem(title: String.localized("ok"), style: .done, target: self, action: #selector(okButtonPressed))
+        return button
+    }
+
+    private var cancelButton: UIBarButtonItem {
+        let button =  UIBarButtonItem(title: String.localized("cancel"), style: .plain, target: self, action: #selector(cancelButtonPressed))
+        return button
+    }
 
     private var staticCells: [UITableViewCell] {
         return options.map {
             let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
-            cell.textLabel?.text = $0
+            cell.textLabel?.text = SecurityConverter.convertHexToString(type: self.securityType, hex: $0)
             cell.selectionStyle = .none
             return cell
         }
     }
 
-    init(title: String, options: [String], selectedOption: String) {
-        self.options = options
-        selectedIndex = options.index(of: selectedOption)!
-        backupIndex = selectedIndex
+    init(title: String, type: SecurityType) {
+        self.securityType = type
+        switch securityType {
+        case .IMAPSecurity:
+            options = [0x00, 0x100, 0x200, 0x400]
+            selectedIndex = options.index(of: DcConfig.getImapSecurity()) ?? 0
+        case .SMTPSecurity:
+            options = [0x00, 0x10000, 0x20000, 0x40000]
+            selectedIndex = options.index(of: DcConfig.getSmtpSecurity()) ?? 0
+        }
         super.init(style: .grouped)
         self.title = title
     }
@@ -38,14 +47,8 @@ class SecuritySettingsController: UITableViewController {
 
     override func viewDidLoad() {
         super.viewDidLoad()
-        resetButton = UIBarButtonItem(title: String.localized("reset"), style: .done, target: self, action: #selector(resetButtonPressed))
-        resetButton.isEnabled = false
-        navigationItem.rightBarButtonItem = resetButton
-    }
-
-    override func viewWillDisappear(_ animated: Bool) {
-        let selectedOption = options[selectedIndex]
-        onDismiss?(selectedOption)
+        navigationItem.rightBarButtonItem = okButton
+        navigationItem.leftBarButtonItem = cancelButton
     }
 
     // MARK: - Table view data source
@@ -78,87 +81,47 @@ class SecuritySettingsController: UITableViewController {
             cell.accessoryType = .checkmark
         }
         selectedIndex = indexPath.row
-        resetButton.isEnabled = true
     }
 
-    @objc func resetButtonPressed() {
-        selectedIndex = backupIndex
-        tableView.reloadData()
+    @objc func okButtonPressed() {
+        switch securityType {
+        case .IMAPSecurity:
+            DcConfig.setImapSecurity(imapFlags: options[selectedIndex])
+        case .SMTPSecurity:
+            DcConfig.setSmtpSecurity(smptpFlags: options[selectedIndex])
+        }
+        navigationController?.popViewController(animated: true)
     }
 
+    @objc func cancelButtonPressed() {
+        navigationController?.popViewController(animated: true)
+    }
 }
 
-
 enum SecurityType {
-    case IMAPSecurity
-    case SMTPSecurity
-}
-
-enum SecurityValue: String {
-    case AUTO = "Automatic"
-    case TLS = "SSL / TLS"
-    case STARTTLS = "STARTTLS"
-    case PLAIN = "OFF"
+     case IMAPSecurity
+     case SMTPSecurity
 }
 
 class SecurityConverter {
-
-    static func convertValueToInt(type: SecurityType, value: SecurityValue) -> Int {
-        switch type {
-        case .IMAPSecurity:
-            switch value {
-            case .AUTO:
-                return 0x000
-            case .STARTTLS:
-                return 0x100
-            case .TLS:
-                return 0x200
-            case .PLAIN:
-                return 0x400
-            }
-        case .SMTPSecurity:
-            switch value {
-            case .AUTO:
-                return 0x00000
-            case .STARTTLS:
-                return 0x10000
-            case .TLS:
-                return 0x20000
-            case .PLAIN:
-                return 0x40000
-            }
-        }
-    }
-
-    // TODO: discuss if we want to internationalize OFF and Automatic
     static func convertHexToString(type: SecurityType, hex value: Int) -> String {
-        switch type {
-        case .IMAPSecurity:
-            switch value {
-            case 0x00:
-                return "Automatic"
-            case 0x100:
-                return "STARTTLS"
-            case 0x200:
-                return "SSL / TLS"
-            case  0x400:
-                return "OFF"
-            default:
-                return "Undefined"
-            }
-        case .SMTPSecurity:
-            switch value {
-            case 0x00000:
-                return "Automatic"
-            case 0x10000:
-                return "STARTTLS"
-            case 0x20000:
-                return "SSL / TLS"
-            case  0x40000:
-                return "OFF"
-            default:
-                return "Undefined"
-            }
+        switch value {
+        case 0x00:
+            return String.localized("crtck_auto")
+        case 0x100:
+            return String.localized("crtck_starttls")
+        case 0x200:
+            return String.localized("crtck_ssltls")
+        case  0x400:
+            return String.localized("crtck_off")
+        case 0x10000:
+            return String.localized("crtck_starttls")
+        case 0x20000:
+            return String.localized("crtck_ssltls")
+        case  0x40000:
+            return String.localized("crtck_off")
+        default:
+            return "Undefined"
         }
     }
 }

+ 2 - 23
deltachat-ios/Coordinator/AppCoordinator.swift

@@ -212,18 +212,8 @@ class AccountSetupCoordinator: Coordinator {
     }
 
     func showImapSecurityOptions() {
-        let currentSecurityOption = DcConfig.getImapSecurity()
-        let convertedOption = SecurityConverter.convertHexToString(type: .IMAPSecurity, hex: currentSecurityOption)
         let securitySettingsController = SecuritySettingsController(title: String.localized("login_imap_security"),
-                                                                    options: ["Automatic", "SSL / TLS", "STARTTLS", "OFF"],
-                                                                    selectedOption: convertedOption)
-        securitySettingsController.onDismiss = {
-            option in
-            if let secValue = SecurityValue(rawValue: option) {
-                let value = SecurityConverter.convertValueToInt(type: .IMAPSecurity, value: secValue)
-                DcConfig.setImapSecurity(imapFlags: value)
-            }
-        }
+                                                                    type: SecurityType.IMAPSecurity)
         navigationController.pushViewController(securitySettingsController, animated: true)
     }
 
@@ -241,18 +231,7 @@ class AccountSetupCoordinator: Coordinator {
     }
 
     func showSmptpSecurityOptions() {
-        let currentSecurityOption = DcConfig.getSmtpSecurity()
-        let convertedOption = SecurityConverter.convertHexToString(type: .SMTPSecurity, hex: currentSecurityOption)
-        let securitySettingsController = SecuritySettingsController(title: String.localized("login_imap_security"),
-                                                                    options: ["Automatic", "SSL / TLS", "STARTTLS", "OFF"],
-                                                                    selectedOption: convertedOption)
-        securitySettingsController.onDismiss = {
-            option in
-            if let secValue = SecurityValue(rawValue: option) {
-                let value = SecurityConverter.convertValueToInt(type: .SMTPSecurity, value: secValue)
-                DcConfig.setSmtpSecurity(smptpFlags: value)
-            }
-        }
+        let securitySettingsController = SecuritySettingsController(title: String.localized("login_imap_security"), type: SecurityType.SMTPSecurity)
         navigationController.pushViewController(securitySettingsController, animated: true)
     }
 }

+ 4 - 0
deltachat-ios/en.lproj/Localizable.strings

@@ -629,3 +629,7 @@
 "complete" = "Complete";
 "info" = "Info";
 "qr_code_title" = "QR scan/show";
+"crtck_auto" = "Automatic";
+"crtck_starttls" = "STARTTLS";
+"crtck_ssltls" = "SSL / TLS";
+"crtck_off" = "Off";

+ 4 - 0
tools/untranslated.xml

@@ -19,6 +19,10 @@
     <string name="complete">Complete</string>
     <string name="info">Info</string>
     <string name="qr_code_title">QR scan/show</string>
+    <string name="crtck_auto">Automatic</string>
+    <string name="crtck_starttls">STARTTLS</string>
+    <string name="crtck_ssltls">SSL / TLS</string>
+    <string name="crtck_off">Off</string>
     <!-- info plist keys need to pre prefixed with INFOPLIST., spaces need to be replaced with dots and dashes need to replaced with underscores -->
     <string name="INFOPLIST.Privacy._.Camera.Usage.Description">Allowing access to the camera lets you take photos and videos.</string>
     <string name="INFOPLIST.Privacy._.Contacts.Usage.Description">Allowing access to your address book lets you chat with contacts from your device.</string>