CredentialsController.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. //
  2. // CredentialsController.swift
  3. // deltachat-ios
  4. //
  5. // Created by Jonas Reinsch on 15.11.17.
  6. // Copyright © 2017 Jonas Reinsch. All rights reserved.
  7. //
  8. import UIKit
  9. // valid security modes for IMAP and SMTP
  10. enum SecurityMode: String, CaseIterable {
  11. case automatic = "Automatic"
  12. case ssltls = "SSL/TLS"
  13. case starttls = "STARTTLS"
  14. case off = "Off"
  15. }
  16. typealias CredentialsModel = (
  17. email: String,
  18. password: String,
  19. imapLoginName: String?,
  20. imapServer: String?,
  21. imapPort: String?,
  22. imapSecurity: SecurityMode,
  23. smtpLoginName: String?,
  24. smtpPassword: String?,
  25. smtpServer: String?,
  26. smtpPort: String?,
  27. smtpSecurity: SecurityMode
  28. )
  29. class CredentialsController: UITableViewController {
  30. let emailCell = TextFieldCell.makeEmailCell()
  31. let passwordCell = TextFieldCell.makePasswordCell()
  32. let imapCellLoginName = TextFieldCell.makeConfigCell(label: "IMAP Login Name", placeholder: "Automatic")
  33. let imapCellServer = TextFieldCell.makeConfigCell(label: "IMAP Server", placeholder: "Automatic")
  34. let imapCellPort = TextFieldCell.makeConfigCell(label: "IMAP Port", placeholder: "Automatic")
  35. let imapCellSecurity = UITableViewCell(style: UITableViewCell.CellStyle.value1, reuseIdentifier: nil)
  36. let smtpCellLoginName = TextFieldCell.makeConfigCell(label: "SMTP Login Name", placeholder: "Automatic")
  37. let smtpCellPassword = TextFieldCell.makeConfigCell(label: "SMTP Password", placeholder: "As above")
  38. let smtpCellServer = TextFieldCell.makeConfigCell(label: "SMTP Server", placeholder: "Automatic")
  39. let smtpCellPort = TextFieldCell.makeConfigCell(label: "SMTP Port", placeholder: "Automatic")
  40. let smtpCellSecurity = UITableViewCell(style: UITableViewCell.CellStyle.value1, reuseIdentifier: nil)
  41. var doneButton: UIBarButtonItem?
  42. var advancedButton: UIBarButtonItem?
  43. let progressBar = UIProgressView(progressViewStyle: .default)
  44. func readyForLogin() -> Bool {
  45. return Utils.isValid(model.email) && !model.password.isEmpty
  46. }
  47. var advancedMode = false {
  48. didSet {
  49. if advancedMode {
  50. advancedButton?.title = "Standard"
  51. } else {
  52. advancedButton?.title = "Advanced"
  53. }
  54. tableView.reloadData()
  55. }
  56. }
  57. var model: CredentialsModel = ("", "", nil, nil, nil, SecurityMode.automatic, nil, nil, nil, nil, SecurityMode.automatic) {
  58. didSet {
  59. if readyForLogin() {
  60. doneButton?.isEnabled = true
  61. } else {
  62. doneButton?.isEnabled = false
  63. }
  64. smtpCellSecurity.detailTextLabel?.text = model.smtpSecurity.rawValue
  65. imapCellSecurity.detailTextLabel?.text = model.imapSecurity.rawValue
  66. logger.info("model: \(model)")
  67. }
  68. }
  69. let cells: [UITableViewCell]
  70. let isCancellable: Bool
  71. init(isCancellable: Bool = false) {
  72. cells = [emailCell, passwordCell]
  73. self.isCancellable = isCancellable
  74. super.init(style: .grouped)
  75. doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(didPressSaveAccountButton))
  76. doneButton?.isEnabled = false
  77. advancedButton = UIBarButtonItem(title: "Advanced", style: .done, target: self, action: #selector(didPressAdvancedButton))
  78. let cancelButton = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(didPressCancelButton))
  79. if isCancellable {
  80. navigationItem.rightBarButtonItems = [doneButton!, cancelButton]
  81. } else {
  82. navigationItem.rightBarButtonItem = doneButton
  83. }
  84. navigationItem.leftBarButtonItem = advancedButton
  85. // FIXME: refactor: do not use target/action here for text field changes
  86. // but text field delegate
  87. emailCell.textField.addTarget(self, action: #selector(emailTextChanged), for: UIControl.Event.editingChanged)
  88. passwordCell.textField.addTarget(self, action: #selector(passwordTextChanged), for: UIControl.Event.editingChanged)
  89. imapCellLoginName.textField.addTarget(self, action: #selector(imapLoginNameChanged), for: .editingChanged)
  90. imapCellServer.textField.addTarget(self, action: #selector(imapServerChanged), for: .editingChanged)
  91. imapCellPort.textField.addTarget(self, action: #selector(imapPortChanged), for: .editingChanged)
  92. smtpCellLoginName.textField.addTarget(self, action: #selector(smtpLoginNamedChanged), for: .editingChanged)
  93. smtpCellPassword.textField.addTarget(self, action: #selector(smtpPasswordChanged), for: .editingChanged)
  94. smtpCellServer.textField.addTarget(self, action: #selector(smtpServerChanged), for: .editingChanged)
  95. smtpCellPort.textField.addTarget(self, action: #selector(smtpPortChanged), for: .editingChanged)
  96. emailCell.textField.textContentType = UITextContentType.emailAddress
  97. emailCell.textField.delegate = self
  98. passwordCell.textField.delegate = self
  99. emailCell.textField.returnKeyType = .next
  100. passwordCell.textField.returnKeyType = .done
  101. }
  102. override func viewDidAppear(_: Bool) {
  103. emailCell.textField.becomeFirstResponder()
  104. }
  105. @objc func didPressCancelButton() {
  106. dismiss(animated: false) {
  107. AppDelegate.appCoordinator.setupInnerViewControllers()
  108. }
  109. }
  110. @objc func didPressSaveAccountButton() {
  111. let m = model
  112. let a = advancedMode
  113. dismiss(animated: true) {
  114. initCore(withCredentials: true, advancedMode: a, model: m, cancellableCredentialsUponFailure: self.isCancellable)
  115. }
  116. }
  117. @objc func didPressAdvancedButton() {
  118. advancedMode = !advancedMode
  119. }
  120. required init?(coder _: NSCoder) {
  121. fatalError("init(coder:) has not been implemented")
  122. }
  123. override func viewDidLoad() {
  124. super.viewDidLoad()
  125. title = "Settings"
  126. navigationController?.navigationBar.prefersLargeTitles = true
  127. }
  128. override func numberOfSections(in _: UITableView) -> Int {
  129. return advancedMode ? 3 : 1
  130. }
  131. override func tableView(_: UITableView, numberOfRowsInSection section: Int) -> Int {
  132. if section == 0 {
  133. return cells.count
  134. }
  135. if section == 1 {
  136. return 4
  137. }
  138. if section == 2 {
  139. return 5
  140. }
  141. return 0 // should never happen
  142. }
  143. override func tableView(_: UITableView, titleForHeaderInSection section: Int) -> String? {
  144. if section == 1 {
  145. return "IMAP"
  146. }
  147. if section == 2 {
  148. return "SMTP"
  149. }
  150. return nil
  151. }
  152. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  153. let isIMAP = indexPath.section == 1
  154. let isSMTP = indexPath.section == 2
  155. if (isIMAP && (indexPath.row == 3)) || (isSMTP && (indexPath.row == 4)) {
  156. let actionSheet = UIAlertController(title: "Security", message: nil, preferredStyle: .actionSheet)
  157. for securityMode in SecurityMode.allCases {
  158. actionSheet.addAction(UIAlertAction(title: securityMode.rawValue, style: .default, handler: {
  159. [unowned self]
  160. _ in
  161. if isIMAP {
  162. self.model.imapSecurity = securityMode
  163. }
  164. if isSMTP {
  165. self.model.smtpSecurity = securityMode
  166. }
  167. }))
  168. }
  169. actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { _ in }))
  170. if let popoverController = actionSheet.popoverPresentationController {
  171. if let cell = tableView.cellForRow(at: indexPath) {
  172. popoverController.sourceView = cell.detailTextLabel
  173. }
  174. }
  175. present(actionSheet, animated: true, completion: {})
  176. }
  177. }
  178. override func tableView(_: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  179. let section = indexPath.section
  180. let row = indexPath.row
  181. if section == 0 {
  182. return cells[row]
  183. }
  184. if section == 1 {
  185. if row == 0 {
  186. return imapCellLoginName
  187. } else if row == 1 {
  188. return imapCellServer
  189. } else if row == 2 {
  190. return imapCellPort
  191. // } else if row == 3 {
  192. // return imapCellSecurity
  193. } else if row == 3 {
  194. // FIXME: support iPad
  195. imapCellSecurity.textLabel?.text = "Security:"
  196. imapCellSecurity.selectionStyle = .none
  197. imapCellSecurity.detailTextLabel?.text = model.imapSecurity.rawValue
  198. return imapCellSecurity
  199. }
  200. }
  201. if section == 2 {
  202. if row == 0 {
  203. return smtpCellLoginName
  204. } else if row == 1 {
  205. return smtpCellPassword
  206. } else if row == 2 {
  207. return smtpCellServer
  208. } else if row == 3 {
  209. return smtpCellPort
  210. } else if row == 4 {
  211. // FIXME: support iPad
  212. smtpCellSecurity.selectionStyle = .none
  213. smtpCellSecurity.textLabel?.text = "Security:"
  214. smtpCellSecurity.detailTextLabel?.text = model.smtpSecurity.rawValue
  215. return smtpCellSecurity
  216. }
  217. }
  218. return UITableViewCell()
  219. }
  220. override func didReceiveMemoryWarning() {
  221. super.didReceiveMemoryWarning()
  222. // Dispose of any resources that can be recreated.
  223. }
  224. }
  225. extension CredentialsController: UITextFieldDelegate {
  226. func textFieldShouldReturn(_ textField: UITextField) -> Bool {
  227. if textField == emailCell.textField {
  228. if let emailText = emailCell.textField.text {
  229. // only jump to next field if valid email
  230. if Utils.isValid(emailText) {
  231. passwordCell.textField.becomeFirstResponder()
  232. }
  233. }
  234. }
  235. if textField == passwordCell.textField {
  236. if readyForLogin() {
  237. didPressSaveAccountButton()
  238. }
  239. }
  240. return true
  241. }
  242. }
  243. extension CredentialsController {
  244. @objc func emailTextChanged() {
  245. let emailText = emailCell.textField.text ?? ""
  246. model.email = emailText
  247. }
  248. @objc func passwordTextChanged() {
  249. let passwordText = passwordCell.textField.text ?? ""
  250. model.password = passwordText
  251. }
  252. @objc func imapLoginNameChanged() {
  253. model.imapLoginName = imapCellLoginName.textField.text
  254. }
  255. @objc func imapServerChanged() {
  256. model.imapServer = imapCellServer.textField.text
  257. }
  258. @objc func imapPortChanged() {
  259. model.imapPort = imapCellPort.textField.text
  260. }
  261. @objc func smtpLoginNamedChanged() {
  262. model.smtpLoginName = smtpCellLoginName.textField.text
  263. }
  264. @objc func smtpPasswordChanged() {
  265. model.smtpPassword = smtpCellPassword.textField.text
  266. }
  267. @objc func smtpServerChanged() {
  268. model.smtpServer = smtpCellServer.textField.text
  269. }
  270. @objc func smtpPortChanged() {
  271. model.smtpPort = smtpCellPort.textField.text
  272. }
  273. }