AccountSetupController.swift 22 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. 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 viewWillDisappear(_ animated: Bool) {
  151. resignFirstResponderOnAllCells()
  152. }
  153. override func viewDidDisappear(_: Bool) {
  154. let nc = NotificationCenter.default
  155. if let backupProgressObserver = self.backupProgressObserver {
  156. nc.removeObserver(backupProgressObserver)
  157. }
  158. if let configureProgressObserver = self.configureProgressObserver {
  159. nc.removeObserver(configureProgressObserver)
  160. }
  161. if let oauth2Observer = self.oauth2Observer {
  162. nc.removeObserver(oauth2Observer)
  163. }
  164. }
  165. // MARK: - Table view data source
  166. override func numberOfSections(in _: UITableView) -> Int {
  167. // #warning Incomplete implementation, return the number of sections
  168. return 3
  169. }
  170. override func tableView(_: UITableView, numberOfRowsInSection section: Int) -> Int {
  171. // #warning Incomplete implementation, return the number of rows
  172. if section == 0 {
  173. return basicSectionCells.count
  174. } else if section == 1 {
  175. return restoreCells.count
  176. } else {
  177. return advancedSectionShowing ? advancedSectionCells.count : 0
  178. }
  179. }
  180. override func tableView(_: UITableView, titleForHeaderInSection section: Int) -> String? {
  181. if section == 2 {
  182. return "Advanced"
  183. } else {
  184. return nil
  185. }
  186. }
  187. override func tableView(_: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  188. if section == 2 {
  189. // Advanced Header
  190. let advancedView = AdvancedSectionHeader()
  191. advancedView.handleTap = toggleAdvancedSection
  192. // set tapHandler
  193. return advancedView
  194. } else {
  195. return nil
  196. }
  197. }
  198. override func tableView(_: UITableView, heightForHeaderInSection _: Int) -> CGFloat {
  199. return 36.0
  200. }
  201. override func tableView(_: UITableView, titleForFooterInSection section: Int) -> String? {
  202. if section == 0 {
  203. return "There are no Delta Chat servers, your data stays on your device!"
  204. } else if section == 2 {
  205. 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"
  206. } else {
  207. return nil
  208. }
  209. }
  210. override func tableView(_: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  211. let section = indexPath.section
  212. let row = indexPath.row
  213. if section == 0 {
  214. // basicSection
  215. return basicSectionCells[row]
  216. } else if section == 1 {
  217. return restoreCells[row]
  218. } else {
  219. // advancedSection
  220. return advancedSectionCells[row]
  221. }
  222. }
  223. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  224. guard let tappedCell = tableView.cellForRow(at: indexPath) else { return }
  225. // handle tap on password -> show oAuthDialogue
  226. if let textFieldCell = tappedCell as? TextFieldCell {
  227. if textFieldCell.accessibilityIdentifier == "passwordCell" {
  228. if let emailAdress = textFieldCell.getText() {
  229. _ = showOAuthAlertIfNeeded(emailAddress: emailAdress, handleCancel: nil)
  230. }
  231. }
  232. }
  233. if tappedCell.accessibilityIdentifier == "restoreCell" {
  234. restoreBackup()
  235. } else if tappedCell.accessibilityIdentifier == "IMAPPortCell" {
  236. coordinator?.showImapPortOptions()
  237. } else if tappedCell.accessibilityIdentifier == "SMTPPortCell" {
  238. coordinator?.showSmtpPortsOptions()
  239. } else if tappedCell.accessibilityIdentifier == "IMAPSecurityCell" {
  240. coordinator?.showImapSecurityOptions()
  241. } else if tappedCell.accessibilityIdentifier == "SMTPSecurityCell" {
  242. coordinator?.showSmptpSecurityOptions()
  243. }
  244. }
  245. private func toggleAdvancedSection(button: UILabel) {
  246. let willShow = !advancedSectionShowing
  247. // extract indexPaths from advancedCells
  248. let advancedIndexPaths: [IndexPath] = advancedSectionCells.indices.map { IndexPath(row: $0, section: 2) }
  249. // advancedSectionCells.indices.map({indexPaths.append(IndexPath(row: $0, section: 1))}
  250. // set flag before delete/insert operation, because cellForRowAt will be triggered and uses this flag
  251. advancedSectionShowing = willShow
  252. button.text = willShow ? "Hide" : "Show"
  253. if willShow {
  254. tableView.insertRows(at: advancedIndexPaths, with: .fade)
  255. } else {
  256. tableView.deleteRows(at: advancedIndexPaths, with: .fade)
  257. }
  258. }
  259. @objc private func loginButtonPressed() {
  260. guard let emailAddress = emailCell.getText() else {
  261. return // handle case when either email or pw fields are empty
  262. }
  263. let oAuthStarted = showOAuthAlertIfNeeded(emailAddress: emailAddress, handleCancel: loginButtonPressed) // if canceled we will run this method again but this time oAuthStarted will be false
  264. if oAuthStarted {
  265. // the loginFlow will be handled by oAuth2
  266. return
  267. }
  268. let password = passwordCell.getText() ?? "" // empty passwords are ok -> for oauth there is no password needed
  269. login(emailAddress: emailAddress, password: password)
  270. }
  271. private func login(emailAddress: String, password: String, skipAdvanceSetup: Bool = false) {
  272. MRConfig.addr = emailAddress
  273. MRConfig.mailPw = password
  274. if !skipAdvanceSetup {
  275. evaluluateAdvancedSetup() // this will set MRConfig related to advanced fields
  276. }
  277. dc_configure(mailboxPointer)
  278. hudHandler.showBackupHud("Configuring account")
  279. }
  280. @objc func closeButtonPressed() {
  281. dismiss(animated: true, completion: nil)
  282. }
  283. // returns true if needed
  284. private func showOAuthAlertIfNeeded(emailAddress: String, handleCancel: (() -> Void)?) -> Bool {
  285. if MRConfig.getAuthFlags() == 4 {
  286. // user has previously denied oAuth2-setup
  287. return false
  288. }
  289. guard let oAuth2UrlPointer = dc_get_oauth2_url(mailboxPointer, emailAddress, "chat.delta:/auth") else {
  290. return false
  291. }
  292. let oAuth2Url = String(cString: oAuth2UrlPointer)
  293. if let url = URL(string: oAuth2Url) {
  294. let title = "Continue with simplified setup"
  295. // swiftlint:disable all
  296. 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."
  297. let oAuthAlertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
  298. let confirm = UIAlertAction(title: "Confirm", style: .default, handler: {
  299. [unowned self] _ in
  300. let nc = NotificationCenter.default
  301. self.oauth2Observer = nc.addObserver(self, selector: #selector(self.oauthLoginApproved), name: NSNotification.Name("oauthLoginApproved"), object: nil)
  302. self.launchOAuthBrowserWindow(url: url)
  303. })
  304. let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: {
  305. _ in
  306. MRConfig.setAuthFlags(flags: Int(DC_LP_AUTH_NORMAL))
  307. handleCancel?()
  308. })
  309. oAuthAlertController.addAction(confirm)
  310. oAuthAlertController.addAction(cancel)
  311. present(oAuthAlertController, animated: true, completion: nil)
  312. return true
  313. } else {
  314. return false
  315. }
  316. }
  317. @objc func oauthLoginApproved(notification: Notification) {
  318. guard let userInfo = notification.userInfo, let token = userInfo["token"] as? String, let emailAddress = emailCell.getText() else {
  319. return
  320. }
  321. passwordCell.setText(text: token)
  322. MRConfig.setAuthFlags(flags: Int(DC_LP_AUTH_OAUTH2))
  323. login(emailAddress: emailAddress, password: token, skipAdvanceSetup: true)
  324. }
  325. private func launchOAuthBrowserWindow(url: URL) {
  326. UIApplication.shared.open(url) // this opens safari as seperate app
  327. }
  328. private func addProgressHudEventListener() {
  329. let nc = NotificationCenter.default
  330. backupProgressObserver = nc.addObserver(
  331. forName: dcNotificationBackupProgress,
  332. object: nil,
  333. queue: nil
  334. ) {
  335. notification in
  336. if let ui = notification.userInfo {
  337. if ui["error"] as! Bool {
  338. self.hudHandler.setHudError(ui["errorMessage"] as? String)
  339. } else if ui["done"] as! Bool {
  340. self.hudHandler.setHudDone(callback: self.handleLoginSuccess)
  341. } else {
  342. self.hudHandler.setHudProgress(ui["progress"] as! Int)
  343. }
  344. }
  345. }
  346. configureProgressObserver = nc.addObserver(
  347. forName: dcNotificationConfigureProgress,
  348. object: nil,
  349. queue: nil
  350. ) {
  351. notification in
  352. if let ui = notification.userInfo {
  353. if ui["error"] as! Bool {
  354. self.hudHandler.setHudError(ui["errorMessage"] as? String)
  355. } else if ui["done"] as! Bool {
  356. self.hudHandler.setHudDone(callback: self.handleLoginSuccess)
  357. } else {
  358. self.hudHandler.setHudProgress(ui["progress"] as! Int)
  359. }
  360. }
  361. }
  362. }
  363. private func evaluluateAdvancedSetup() {
  364. for cell in advancedSectionCells {
  365. if let textFieldCell = cell as? TextFieldCell {
  366. switch cell.accessibilityIdentifier {
  367. case "IMAPServerCell":
  368. MRConfig.mailServer = textFieldCell.getText() ?? nil
  369. case "IMAPUserCell":
  370. MRConfig.mailUser = textFieldCell.getText() ?? nil
  371. case "IMAPPortCell":
  372. MRConfig.mailPort = textFieldCell.getText() ?? nil
  373. case "IMAPSecurityCell":
  374. let flag = 0
  375. MRConfig.setImapSecurity(imapFlags: flag)
  376. case "SMTPServerCell":
  377. MRConfig.sendServer = textFieldCell.getText() ?? nil
  378. case "SMTPSUserCell":
  379. MRConfig.sendUser = textFieldCell.getText() ?? nil
  380. case "SMTPPortCell":
  381. MRConfig.sendPort = textFieldCell.getText() ?? nil
  382. case "SMTPPasswordCell":
  383. MRConfig.sendPw = textFieldCell.getText() ?? nil
  384. case "SMTPSecurityCell":
  385. let flag = 0
  386. MRConfig.setSmtpSecurity(smptpFlags: flag)
  387. default:
  388. logger.info("unknown identifier", cell.accessibilityIdentifier ?? "")
  389. }
  390. }
  391. }
  392. }
  393. private func restoreBackup() {
  394. logger.info("restoring backup")
  395. if MRConfig.configured {
  396. return
  397. }
  398. let documents = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
  399. if !documents.isEmpty {
  400. logger.info("looking for backup in: \(documents[0])")
  401. if let file = dc_imex_has_backup(mailboxPointer, documents[0]) {
  402. logger.info("restoring backup: \(String(cString: file))")
  403. hudHandler.showBackupHud("Restoring Backup")
  404. dc_imex(mailboxPointer, DC_IMEX_IMPORT_BACKUP, file, nil)
  405. return
  406. }
  407. let alert = UIAlertController(title: "Can not restore", message: "No Backup found", preferredStyle: .alert)
  408. alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: { _ in
  409. }))
  410. present(alert, animated: true, completion: nil)
  411. return
  412. }
  413. logger.error("no documents directory found")
  414. }
  415. private func handleLoginSuccess() {
  416. // used when login hud successfully went trough
  417. dismiss(animated: true, completion: nil)
  418. }
  419. private func resignFirstResponderOnAllCells() {
  420. basicSectionCells.map({
  421. resignCell(cell: $0)
  422. })
  423. advancedSectionCells.map({
  424. resignCell(cell: $0)
  425. }
  426. )
  427. }
  428. func resignCell(cell: UITableViewCell) {
  429. if let c = cell as? TextFieldCell {
  430. c.textField.resignFirstResponder()
  431. }
  432. }
  433. }
  434. extension AccountSetupController: UITextFieldDelegate {
  435. func textFieldShouldReturn(_ textField: UITextField) -> Bool {
  436. let currentTag = textField.tag
  437. if textField.accessibilityIdentifier == "emailTextField", showOAuthAlertIfNeeded(emailAddress: textField.text ?? "", handleCancel: {
  438. self.passwordCell.textField.becomeFirstResponder()
  439. }) {
  440. // all the action is defined in if condition
  441. } else {
  442. if let nextField = tableView.viewWithTag(currentTag + 1) as? UITextField {
  443. if nextField.tag > 1, !advancedSectionShowing {
  444. // gets here when trying to activate a collapsed cell
  445. return false
  446. }
  447. nextField.becomeFirstResponder()
  448. }
  449. }
  450. return false
  451. }
  452. }
  453. class AdvancedSectionHeader: UIView {
  454. var handleTap: ((UILabel) -> Void)?
  455. private var label: UILabel = {
  456. let label = UILabel()
  457. label.text = "ADVANCED"
  458. label.font = UIFont.systemFont(ofSize: 15)
  459. label.textColor = UIColor.darkGray
  460. return label
  461. }()
  462. /*
  463. 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
  464. */
  465. private lazy var toggleButton: UILabel = {
  466. let label = UILabel()
  467. label.text = "Show"
  468. label.font = UIFont.systemFont(ofSize: 15, weight: .medium)
  469. label.textColor = UIColor.systemBlue
  470. return label
  471. }()
  472. //
  473. // private var toggleButton:UIButton = {
  474. // let button = UIButton(type: .system)
  475. // button.setTitle("Show", for: .normal)
  476. // button.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside )
  477. // //button.target(forAction: #selector(buttonTapped(_:)), withSender: self)
  478. // return button
  479. // }()
  480. init() {
  481. super.init(frame: .zero) // will be constraint from tableViewDelegate
  482. setupSubviews()
  483. let tap = UITapGestureRecognizer(target: self, action: #selector(viewTapped)) // use this if the whole header is supposed to be clickable
  484. addGestureRecognizer(tap)
  485. }
  486. required init?(coder _: NSCoder) {
  487. fatalError("init(coder:) has not been implemented")
  488. }
  489. func setupSubviews() {
  490. addSubview(label)
  491. label.translatesAutoresizingMaskIntoConstraints = false
  492. label.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 15).isActive = true
  493. label.centerYAnchor.constraint(equalTo: centerYAnchor, constant: 0).isActive = true
  494. addSubview(toggleButton)
  495. toggleButton.translatesAutoresizingMaskIntoConstraints = false
  496. toggleButton.leadingAnchor.constraint(equalTo: trailingAnchor, constant: -60).isActive = true // since button will change title it should be left aligned
  497. toggleButton.centerYAnchor.constraint(equalTo: label.centerYAnchor, constant: 0).isActive = true
  498. }
  499. @objc func buttonTapped(_: UIButton) {
  500. // handleTap?(button)
  501. }
  502. @objc func viewTapped() {
  503. handleTap?(toggleButton)
  504. }
  505. }
  506. /*
  507. class InputTableViewCell: UITableViewCell {
  508. lazy var inputField: UITextField = {
  509. let textField = UITextField()
  510. return textField
  511. }()
  512. init() {
  513. super.init(style: .default, reuseIdentifier: nil)
  514. setupView()
  515. }
  516. required init?(coder aDecoder: NSCoder) {
  517. fatalError("init(coder:) has not been implemented")
  518. }
  519. private func setupView() {
  520. contentView.addSubview(inputField)
  521. inputField.translatesAutoresizingMaskIntoConstraints = false
  522. inputField.centerYAnchor.constraint(equalTo: contentView.centerYAnchor, constant: 0).isActive = true
  523. inputField.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 5).isActive = true
  524. inputField.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -5).isActive = true
  525. inputField.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 100).isActive = true
  526. inputField.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: 0).isActive = true
  527. }
  528. public func getText() -> String? {
  529. return inputField.text
  530. }
  531. }
  532. class PasswordInputCell: UITableViewCell {
  533. lazy var inputField: UITextField = {
  534. let textField = UITextField()
  535. textField.isSecureTextEntry = true
  536. return textField
  537. }()
  538. // TODO: to add Eye-icon -> uncomment -> add to inputField.rightView
  539. /*
  540. lazy var makeVisibleIcon: UIImageView = {
  541. let view = UIImageView(image: )
  542. return view
  543. }()
  544. */
  545. init() {
  546. super.init(style: .default, reuseIdentifier: nil)
  547. setupView()
  548. }
  549. required init?(coder aDecoder: NSCoder) {
  550. fatalError("init(coder:) has not been implemented")
  551. }
  552. private func setupView() {
  553. contentView.addSubview(inputField)
  554. inputField.translatesAutoresizingMaskIntoConstraints = false
  555. inputField.centerYAnchor.constraint(equalTo: contentView.centerYAnchor, constant: 0).isActive = true
  556. inputField.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 5).isActive = true
  557. inputField.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -5).isActive = true
  558. inputField.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 100).isActive = true
  559. inputField.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: 0).isActive = true
  560. }
  561. public func getText() -> String? {
  562. return inputField.text
  563. }
  564. }
  565. */