CredentialsController.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 TextFieldCell:UITableViewCell {
  30. let textField = UITextField()
  31. init(description: String, placeholder: String) {
  32. super.init(style: .value1, reuseIdentifier: nil)
  33. textLabel?.text = "\(description):"
  34. contentView.addSubview(textField)
  35. textField.translatesAutoresizingMaskIntoConstraints = false
  36. // see: https://stackoverflow.com/a/35903650
  37. // this makes the textField respect the trailing margin of
  38. // the table view cell
  39. let margins = contentView.layoutMarginsGuide
  40. let trailing = margins.trailingAnchor
  41. textField.trailingAnchor.constraint(equalTo: trailing).isActive = true
  42. textField.centerYAnchor.constraint(equalTo: contentView.centerYAnchor).isActive = true
  43. textField.textAlignment = .right
  44. textField.placeholder = placeholder
  45. selectionStyle = .none
  46. textField.enablesReturnKeyAutomatically = true
  47. }
  48. override func setSelected(_ selected: Bool, animated: Bool) {
  49. if selected {
  50. textField.becomeFirstResponder()
  51. }
  52. }
  53. required init?(coder aDecoder: NSCoder) {
  54. fatalError("init(coder:) has not been implemented")
  55. }
  56. static func makeEmailCell() -> TextFieldCell {
  57. let emailCell = TextFieldCell(description: "Email", placeholder: "you@example.com")
  58. emailCell.textField.keyboardType = .emailAddress
  59. // switch off quicktype
  60. emailCell.textField.autocorrectionType = .no
  61. emailCell.textField.autocapitalizationType = .none
  62. return emailCell
  63. }
  64. static func makePasswordCell() -> TextFieldCell {
  65. let passwordCell = TextFieldCell(description: "Password", placeholder: "your IMAP password")
  66. passwordCell.textField.textContentType = UITextContentType.password
  67. passwordCell.textField.isSecureTextEntry = true
  68. return passwordCell
  69. }
  70. static func makeNameCell() -> TextFieldCell {
  71. let nameCell = TextFieldCell(description: "Name", placeholder: "new contacts nickname")
  72. nameCell.textField.autocapitalizationType = .words
  73. nameCell.textField.autocorrectionType = .no
  74. // .namePhonePad doesn't support autocapitalization
  75. // see: https://stackoverflow.com/a/36365399
  76. // therefore we use .default to capitalize the first character of the name
  77. nameCell.textField.keyboardType = .default
  78. return nameCell
  79. }
  80. static func makeConfigCell(label: String, placeholder: String) -> TextFieldCell {
  81. let nameCell = TextFieldCell(description: label, placeholder: placeholder)
  82. nameCell.textField.autocapitalizationType = .words
  83. nameCell.textField.autocorrectionType = .no
  84. // .namePhonePad doesn't support autocapitalization
  85. // see: https://stackoverflow.com/a/36365399
  86. // therefore we use .default to capitalize the first character of the name
  87. nameCell.textField.keyboardType = .default
  88. return nameCell
  89. }
  90. }
  91. class CredentialsController: UITableViewController {
  92. let emailCell = TextFieldCell.makeEmailCell()
  93. let passwordCell = TextFieldCell.makePasswordCell()
  94. let imapCellLoginName = TextFieldCell.makeConfigCell(label: "IMAP Login Name", placeholder: "Automatic")
  95. let imapCellServer = TextFieldCell.makeConfigCell(label: "IMAP Server", placeholder: "Automatic")
  96. let imapCellPort = TextFieldCell.makeConfigCell(label: "IMAP Port", placeholder: "Automatic")
  97. let imapCellSecurity = UITableViewCell(style: UITableViewCell.CellStyle.value1, reuseIdentifier: nil)
  98. let smtpCellLoginName = TextFieldCell.makeConfigCell(label: "SMTP Login Name", placeholder: "Automatic")
  99. let smtpCellPassword = TextFieldCell.makeConfigCell(label: "SMTP Password", placeholder: "As above")
  100. let smtpCellServer = TextFieldCell.makeConfigCell(label: "SMTP Server", placeholder: "Automatic")
  101. let smtpCellPort = TextFieldCell.makeConfigCell(label: "SMTP Port", placeholder: "Automatic")
  102. let smtpCellSecurity = UITableViewCell(style: UITableViewCell.CellStyle.value1, reuseIdentifier: nil)
  103. var doneButton:UIBarButtonItem?
  104. var advancedButton:UIBarButtonItem?
  105. let progressBar = UIProgressView(progressViewStyle: .default)
  106. func readyForLogin() -> Bool {
  107. return Utils.isValid(model.email) && !model.password.isEmpty
  108. }
  109. var advancedMode = false {
  110. didSet {
  111. if advancedMode {
  112. advancedButton?.title = "Standard"
  113. } else {
  114. advancedButton?.title = "Advanced"
  115. }
  116. tableView.reloadData()
  117. }
  118. }
  119. var model:CredentialsModel = ("", "", nil, nil, nil, SecurityMode.automatic, nil, nil, nil, nil, SecurityMode.automatic) {
  120. didSet {
  121. if readyForLogin() {
  122. doneButton?.isEnabled = true
  123. } else {
  124. doneButton?.isEnabled = false
  125. }
  126. smtpCellSecurity.detailTextLabel?.text = model.smtpSecurity.rawValue
  127. imapCellSecurity.detailTextLabel?.text = model.imapSecurity.rawValue
  128. print(model)
  129. }
  130. }
  131. let cells:[UITableViewCell]
  132. init(isCancellable:Bool = false) {
  133. cells = [emailCell, passwordCell]
  134. super.init(style: .grouped)
  135. doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(didPressSaveAccountButton))
  136. doneButton?.isEnabled = false
  137. advancedButton = UIBarButtonItem(title: "Advanced", style: .done, target: self, action: #selector(didPressAdvancedButton))
  138. let cancelButton = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(didPressCancelButton))
  139. if isCancellable {
  140. navigationItem.rightBarButtonItems = [doneButton!, cancelButton]
  141. } else {
  142. navigationItem.rightBarButtonItem = doneButton
  143. }
  144. navigationItem.leftBarButtonItem = advancedButton
  145. // FIXME: refactor: do not use target/action here for text field changes
  146. // but text field delegate
  147. emailCell.textField.addTarget(self, action: #selector(emailTextChanged), for: UIControl.Event.editingChanged)
  148. passwordCell.textField.addTarget(self, action: #selector(passwordTextChanged), for: UIControl.Event.editingChanged)
  149. imapCellLoginName.textField.addTarget(self, action: #selector(imapLoginNameChanged), for: .editingChanged)
  150. imapCellServer.textField.addTarget(self, action: #selector(imapServerChanged), for: .editingChanged)
  151. imapCellPort.textField.addTarget(self, action: #selector(imapPortChanged), for: .editingChanged)
  152. smtpCellLoginName.textField.addTarget(self, action: #selector(smtpLoginNamedChanged), for: .editingChanged)
  153. smtpCellPassword.textField.addTarget(self, action: #selector(smtpPasswordChanged), for: .editingChanged)
  154. smtpCellServer.textField.addTarget(self, action: #selector(smtpServerChanged), for: .editingChanged)
  155. smtpCellPort.textField.addTarget(self, action: #selector(smtpPortChanged), for: .editingChanged)
  156. emailCell.textField.textContentType = UITextContentType.emailAddress
  157. emailCell.textField.delegate = self
  158. passwordCell.textField.delegate = self
  159. emailCell.textField.returnKeyType = .next
  160. passwordCell.textField.returnKeyType = .done
  161. }
  162. override func viewDidAppear(_ animated: Bool) {
  163. emailCell.textField.becomeFirstResponder()
  164. }
  165. @objc func didPressCancelButton() {
  166. dismiss(animated: true, completion: nil)
  167. }
  168. @objc func didPressSaveAccountButton() {
  169. let m = model
  170. let a = advancedMode
  171. dismiss(animated: true) {
  172. initCore(withCredentials: true, advancedMode: a, model: m)
  173. }
  174. }
  175. @objc func didPressAdvancedButton() {
  176. advancedMode = !advancedMode
  177. }
  178. required init?(coder aDecoder: NSCoder) {
  179. fatalError("init(coder:) has not been implemented")
  180. }
  181. override func viewDidLoad() {
  182. super.viewDidLoad()
  183. title = "Account"
  184. navigationController?.navigationBar.prefersLargeTitles = true
  185. }
  186. override func numberOfSections(in tableView: UITableView) -> Int {
  187. return advancedMode ? 3 : 1
  188. }
  189. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  190. if section == 0 {
  191. return cells.count
  192. }
  193. if section == 1 {
  194. return 4
  195. }
  196. if section == 2 {
  197. return 5
  198. }
  199. return 0 // should never happen
  200. }
  201. override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  202. if section == 1 {
  203. return "IMAP"
  204. }
  205. if section == 2 {
  206. return "SMTP"
  207. }
  208. return nil
  209. }
  210. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  211. let isIMAP = indexPath.section == 1
  212. let isSMTP = indexPath.section == 2
  213. if (isIMAP && (indexPath.row == 3)) || (isSMTP && (indexPath.row == 4)) {
  214. let actionSheet = UIAlertController(title: "Security", message: nil, preferredStyle: .actionSheet)
  215. for securityMode in SecurityMode.allCases {
  216. actionSheet.addAction(UIAlertAction(title: securityMode.rawValue, style: .default, handler: {
  217. [unowned self]
  218. _ in
  219. if isIMAP {
  220. self.model.imapSecurity = securityMode
  221. }
  222. if isSMTP {
  223. self.model.smtpSecurity = securityMode
  224. }
  225. }))
  226. }
  227. actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: {a in}))
  228. if let popoverController = actionSheet.popoverPresentationController {
  229. if let cell = tableView.cellForRow(at: indexPath) {
  230. popoverController.sourceView = cell.detailTextLabel
  231. }
  232. }
  233. self.present(actionSheet, animated: true, completion: {})
  234. }
  235. }
  236. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  237. let section = indexPath.section
  238. let row = indexPath.row
  239. if section == 0 {
  240. return cells[row]
  241. }
  242. if section == 1 {
  243. if row == 0 {
  244. return imapCellLoginName
  245. } else if row == 1 {
  246. return imapCellServer
  247. } else if row == 2 {
  248. return imapCellPort
  249. // } else if row == 3 {
  250. // return imapCellSecurity
  251. } else if row == 3 {
  252. // FIXME: support iPad
  253. imapCellSecurity.textLabel?.text = "Security:"
  254. imapCellSecurity.selectionStyle = .none
  255. imapCellSecurity.detailTextLabel?.text = model.imapSecurity.rawValue
  256. return imapCellSecurity
  257. }
  258. }
  259. if section == 2 {
  260. if row == 0 {
  261. return smtpCellLoginName
  262. } else if row == 1 {
  263. return smtpCellPassword
  264. } else if row == 2 {
  265. return smtpCellServer
  266. } else if row == 3 {
  267. return smtpCellPort
  268. } else if row == 4 {
  269. // FIXME: support iPad
  270. smtpCellSecurity.selectionStyle = .none
  271. smtpCellSecurity.textLabel?.text = "Security:"
  272. smtpCellSecurity.detailTextLabel?.text = model.smtpSecurity.rawValue
  273. return smtpCellSecurity
  274. }
  275. }
  276. return UITableViewCell()
  277. }
  278. override func didReceiveMemoryWarning() {
  279. super.didReceiveMemoryWarning()
  280. // Dispose of any resources that can be recreated.
  281. }
  282. }
  283. extension CredentialsController: UITextFieldDelegate {
  284. func textFieldShouldReturn(_ textField: UITextField) -> Bool {
  285. if textField == emailCell.textField {
  286. if let emailText = emailCell.textField.text {
  287. // only jump to next field if valid email
  288. if Utils.isValid(emailText) {
  289. passwordCell.textField.becomeFirstResponder()
  290. }
  291. }
  292. }
  293. if textField == passwordCell.textField {
  294. if readyForLogin() {
  295. self.didPressSaveAccountButton()
  296. }
  297. }
  298. return true
  299. }
  300. }
  301. extension CredentialsController {
  302. @objc func emailTextChanged() {
  303. let emailText = emailCell.textField.text ?? ""
  304. model.email = emailText
  305. }
  306. @objc func passwordTextChanged() {
  307. let passwordText = passwordCell.textField.text ?? ""
  308. model.password = passwordText
  309. }
  310. @objc func imapLoginNameChanged() {
  311. model.imapLoginName = imapCellLoginName.textField.text
  312. }
  313. @objc func imapServerChanged() {
  314. model.imapServer = imapCellServer.textField.text
  315. }
  316. @objc func imapPortChanged() {
  317. model.imapPort = imapCellPort.textField.text
  318. }
  319. @objc func smtpLoginNamedChanged() {
  320. model.smtpLoginName = smtpCellLoginName.textField.text
  321. }
  322. @objc func smtpPasswordChanged() {
  323. model.smtpPassword = smtpCellPassword.textField.text
  324. }
  325. @objc func smtpServerChanged() {
  326. model.smtpServer = smtpCellServer.textField.text
  327. }
  328. @objc func smtpPortChanged() {
  329. model.smtpPort = smtpCellPort.textField.text
  330. }
  331. }