Ver Fonte

return key on email field will change focus to pw field - even when oAuth was triggered and canceled

Bastian van de Wetering há 6 anos atrás
pai
commit
c8fd3e6e4d

+ 4 - 0
deltachat-ios.xcodeproj/project.pbxproj

@@ -95,6 +95,7 @@
 		AEACE2E51FB32E1900DCDD78 /* Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEACE2E41FB32E1900DCDD78 /* Utils.swift */; };
 		AEE56D762253431E007DC082 /* AccountSetupController.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEE56D752253431E007DC082 /* AccountSetupController.swift */; };
 		AEE56D7D2253ADB4007DC082 /* HudHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEE56D7C2253ADB4007DC082 /* HudHandler.swift */; };
+		AEE56D80225504DB007DC082 /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = AEE56D7F225504DB007DC082 /* Extensions.swift */; };
 /* End PBXBuildFile section */
 
 /* Begin PBXContainerItemProxy section */
@@ -269,6 +270,7 @@
 		AEACE2E41FB32E1900DCDD78 /* Utils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Utils.swift; sourceTree = "<group>"; };
 		AEE56D752253431E007DC082 /* AccountSetupController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountSetupController.swift; sourceTree = "<group>"; };
 		AEE56D7C2253ADB4007DC082 /* HudHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HudHandler.swift; sourceTree = "<group>"; };
+		AEE56D7F225504DB007DC082 /* Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Extensions.swift; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
 /* Begin PBXFrameworksBuildPhase section */
@@ -293,6 +295,7 @@
 			children = (
 				78E45E4321D3F14A00D4B15E /* UIImage+Extension.swift */,
 				78E45E4121D3DB4000D4B15E /* UIViewController+Extension.swift */,
+				AEE56D7F225504DB007DC082 /* Extensions.swift */,
 			);
 			path = Extensions;
 			sourceTree = "<group>";
@@ -808,6 +811,7 @@
 				7070FB7420FF345F000DC258 /* dc_e2ee.c in Sources */,
 				AE0D26FD1FB1FE88002FAFCE /* ChatListController.swift in Sources */,
 				7070FB6920FF345F000DC258 /* dc_mimeparser.c in Sources */,
+				AEE56D80225504DB007DC082 /* Extensions.swift in Sources */,
 				7A0052C81FBE6CB40048C3BF /* NewContactController.swift in Sources */,
 				AEE56D762253431E007DC082 /* AccountSetupController.swift in Sources */,
 				7070FB6020FF345F000DC258 /* dc_hash.c in Sources */,

+ 43 - 13
deltachat-ios/AccountSetupController.swift

@@ -22,14 +22,25 @@ class AccountSetupController: UITableViewController {
 
     private lazy var emailCell:TextFieldCell = {
         let cell = TextFieldCell.makeEmailCell()
+		cell.textField.tag = 0
+		cell.textField.delegate = self
         return cell
     }()
 
     private lazy var passwordCell:TextFieldCell = {
 		let cell = TextFieldCell.makePasswordCell()
-        return cell
+		cell.textField.tag = 1
+		cell.textField.delegate = self
+		return cell
     }()
 
+	/*
+	Advanced Cells:
+	IMAP Server, IMAP User, IMAP Port, IMAP Security, SMTP Server, SMTP User, SMTP Port, SMTP Password, SMTP Security
+	*/
+
+
+
 	lazy var imapServerCell = TextFieldCell(description: "IMAP Server", placeholder: MRConfig.mailServer ?? MRConfig.configuredMailServer)
 	lazy var imapUserCell = TextFieldCell(description: "IMAP User", placeholder: MRConfig.mailUser ?? MRConfig.configuredMailUser)
 	lazy var imapPortCell = TextFieldCell(description: "IMAP Port", placeholder: MRConfig.mailPort ?? MRConfig.configuredMailPort)
@@ -43,10 +54,9 @@ class AccountSetupController: UITableViewController {
 
 
 
-    /*
-    Advanced Cells:
-     IMAP Server, IMAP User, IMAP Port, IMAP Security, SMTP Server, SMTP User, SMTP Port, SMTP Password, SMTP Security
-    */
+	// this loginButton can be enabled and disabled
+	let loginButton:UIBarButtonItem = UIBarButtonItem(title: "Login", style: .done, target: self, action: #selector(loginButtonPressed))
+
 
 	private lazy var basicSectionCells:[UITableViewCell] = [emailCell, passwordCell]
 	private lazy var advancedSectionCells:[UITableViewCell] = [imapServerCell,imapUserCell,imapPortCell,imapSecurityCell,smtpServerCell,smtpUserCell,smtpPortCell,smtpPasswordCell,smtpSecurityCell]
@@ -65,12 +75,13 @@ class AccountSetupController: UITableViewController {
         super.viewDidLoad()
         self.title = "Login to your server"
 		self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Close", style: .plain, target: self, action: #selector(closeButtonPressed))
-        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Login", style: .done, target: self, action: #selector(loginButtonPressed))
+        self.navigationItem.rightBarButtonItem = loginButton
     }
 
     override func viewDidAppear(_ animated: Bool) {
         super.viewDidAppear(animated)
         addProgressHudEventListener()
+		//loginButton.isEnabled = false
     }
 
     override func viewDidDisappear(_ animated: Bool) {
@@ -144,16 +155,13 @@ class AccountSetupController: UITableViewController {
             // advancedSection
             return advancedSectionCells[row]
         }
-
-
-
     }
 
     override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
         // handle tap on password 
         if indexPath.section == 0 && indexPath.row == 1 {
             if let emailAdress = emailCell.getText() {
-                let _ = showOAuthAlertIfNeeded(emailAddress: emailAdress)
+				_ = showOAuthAlertIfNeeded(emailAddress: emailAdress, handleCancel: nil)
             } else {
                 return
             }
@@ -189,7 +197,7 @@ class AccountSetupController: UITableViewController {
             return // handle case when either email or pw fields are empty
         }
 
-        let oAuthStarted = showOAuthAlertIfNeeded(emailAddress: emailAddress)
+        let oAuthStarted = showOAuthAlertIfNeeded(emailAddress: emailAddress, handleCancel: nil)
 
         if oAuthStarted {
             // the loginFlow will be handled by oAuth2
@@ -209,7 +217,7 @@ class AccountSetupController: UITableViewController {
 	}
 
     // returns true if needed
-    private func showOAuthAlertIfNeeded(emailAddress: String) -> Bool {
+	private func showOAuthAlertIfNeeded(emailAddress: String, handleCancel: (()->())?) -> Bool {
         if oAuthDenied {
             return false
         }
@@ -229,6 +237,7 @@ class AccountSetupController: UITableViewController {
             let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: {
                _ in
                 self.oAuthDenied = true
+				handleCancel?()
             })
             oAuthAlertController.addAction(confirm)
             oAuthAlertController.addAction(cancel)
@@ -281,6 +290,27 @@ class AccountSetupController: UITableViewController {
     }
 }
 
+extension AccountSetupController: UITextFieldDelegate {
+	func textFieldShouldReturn(_ textField: UITextField) -> Bool {
+		let currentTag = textField.tag
+
+		if currentTag == 0 {
+			// special case: email field should check for potential oAuth
+			showOAuthAlertIfNeeded(emailAddress: textField.text ?? "", handleCancel: {
+				self.passwordCell.textField.becomeFirstResponder()
+			})
+		}
+
+
+		if let nextField = tableView.viewWithTag(currentTag + 1) as? UITextField {
+			nextField.becomeFirstResponder()
+		}
+
+
+		return false
+	}
+}
+
 class AdvancedSectionHeader: UIView {
 
     var handleTap:((UIButton) -> ())?
@@ -321,7 +351,7 @@ class AdvancedSectionHeader: UIView {
         self.addSubview(toggleButton)
         toggleButton.translatesAutoresizingMaskIntoConstraints = false
 
-        toggleButton.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -20).isActive = true
+        toggleButton.leadingAnchor.constraint(equalTo: self.trailingAnchor, constant: -60).isActive = true	// since button will change title it should be left aligned
         toggleButton.centerYAnchor.constraint(equalTo: label.centerYAnchor, constant: 0).isActive = true
 
     }

+ 16 - 0
deltachat-ios/Extensions/Extensions.swift

@@ -0,0 +1,16 @@
+//
+//  String+Extension.swift
+//  deltachat-ios
+//
+//  Created by Bastian van de Wetering on 03.04.19.
+//  Copyright © 2019 Jonas Reinsch. All rights reserved.
+//
+
+import Foundation
+
+extension String {
+
+	func containsCharacters() -> Bool {
+		return !self.trimmingCharacters(in: [" "]).isEmpty
+	}
+}

+ 1 - 1
deltachat-ios/TextFieldCell.swift

@@ -15,7 +15,7 @@ class TextFieldCell: UITableViewCell {
     lazy var textField: UITextField = {
         let textField = UITextField()
         textField.textAlignment = .right
-        textField.enablesReturnKeyAutomatically = true
+       // textField.enablesReturnKeyAutomatically = true
         textField.placeholder = self.placeholder
         // textField.backgroundColor = UIColor.lightGray
         return textField