AccountSetupController.swift 22 KB

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