Quellcode durchsuchen

replace magic numbers and hardcoded strings by constants in AccountSetupController, improve moving through text fields

cyberta vor 5 Jahren
Ursprung
Commit
afaabd8591
1 geänderte Dateien mit 84 neuen und 51 gelöschten Zeilen
  1. 84 51
      deltachat-ios/Controller/AccountSetupController.swift

+ 84 - 51
deltachat-ios/Controller/AccountSetupController.swift

@@ -12,6 +12,30 @@ class AccountSetupController: UITableViewController {
     private var configureProgressObserver: Any?
     private var oauth2Observer: Any?
 
+    private let tagEmailCell = 0
+    private let tagPasswordCell = 1
+    private let tagAdvancedCell = 2
+    private let tagImapServerCell = 3
+    private let tagImapUserCell = 4
+    private let tagImapPortCell = 5
+    private let tagImapSecurityCell = 6
+    private let tagSmtpServerCell = 7
+    private let tagSmtpUserCell = 8
+    private let tagSmtpPortCell = 9
+    private let tagSmtpPasswordCell = 10
+    private let tagSmtpSecurityCell = 11
+    private let tagEmptyServerCell = 12
+    private let tagDeleteAccountCell = 13
+    private let tagRestoreCell = 14
+
+    private let tagTextFieldEmail = 100
+    private let tagTextFieldPassword = 200
+    private let tagTextFieldImapServer = 300
+    private let tagTextFieldImapLogin = 400
+    private let tagTextFieldSmtpServer = 500
+    private let tagTextFieldSmtpUser = 600
+    private let tagTextFieldSmtpPassword = 700
+
 
     // the progress dialog
 
@@ -82,8 +106,8 @@ class AccountSetupController: UITableViewController {
 
     private lazy var emailCell: TextFieldCell = {
         let cell = TextFieldCell.makeEmailCell(delegate: self)
-        cell.textField.tag = 0
-        cell.textField.accessibilityIdentifier = "emailTextField" // will be used to eventually show oAuth-Dialogue when pressing return key
+        cell.tag = tagEmailCell
+        cell.textField.tag = tagTextFieldEmail // will be used to eventually show oAuth-Dialogue when pressing return key
         cell.setText(text: DcConfig.addr ?? nil)
         cell.textField.delegate = self
         cell.textField.addTarget(self, action: #selector(textFieldDidChange), for: .editingChanged)
@@ -92,10 +116,11 @@ class AccountSetupController: UITableViewController {
 
     private lazy var passwordCell: TextFieldCell = {
         let cell = TextFieldCell.makePasswordCell(delegate: self)
-        cell.textField.tag = 1
-        cell.accessibilityIdentifier = "passwordCell" // will be used to eventually show oAuth-Dialogue when selecting
+        cell.tag = tagPasswordCell
+        cell.textField.tag = tagTextFieldPassword  // will be used to eventually show oAuth-Dialogue when selecting
         cell.textField.addTarget(self, action: #selector(textFieldDidChange), for: .editingChanged)
         cell.setText(text: DcConfig.mailPw ?? nil)
+        cell.textField.delegate = self
         return cell
     }()
 
@@ -103,7 +128,7 @@ class AccountSetupController: UITableViewController {
         let cell = UITableViewCell(style: .value1, reuseIdentifier: nil)
         cell.textLabel?.text = String.localized("import_backup_title")
         cell.accessoryType = .disclosureIndicator
-        cell.accessibilityIdentifier = "restoreCell"
+        cell.tag = tagRestoreCell
         return cell
     }()
 
@@ -111,7 +136,7 @@ class AccountSetupController: UITableViewController {
         let cell = ActionCell(frame: .zero)
         cell.actionTitle = String.localized("pref_empty_server_title")
         cell.actionColor = UIColor.red
-        cell.accessibilityIdentifier = "emptyServerCell"
+        cell.tag = tagEmptyServerCell
         return cell
     }()
 
@@ -119,7 +144,7 @@ class AccountSetupController: UITableViewController {
         let cell = ActionCell(frame: .zero)
         cell.actionTitle = String.localized("delete_account")
         cell.actionColor = UIColor.red
-        cell.accessibilityIdentifier = "deleteAccountCell"
+        cell.tag = tagDeleteAccountCell
         return cell
     }()
 
@@ -127,7 +152,7 @@ class AccountSetupController: UITableViewController {
         let cell = UITableViewCell(style: .value1, reuseIdentifier: nil)
         cell.textLabel?.text = String.localized("menu_advanced")
         cell.accessoryType = .disclosureIndicator
-        cell.accessibilityIdentifier = "advancedShowCell"
+        cell.tag = tagAdvancedCell
         return cell
     }()
 
@@ -135,8 +160,8 @@ class AccountSetupController: UITableViewController {
         let cell = TextFieldCell(descriptionID: "login_imap_server",
                                  placeholder: DcConfig.mailServer ?? DcConfig.configuredMailServer,
                                  delegate: self)
-        cell.accessibilityIdentifier = "IMAPServerCell"
-        cell.textField.tag = 2
+        cell.tag = tagImapServerCell
+        cell.textField.tag = tagTextFieldImapServer
         cell.textField.autocorrectionType = .no
         cell.textField.spellCheckingType = .no
         cell.textField.autocapitalizationType = .none
@@ -145,8 +170,8 @@ class AccountSetupController: UITableViewController {
 
     lazy var imapUserCell: TextFieldCell = {
         let cell = TextFieldCell(descriptionID: "login_imap_login", placeholder: DcConfig.mailUser ?? DcConfig.configuredMailUser, delegate: self)
-        cell.accessibilityIdentifier = "IMAPUserCell"
-        cell.textField.tag = 3
+        cell.textField.tag = tagTextFieldImapLogin
+        cell.tag = tagImapUserCell
         return cell
     }()
 
@@ -155,7 +180,7 @@ class AccountSetupController: UITableViewController {
         cell.textLabel?.text = String.localized("login_imap_port")
         cell.accessoryType = .disclosureIndicator
         cell.detailTextLabel?.text = DcConfig.mailPort ?? DcConfig.configuredMailPort
-        cell.accessibilityIdentifier = "IMAPPortCell"
+        cell.tag = tagImapPortCell
         cell.selectionStyle = .none
         return cell
     }()
@@ -164,11 +189,10 @@ class AccountSetupController: UITableViewController {
         let text = "\(DcConfig.getImapSecurity())"
         let cell = UITableViewCell(style: .value1, reuseIdentifier: nil)
         cell.textLabel?.text = String.localized("login_imap_security")
-        // let cell = TextFieldCell(description: "IMAP Security", placeholder: text, delegate: self)
-        cell.accessibilityIdentifier = "IMAPSecurityCell"
         cell.accessoryType = .disclosureIndicator
         cell.detailTextLabel?.text = "\(DcConfig.getImapSecurity())"
         cell.selectionStyle = .none
+        cell.tag = tagImapSecurityCell
         return cell
     }()
 
@@ -176,8 +200,8 @@ class AccountSetupController: UITableViewController {
         let cell = TextFieldCell(descriptionID: "login_smtp_server",
                                  placeholder: DcConfig.sendServer ?? DcConfig.configuredSendServer,
                                  delegate: self)
-        cell.accessibilityIdentifier = "SMTPServerCell"
-        cell.textField.tag = 4
+        cell.textField.tag = tagTextFieldSmtpServer
+        cell.tag = tagSmtpServerCell
         cell.textField.autocorrectionType = .no
         cell.textField.spellCheckingType = .no
         cell.textField.autocapitalizationType = .none
@@ -186,8 +210,8 @@ class AccountSetupController: UITableViewController {
 
     lazy var smtpUserCell: TextFieldCell = {
         let cell = TextFieldCell(descriptionID: "login_smtp_login", placeholder: DcConfig.sendUser ?? DcConfig.configuredSendUser, delegate: self)
-        cell.accessibilityIdentifier = "SMTPUserCell"
-        cell.textField.tag = 5
+        cell.textField.tag = tagTextFieldSmtpUser
+        cell.tag = tagSmtpUserCell
         return cell
     }()
 
@@ -196,8 +220,8 @@ class AccountSetupController: UITableViewController {
         cell.textLabel?.text = String.localized("login_smtp_port")
         cell.accessoryType = .disclosureIndicator
         cell.detailTextLabel?.text = DcConfig.sendPort ?? DcConfig.configuredSendPort
-        cell.accessibilityIdentifier = "SMTPPortCell"
         cell.selectionStyle = .none
+        cell.tag = tagSmtpPortCell
         return cell
     }()
 
@@ -205,8 +229,8 @@ class AccountSetupController: UITableViewController {
         let cell = TextFieldCell(descriptionID: "login_smtp_password", placeholder: "*************", delegate: self)
         cell.textField.textContentType = UITextContentType.password
         cell.textField.isSecureTextEntry = true
-        cell.accessibilityIdentifier = "SMTPPasswordCell"
-        cell.textField.tag = 6
+        cell.textField.tag = tagTextFieldSmtpPassword
+        cell.tag = tagSmtpPasswordCell
         return cell
     }()
 
@@ -215,7 +239,7 @@ class AccountSetupController: UITableViewController {
         let cell = UITableViewCell(style: .value1, reuseIdentifier: nil)
         cell.textLabel?.text = String.localized("login_smtp_security")
         cell.detailTextLabel?.text = security
-        cell.accessibilityIdentifier = "SMTPSecurityCell"
+        cell.tag = tagSmtpSecurityCell
         cell.accessoryType = .disclosureIndicator
         cell.selectionStyle = .none
         return cell
@@ -421,31 +445,32 @@ class AccountSetupController: UITableViewController {
     override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
         guard let tappedCell = tableView.cellForRow(at: indexPath) else { return }
         // handle tap on password -> show oAuthDialogue
-        if let textFieldCell = tappedCell as? TextFieldCell {
-            if textFieldCell.accessibilityIdentifier == "passwordCell" {
+        switch tappedCell.tag {
+        case tagPasswordCell:
+            if let textFieldCell = tappedCell as? TextFieldCell {
                 if let emailAdress = textFieldCell.getText() {
                     _ = showOAuthAlertIfNeeded(emailAddress: emailAdress, handleCancel: nil)
                 }
             }
-        }
-
-        if tappedCell.accessibilityIdentifier == "restoreCell" {
+        case tagRestoreCell:
             tableView.reloadData() // otherwise the disclosureIndicator may stay selected
             restoreBackup()
-        } else if tappedCell.accessibilityIdentifier == "emptyServerCell" {
+        case tagEmptyServerCell:
             emptyServer()
-        } else if tappedCell.accessibilityIdentifier == "deleteAccountCell" {
+        case tagDeleteAccountCell:
             deleteAccount()
-        } else if tappedCell.accessibilityIdentifier == "advancedShowCell" {
+        case tagAdvancedCell:
             toggleAdvancedSection()
-        } else if tappedCell.accessibilityIdentifier == "IMAPPortCell" {
+        case tagImapPortCell:
             coordinator?.showImapPortOptions()
-        } else if tappedCell.accessibilityIdentifier == "SMTPPortCell" {
+        case tagSmtpPortCell:
             coordinator?.showSmtpPortsOptions()
-        } else if tappedCell.accessibilityIdentifier == "IMAPSecurityCell" {
+        case tagImapSecurityCell:
             coordinator?.showImapSecurityOptions()
-        } else if tappedCell.accessibilityIdentifier == "SMTPSecurityCell" {
+        case tagSmtpSecurityCell:
             coordinator?.showSmptpSecurityOptions()
+        default:
+            break
         }
     }
 
@@ -602,29 +627,31 @@ class AccountSetupController: UITableViewController {
     private func evaluateAdvancedSetup() {
         for cell in advancedSectionCells {
             if let textFieldCell = cell as? TextFieldCell {
-                switch cell.accessibilityIdentifier {
-                case "IMAPServerCell":
+                switch  textFieldCell.tag {
+                case tagImapServerCell:
                     DcConfig.mailServer = textFieldCell.getText() ?? nil
-                case "IMAPUserCell":
+                case tagImapUserCell:
                     DcConfig.mailUser = textFieldCell.getText() ?? nil
-                case "IMAPPortCell":
+                case tagImapPortCell:
                     DcConfig.mailPort = textFieldCell.getText() ?? nil
-                case "IMAPSecurityCell":
+                case tagImapSecurityCell:
+                    ///FIXME: set flags correctly
                     let flag = 0
                     DcConfig.setImapSecurity(imapFlags: flag)
-                case "SMTPServerCell":
+                case tagSmtpServerCell:
                     DcConfig.sendServer = textFieldCell.getText() ?? nil
-                case "SMTPUserCell":
+                case tagSmtpUserCell:
                     DcConfig.sendUser = textFieldCell.getText() ?? nil
-                case "SMTPPortCell":
+                case tagSmtpPortCell:
                     DcConfig.sendPort = textFieldCell.getText() ?? nil
-                case "SMTPPasswordCell":
+                case tagSmtpPasswordCell:
                     DcConfig.sendPw = textFieldCell.getText() ?? nil
-                case "SMTPSecurityCell":
+                case tagSmtpSecurityCell:
+                    ///FIXME: set flags correctly
                     let flag = 0
                     DcConfig.setSmtpSecurity(smptpFlags: flag)
                 default:
-                    logger.info("unknown identifier", cell.accessibilityIdentifier ?? "")
+                    logger.info("unknown identifier \(cell.tag)")
                 }
             }
         }
@@ -771,25 +798,31 @@ class AccountSetupController: UITableViewController {
 extension AccountSetupController: UITextFieldDelegate {
     func textFieldShouldReturn(_ textField: UITextField) -> Bool {
         let currentTag = textField.tag
-        if let nextField = tableView.viewWithTag(currentTag + 1) as? UITextField {
-            if nextField.tag > 1, !advancedSectionShowing {
+        if let nextField = tableView.viewWithTag(currentTag + 100) as? UITextField {
+            if nextField.tag > tagTextFieldPassword, !advancedSectionShowing {
                 // gets here when trying to activate a collapsed cell
                 return false
             }
             nextField.becomeFirstResponder()
+            return true
+        } else {
+            textField.resignFirstResponder()
+            emailCell.textField.becomeFirstResponder()
+            let indexPath = IndexPath(row: 0, section: 0)
+            tableView.scrollToRow(at: indexPath, at: UITableView.ScrollPosition.top, animated: true)
+            return true
         }
-        return false
     }
 
     func textFieldDidBeginEditing(_ textField: UITextField) {
-        if textField.accessibilityIdentifier == "emailTextField" {
+        if textField.tag == tagTextFieldEmail {
             // this will re-enable possible oAuth2-login
             skipOauth = false
         }
     }
 
     func textFieldDidEndEditing(_ textField: UITextField) {
-        if textField.accessibilityIdentifier == "emailTextField" {
+        if textField.tag == tagTextFieldEmail {
             let _ = showOAuthAlertIfNeeded(emailAddress: textField.text ?? "", handleCancel: {
                 self.passwordCell.textField.becomeFirstResponder()
             })