AccountSetupController.swift 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. //
  2. // AccountSetupController.swift
  3. // deltachat-ios
  4. //
  5. // Created by Bastian van de Wetering on 02.04.19.
  6. // Copyright © 2019 Jonas Reinsch. All rights reserved.
  7. //
  8. import SafariServices
  9. import UIKit
  10. import UICircularProgressRing
  11. class AccountSetupController: UITableViewController {
  12. weak var coordinator: AccountSetupCoordinator?
  13. private var backupProgressObserver: Any?
  14. private var configureProgressObserver: Any?
  15. private var oauth2Observer: Any?
  16. lazy var configurationProgress: UICircularProgressRing = {
  17. let progress = UICircularProgressRing()
  18. progress.style = UICircularRingStyle.inside
  19. //progress.backgroundColor = UIColor.white.withAlphaComponent(0)
  20. progress.maxValue = 100
  21. progress.outerRingColor = UIColor.init(white: 1, alpha: 0)
  22. progress.innerRingColor = DCColors.primary
  23. progress.innerRingWidth = 2
  24. progress.startAngle = 270
  25. progress.fontColor = UIColor.lightGray
  26. progress.font = UIFont.systemFont(ofSize: 12)
  27. return progress
  28. }()
  29. lazy var loginProgressHud: UIAlertController = {
  30. let alert = UIAlertController(title: "Configuring Account", message: "\n\n\n", preferredStyle: .alert)
  31. // temp workaround: add 3 newlines to let alertbox grow to fit progress indicator
  32. let progressView = configurationProgress
  33. progressView.translatesAutoresizingMaskIntoConstraints = false
  34. alert.view.addSubview(progressView)
  35. progressView.centerXAnchor.constraint(equalTo: alert.view.centerXAnchor).isActive = true
  36. progressView.centerYAnchor.constraint(equalTo: alert.view.centerYAnchor, constant: 0).isActive = true
  37. progressView.heightAnchor.constraint(equalToConstant: 70).isActive = true
  38. progressView.widthAnchor.constraint(equalToConstant: 70).isActive = true
  39. alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
  40. return alert
  41. }()
  42. /*
  43. private lazy var hudHandler: HudHandler = {
  44. let hudHandler = HudHandler(parentView: self)
  45. return hudHandler
  46. }()
  47. */
  48. private lazy var emailCell: TextFieldCell = {
  49. let cell = TextFieldCell.makeEmailCell(delegate: self)
  50. cell.textField.tag = 0
  51. cell.textField.accessibilityIdentifier = "emailTextField" // will be used to eventually show oAuth-Dialogue when pressing return key
  52. cell.setText(text: MRConfig.addr ?? nil)
  53. cell.textField.delegate = self
  54. return cell
  55. }()
  56. private lazy var passwordCell: TextFieldCell = {
  57. let cell = TextFieldCell.makePasswordCell(delegate: self)
  58. cell.textField.tag = 1
  59. cell.accessibilityIdentifier = "passwordCell" // will be used to eventually show oAuth-Dialogue when selecting
  60. cell.setText(text: MRConfig.mailPw ?? nil)
  61. return cell
  62. }()
  63. private lazy var restoreCell: ActionCell = {
  64. let cell = ActionCell(frame: .zero)
  65. cell.actionTitle = "Restore from backup"
  66. cell.accessibilityIdentifier = "restoreCell"
  67. return cell
  68. }()
  69. lazy var imapServerCell: TextFieldCell = {
  70. let cell = TextFieldCell(description: "IMAP Server", placeholder: MRConfig.mailServer ?? MRConfig.configuredMailServer, delegate: self)
  71. cell.accessibilityIdentifier = "IMAPServerCell"
  72. cell.textField.tag = 2
  73. return cell
  74. }()
  75. lazy var imapUserCell: TextFieldCell = {
  76. let cell = TextFieldCell(description: "IMAP User", placeholder: MRConfig.mailUser ?? MRConfig.configuredMailUser, delegate: self)
  77. cell.accessibilityIdentifier = "IMAPUserCell"
  78. cell.textField.tag = 3
  79. return cell
  80. }()
  81. lazy var imapPortCell: UITableViewCell = {
  82. let cell = UITableViewCell(style: .value1, reuseIdentifier: nil)
  83. cell.textLabel?.text = "IMAP Port"
  84. cell.accessoryType = .disclosureIndicator
  85. cell.detailTextLabel?.text = MRConfig.mailPort ?? MRConfig.configuredMailPort
  86. cell.accessibilityIdentifier = "IMAPPortCell"
  87. cell.selectionStyle = .none
  88. return cell
  89. }()
  90. lazy var imapSecurityCell: UITableViewCell = {
  91. let text = "\(MRConfig.getImapSecurity())"
  92. let cell = UITableViewCell(style: .value1, reuseIdentifier: nil)
  93. cell.textLabel?.text = "IMAP Security"
  94. // let cell = TextFieldCell(description: "IMAP Security", placeholder: text, delegate: self)
  95. cell.accessibilityIdentifier = "IMAPSecurityCell"
  96. cell.accessoryType = .disclosureIndicator
  97. cell.detailTextLabel?.text = "\(MRConfig.getImapSecurity())"
  98. cell.selectionStyle = .none
  99. return cell
  100. }()
  101. lazy var smtpServerCell: TextFieldCell = {
  102. let cell = TextFieldCell(description: "SMTP Server", placeholder: MRConfig.sendServer ?? MRConfig.configuredSendServer, delegate: self)
  103. cell.accessibilityIdentifier = "SMTPServerCell"
  104. cell.textField.tag = 4
  105. return cell
  106. }()
  107. lazy var smtpUserCell: TextFieldCell = {
  108. let cell = TextFieldCell(description: "SMTP User", placeholder: MRConfig.sendUser ?? MRConfig.configuredSendUser, delegate: self)
  109. cell.accessibilityIdentifier = "SMTPUserCell"
  110. cell.textField.tag = 5
  111. return cell
  112. }()
  113. lazy var smtpPortCell: UITableViewCell = {
  114. let cell = UITableViewCell(style: .value1, reuseIdentifier: nil)
  115. cell.textLabel?.text = "SMTP Port"
  116. cell.accessoryType = .disclosureIndicator
  117. cell.detailTextLabel?.text = MRConfig.sendPort ?? MRConfig.configuredSendPort
  118. cell.accessibilityIdentifier = "SMTPPortCell"
  119. cell.selectionStyle = .none
  120. return cell
  121. }()
  122. lazy var smtpPasswordCell: TextFieldCell = {
  123. let cell = TextFieldCell(description: "SMTP Password", placeholder: "*************", delegate: self)
  124. cell.accessibilityIdentifier = "SMTPPasswordCell"
  125. cell.textField.tag = 6
  126. return cell
  127. }()
  128. lazy var smtpSecurityCell: UITableViewCell = {
  129. let security = "\(MRConfig.getSmtpSecurity())"
  130. let cell = UITableViewCell(style: .value1, reuseIdentifier: nil)
  131. cell.textLabel?.text = "SMTP Security"
  132. cell.detailTextLabel?.text = security
  133. cell.accessibilityIdentifier = "SMTPSecurityCell"
  134. cell.accessoryType = .disclosureIndicator
  135. cell.selectionStyle = .none
  136. return cell
  137. }()
  138. // this loginButton can be enabled and disabled
  139. let loginButton: UIBarButtonItem = UIBarButtonItem(title: "Login", style: .done, target: self, action: #selector(loginButtonPressed))
  140. private lazy var basicSectionCells: [UITableViewCell] = [emailCell, passwordCell]
  141. private lazy var restoreCells: [UITableViewCell] = [restoreCell]
  142. private lazy var advancedSectionCells: [UITableViewCell] = [
  143. imapServerCell,
  144. imapUserCell,
  145. imapPortCell,
  146. imapSecurityCell,
  147. smtpServerCell,
  148. smtpUserCell,
  149. smtpPortCell,
  150. smtpPasswordCell,
  151. smtpSecurityCell
  152. ]
  153. private var advancedSectionShowing: Bool = false
  154. init() {
  155. super.init(style: .grouped)
  156. hidesBottomBarWhenPushed = true
  157. }
  158. required init?(coder _: NSCoder) {
  159. fatalError("init(coder:) has not been implemented")
  160. }
  161. override func viewDidLoad() {
  162. super.viewDidLoad()
  163. title = "Login to your server"
  164. // navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Close", style: .plain, target: self, action: #selector(closeButtonPressed))
  165. navigationItem.rightBarButtonItem = loginButton
  166. }
  167. override func viewWillAppear(_ animated: Bool) {
  168. super.viewWillAppear(animated)
  169. // needs to be changed if returning from portSettingsController
  170. smtpPortCell.detailTextLabel?.text = MRConfig.sendPort ?? MRConfig.configuredSendPort
  171. imapPortCell.detailTextLabel?.text = MRConfig.mailPort ?? MRConfig.configuredMailPort
  172. smtpSecurityCell.detailTextLabel?.text = SecurityConverter.convertHexToString(type: .SMTPSecurity, hex: MRConfig.getSmtpSecurity())
  173. imapSecurityCell.detailTextLabel?.text = SecurityConverter.convertHexToString(type: .IMAPSecurity, hex: MRConfig.getImapSecurity())
  174. }
  175. override func viewDidAppear(_ animated: Bool) {
  176. super.viewDidAppear(animated)
  177. addProgressHudEventListener()
  178. // loginButton.isEnabled = false
  179. }
  180. override func viewWillDisappear(_ animated: Bool) {
  181. resignFirstResponderOnAllCells()
  182. }
  183. override func viewDidDisappear(_: Bool) {
  184. let nc = NotificationCenter.default
  185. if let backupProgressObserver = self.backupProgressObserver {
  186. nc.removeObserver(backupProgressObserver)
  187. }
  188. if let configureProgressObserver = self.configureProgressObserver {
  189. nc.removeObserver(configureProgressObserver)
  190. }
  191. if let oauth2Observer = self.oauth2Observer {
  192. nc.removeObserver(oauth2Observer)
  193. }
  194. }
  195. // MARK: - Table view data source
  196. override func numberOfSections(in _: UITableView) -> Int {
  197. // #warning Incomplete implementation, return the number of sections
  198. return 3
  199. }
  200. override func tableView(_: UITableView, numberOfRowsInSection section: Int) -> Int {
  201. // #warning Incomplete implementation, return the number of rows
  202. if section == 0 {
  203. return basicSectionCells.count
  204. } else if section == 1 {
  205. return restoreCells.count
  206. } else {
  207. return advancedSectionShowing ? advancedSectionCells.count : 0
  208. }
  209. }
  210. override func tableView(_: UITableView, titleForHeaderInSection section: Int) -> String? {
  211. if section == 2 {
  212. return "Advanced"
  213. } else {
  214. return nil
  215. }
  216. }
  217. override func tableView(_: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  218. if section == 2 {
  219. // Advanced Header
  220. let advancedView = AdvancedSectionHeader()
  221. advancedView.handleTap = toggleAdvancedSection
  222. // set tapHandler
  223. return advancedView
  224. } else {
  225. return nil
  226. }
  227. }
  228. override func tableView(_: UITableView, heightForHeaderInSection _: Int) -> CGFloat {
  229. return 36.0
  230. }
  231. override func tableView(_: UITableView, titleForFooterInSection section: Int) -> String? {
  232. if section == 0 {
  233. return "There are no Delta Chat servers, your data stays on your device!"
  234. } else if section == 2 {
  235. return "For known email providers additional settings are setup automatically. Sometimes IMAP needs to be enabled in the web frontend. Consult your email provider or friends for help"
  236. } else {
  237. return nil
  238. }
  239. }
  240. override func tableView(_: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  241. let section = indexPath.section
  242. let row = indexPath.row
  243. if section == 0 {
  244. // basicSection
  245. return basicSectionCells[row]
  246. } else if section == 1 {
  247. return restoreCells[row]
  248. } else {
  249. // advancedSection
  250. return advancedSectionCells[row]
  251. }
  252. }
  253. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  254. guard let tappedCell = tableView.cellForRow(at: indexPath) else { return }
  255. // handle tap on password -> show oAuthDialogue
  256. if let textFieldCell = tappedCell as? TextFieldCell {
  257. if textFieldCell.accessibilityIdentifier == "passwordCell" {
  258. if let emailAdress = textFieldCell.getText() {
  259. _ = showOAuthAlertIfNeeded(emailAddress: emailAdress, handleCancel: nil)
  260. }
  261. }
  262. }
  263. if tappedCell.accessibilityIdentifier == "restoreCell" {
  264. restoreBackup()
  265. } else if tappedCell.accessibilityIdentifier == "IMAPPortCell" {
  266. coordinator?.showImapPortOptions()
  267. } else if tappedCell.accessibilityIdentifier == "SMTPPortCell" {
  268. coordinator?.showSmtpPortsOptions()
  269. } else if tappedCell.accessibilityIdentifier == "IMAPSecurityCell" {
  270. coordinator?.showImapSecurityOptions()
  271. } else if tappedCell.accessibilityIdentifier == "SMTPSecurityCell" {
  272. coordinator?.showSmptpSecurityOptions()
  273. }
  274. }
  275. private func toggleAdvancedSection(button: UILabel) {
  276. let willShow = !advancedSectionShowing
  277. // extract indexPaths from advancedCells
  278. let advancedIndexPaths: [IndexPath] = advancedSectionCells.indices.map { IndexPath(row: $0, section: 2) }
  279. // advancedSectionCells.indices.map({indexPaths.append(IndexPath(row: $0, section: 1))}
  280. // set flag before delete/insert operation, because cellForRowAt will be triggered and uses this flag
  281. advancedSectionShowing = willShow
  282. button.text = willShow ? "Hide" : "Show"
  283. if willShow {
  284. tableView.insertRows(at: advancedIndexPaths, with: .fade)
  285. } else {
  286. tableView.deleteRows(at: advancedIndexPaths, with: .fade)
  287. }
  288. }
  289. @objc private func loginButtonPressed() {
  290. guard let emailAddress = emailCell.getText() else {
  291. return // handle case when either email or pw fields are empty
  292. }
  293. let oAuthStarted = showOAuthAlertIfNeeded(emailAddress: emailAddress, handleCancel: loginButtonPressed) // if canceled we will run this method again but this time oAuthStarted will be false
  294. if oAuthStarted {
  295. // the loginFlow will be handled by oAuth2
  296. return
  297. }
  298. let password = passwordCell.getText() ?? "" // empty passwords are ok -> for oauth there is no password needed
  299. login(emailAddress: emailAddress, password: password)
  300. }
  301. private func login(emailAddress: String, password: String, skipAdvanceSetup: Bool = false) {
  302. MRConfig.addr = emailAddress
  303. MRConfig.mailPw = password
  304. if !skipAdvanceSetup {
  305. evaluluateAdvancedSetup() // this will set MRConfig related to advanced fields
  306. }
  307. dc_configure(mailboxPointer)
  308. present(loginProgressHud, animated: true, completion: nil)
  309. }
  310. @objc func closeButtonPressed() {
  311. dismiss(animated: true, completion: nil)
  312. }
  313. // returns true if needed
  314. private func showOAuthAlertIfNeeded(emailAddress: String, handleCancel: (() -> Void)?) -> Bool {
  315. if MRConfig.getAuthFlags() == 4 {
  316. // user has previously denied oAuth2-setup
  317. return false
  318. }
  319. guard let oAuth2UrlPointer = dc_get_oauth2_url(mailboxPointer, emailAddress, "chat.delta:/auth") else {
  320. return false
  321. }
  322. let oAuth2Url = String(cString: oAuth2UrlPointer)
  323. if let url = URL(string: oAuth2Url) {
  324. let title = "Continue with simplified setup"
  325. // swiftlint:disable all
  326. let message = "The entered e-mail address supports a simplified setup (oAuth2).\n\nIn the next step, please allow Delta Chat to act as your Chat with E-Mail app.\n\nThere are no Delta Chat servers, your data stays on your device."
  327. let oAuthAlertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
  328. let confirm = UIAlertAction(title: "Confirm", style: .default, handler: {
  329. [unowned self] _ in
  330. let nc = NotificationCenter.default
  331. self.oauth2Observer = nc.addObserver(self, selector: #selector(self.oauthLoginApproved), name: NSNotification.Name("oauthLoginApproved"), object: nil)
  332. self.launchOAuthBrowserWindow(url: url)
  333. })
  334. let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: {
  335. _ in
  336. MRConfig.setAuthFlags(flags: Int(DC_LP_AUTH_NORMAL))
  337. handleCancel?()
  338. })
  339. oAuthAlertController.addAction(confirm)
  340. oAuthAlertController.addAction(cancel)
  341. present(oAuthAlertController, animated: true, completion: nil)
  342. return true
  343. } else {
  344. return false
  345. }
  346. }
  347. @objc func oauthLoginApproved(notification: Notification) {
  348. guard let userInfo = notification.userInfo, let token = userInfo["token"] as? String, let emailAddress = emailCell.getText() else {
  349. return
  350. }
  351. passwordCell.setText(text: token)
  352. MRConfig.setAuthFlags(flags: Int(DC_LP_AUTH_OAUTH2))
  353. login(emailAddress: emailAddress, password: token, skipAdvanceSetup: true)
  354. }
  355. private func launchOAuthBrowserWindow(url: URL) {
  356. UIApplication.shared.open(url) // this opens safari as seperate app
  357. }
  358. private func addProgressHudEventListener() {
  359. let nc = NotificationCenter.default
  360. backupProgressObserver = nc.addObserver(
  361. forName: dcNotificationBackupProgress,
  362. object: nil,
  363. queue: nil
  364. ) {
  365. notification in
  366. if let ui = notification.userInfo {
  367. if ui["error"] as! Bool {
  368. self.updateProgressHud(error: ui["errorMessage"] as? String)
  369. // self.hudHandler.setHudError(ui["errorMessage"] as? String)
  370. } else if ui["done"] as! Bool {
  371. self.updateProgressHudSuccess(callback: self.handleLoginSuccess)
  372. // self.hudHandler.setHudDone(callback: self.handleLoginSuccess)
  373. } else {
  374. self.updateProgressHudValue(value: ui["progress"] as! Int)
  375. // self.hudHandler.setHudProgress(ui["progress"] as! Int)
  376. }
  377. }
  378. }
  379. configureProgressObserver = nc.addObserver(
  380. forName: dcNotificationConfigureProgress,
  381. object: nil,
  382. queue: nil
  383. ) {
  384. notification in
  385. if let ui = notification.userInfo {
  386. if ui["error"] as! Bool {
  387. self.updateProgressHud(error: ui["errorMessage"] as? String)
  388. // self.hudHandler.setHudError(ui["errorMessage"] as? String)
  389. } else if ui["done"] as! Bool {
  390. self.updateProgressHudSuccess(callback: self.handleLoginSuccess)
  391. // self.hudHandler.setHudDone(callback: self.handleLoginSuccess)
  392. } else {
  393. self.updateProgressHudValue(value: ui["progress"] as! Int)
  394. // self.hudHandler.setHudProgress(ui["progress"] as! Int)
  395. }
  396. }
  397. }
  398. }
  399. private func evaluluateAdvancedSetup() {
  400. for cell in advancedSectionCells {
  401. if let textFieldCell = cell as? TextFieldCell {
  402. switch cell.accessibilityIdentifier {
  403. case "IMAPServerCell":
  404. MRConfig.mailServer = textFieldCell.getText() ?? nil
  405. case "IMAPUserCell":
  406. MRConfig.mailUser = textFieldCell.getText() ?? nil
  407. case "IMAPPortCell":
  408. MRConfig.mailPort = textFieldCell.getText() ?? nil
  409. case "IMAPSecurityCell":
  410. let flag = 0
  411. MRConfig.setImapSecurity(imapFlags: flag)
  412. case "SMTPServerCell":
  413. MRConfig.sendServer = textFieldCell.getText() ?? nil
  414. case "SMTPSUserCell":
  415. MRConfig.sendUser = textFieldCell.getText() ?? nil
  416. case "SMTPPortCell":
  417. MRConfig.sendPort = textFieldCell.getText() ?? nil
  418. case "SMTPPasswordCell":
  419. MRConfig.sendPw = textFieldCell.getText() ?? nil
  420. case "SMTPSecurityCell":
  421. let flag = 0
  422. MRConfig.setSmtpSecurity(smptpFlags: flag)
  423. default:
  424. logger.info("unknown identifier", cell.accessibilityIdentifier ?? "")
  425. }
  426. }
  427. }
  428. }
  429. private func restoreBackup() {
  430. logger.info("restoring backup")
  431. if MRConfig.configured {
  432. return
  433. }
  434. let documents = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
  435. if !documents.isEmpty {
  436. logger.info("looking for backup in: \(documents[0])")
  437. if let file = dc_imex_has_backup(mailboxPointer, documents[0]) {
  438. logger.info("restoring backup: \(String(cString: file))")
  439. // hudHandler.showBackupHud("Restoring Backup")
  440. dc_imex(mailboxPointer, DC_IMEX_IMPORT_BACKUP, file, nil)
  441. return
  442. }
  443. let alert = UIAlertController(title: "Can not restore", message: "No Backup found", preferredStyle: .alert)
  444. alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: { _ in
  445. }))
  446. present(alert, animated: true, completion: nil)
  447. return
  448. }
  449. logger.error("no documents directory found")
  450. }
  451. private func handleLoginSuccess() {
  452. // used when login hud successfully went trough
  453. dismiss(animated: true, completion: nil)
  454. }
  455. private func resignFirstResponderOnAllCells() {
  456. basicSectionCells.map({
  457. resignCell(cell: $0)
  458. })
  459. advancedSectionCells.map({
  460. resignCell(cell: $0)
  461. }
  462. )
  463. }
  464. func resignCell(cell: UITableViewCell) {
  465. if let c = cell as? TextFieldCell {
  466. c.textField.resignFirstResponder()
  467. }
  468. }
  469. }
  470. extension AccountSetupController: UITextFieldDelegate {
  471. func textFieldShouldReturn(_ textField: UITextField) -> Bool {
  472. let currentTag = textField.tag
  473. if let nextField = tableView.viewWithTag(currentTag + 1) as? UITextField {
  474. if nextField.tag > 1, !advancedSectionShowing {
  475. // gets here when trying to activate a collapsed cell
  476. return false
  477. }
  478. nextField.becomeFirstResponder()
  479. }
  480. return false
  481. }
  482. func textFieldDidBeginEditing(_ textField: UITextField) {
  483. if textField.accessibilityIdentifier == "emailTextField" {
  484. // this will re-enable possible oAuth2-login
  485. MRConfig.setAuthFlags(flags: Int(DC_LP_AUTH_OAUTH2))
  486. }
  487. }
  488. func textFieldDidEndEditing(_ textField: UITextField) {
  489. if textField.accessibilityIdentifier == "emailTextField" {
  490. let _ = showOAuthAlertIfNeeded(emailAddress: textField.text ?? "", handleCancel: {
  491. self.passwordCell.textField.becomeFirstResponder()
  492. })
  493. }
  494. }
  495. }
  496. class AdvancedSectionHeader: UIView {
  497. var handleTap: ((UILabel) -> Void)?
  498. private var label: UILabel = {
  499. let label = UILabel()
  500. label.text = "ADVANCED"
  501. label.font = UIFont.systemFont(ofSize: 15)
  502. label.textColor = UIColor.darkGray
  503. return label
  504. }()
  505. /*
  506. why UILabel, why no UIButton? For unknown reasons UIButton's target function was not triggered when one of the textfields in the tableview was active -> used label as workaround
  507. */
  508. private lazy var toggleButton: UILabel = {
  509. let label = UILabel()
  510. label.text = "Show"
  511. label.font = UIFont.systemFont(ofSize: 15, weight: .medium)
  512. label.textColor = UIColor.systemBlue
  513. return label
  514. }()
  515. //
  516. // private var toggleButton:UIButton = {
  517. // let button = UIButton(type: .system)
  518. // button.setTitle("Show", for: .normal)
  519. // button.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside )
  520. // //button.target(forAction: #selector(buttonTapped(_:)), withSender: self)
  521. // return button
  522. // }()
  523. init() {
  524. super.init(frame: .zero) // will be constraint from tableViewDelegate
  525. setupSubviews()
  526. let tap = UITapGestureRecognizer(target: self, action: #selector(viewTapped)) // use this if the whole header is supposed to be clickable
  527. addGestureRecognizer(tap)
  528. }
  529. required init?(coder _: NSCoder) {
  530. fatalError("init(coder:) has not been implemented")
  531. }
  532. func setupSubviews() {
  533. addSubview(label)
  534. label.translatesAutoresizingMaskIntoConstraints = false
  535. label.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 15).isActive = true
  536. label.centerYAnchor.constraint(equalTo: centerYAnchor, constant: 0).isActive = true
  537. addSubview(toggleButton)
  538. toggleButton.translatesAutoresizingMaskIntoConstraints = false
  539. toggleButton.leadingAnchor.constraint(equalTo: trailingAnchor, constant: -60).isActive = true // since button will change title it should be left aligned
  540. toggleButton.centerYAnchor.constraint(equalTo: label.centerYAnchor, constant: 0).isActive = true
  541. }
  542. @objc func buttonTapped(_: UIButton) {
  543. // handleTap?(button)
  544. }
  545. @objc func viewTapped() {
  546. handleTap?(toggleButton)
  547. }
  548. }
  549. extension AccountSetupController {
  550. func updateProgressHud(error message: String?) {
  551. }
  552. func updateProgressHudSuccess(callback: (()->())?) {
  553. }
  554. func updateProgressHudValue(value: Int?) {
  555. if let value = value {
  556. print(value)
  557. configurationProgress.value = CGFloat(value / 10)
  558. } else {
  559. fatalError()
  560. }
  561. }
  562. }