AccountSetupController.swift 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 UIKit
  9. class AccountSetupController: UITableViewController {
  10. var backupProgressObserver: Any?
  11. var configureProgressObserver: Any?
  12. lazy var hudHandler: HudHandler = {
  13. let hudHandler = HudHandler(parentView: self.tableView)
  14. return hudHandler
  15. }()
  16. lazy var emailCell:InputTableViewCell = {
  17. let cell = InputTableViewCell()
  18. cell.textLabel?.text = "Email"
  19. cell.inputField.placeholder = "user@example.com"
  20. return cell
  21. }()
  22. lazy var passwordCell:PasswordInputCell = {
  23. let cell = PasswordInputCell()
  24. cell.textLabel?.text = "Password"
  25. cell.inputField.placeholder = "Required"
  26. return cell
  27. }()
  28. init() {
  29. super.init(style: .grouped)
  30. }
  31. required init?(coder aDecoder: NSCoder) {
  32. fatalError("init(coder:) has not been implemented")
  33. }
  34. override func viewDidLoad() {
  35. super.viewDidLoad()
  36. self.title = "Login to your server"
  37. self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Login", style: .done, target: self, action: #selector(loginButtonPressed))
  38. }
  39. override func viewDidAppear(_ animated: Bool) {
  40. super.viewDidAppear(animated)
  41. addProgressHudEventListener()
  42. }
  43. override func viewDidDisappear(_ animated: Bool) {
  44. let nc = NotificationCenter.default
  45. if let backupProgressObserver = self.backupProgressObserver {
  46. nc.removeObserver(backupProgressObserver)
  47. }
  48. if let configureProgressObserver = self.configureProgressObserver {
  49. nc.removeObserver(configureProgressObserver)
  50. }
  51. }
  52. // MARK: - Table view data source
  53. override func numberOfSections(in tableView: UITableView) -> Int {
  54. // #warning Incomplete implementation, return the number of sections
  55. return 2
  56. }
  57. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  58. // #warning Incomplete implementation, return the number of rows
  59. if section == 0 {
  60. return 2
  61. } else {
  62. return 0
  63. }
  64. }
  65. override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  66. if section == 1 {
  67. return "Advanced"
  68. } else {
  69. return nil
  70. }
  71. }
  72. /*
  73. override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  74. if section == 0 {
  75. return nil
  76. } else {
  77. let label = UILabel()
  78. label.text = "Advanced"
  79. return label
  80. }
  81. }
  82. */
  83. override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
  84. if section == 0 {
  85. return "There are no Delta Chat servers, your data stays on your device!"
  86. } else {
  87. return nil
  88. }
  89. }
  90. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  91. let row = indexPath.row
  92. if row == 0 {
  93. return emailCell
  94. } else {
  95. return passwordCell
  96. }
  97. }
  98. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  99. // handle tap on password
  100. print(indexPath)
  101. }
  102. @objc func loginButtonPressed() {
  103. guard let emailAddress = emailCell.getText() else {
  104. return // handle case when either email or pw fields are empty
  105. }
  106. let oAuthStared = showOAuthAlertIfNeeded(emailAddress: emailAddress)
  107. if oAuthStared {
  108. return
  109. }
  110. let passWord = passwordCell.getText() ?? "" // empty passwords are ok -> for oauth there is no password needed
  111. MRConfig.addr = emailAddress
  112. MRConfig.mailPw = passWord
  113. dc_configure(mailboxPointer)
  114. hudHandler.showBackupHud("Configuring account")
  115. }
  116. // returns true if needed
  117. private func showOAuthAlertIfNeeded(emailAddress: String) -> Bool {
  118. guard let oAuth2UrlPointer = dc_get_oauth2_url(mailboxPointer, emailAddress, "chat.delta:/auth") else {
  119. return false
  120. }
  121. let oAuth2Url = String(cString: oAuth2UrlPointer)
  122. // TODO: open webView with url
  123. if let url = URL.init(string: oAuth2Url) {
  124. UIApplication.shared.open(url)
  125. return true
  126. } else {
  127. return false
  128. }
  129. }
  130. private func addProgressHudEventListener() {
  131. let nc = NotificationCenter.default
  132. backupProgressObserver = nc.addObserver(
  133. forName: dcNotificationBackupProgress,
  134. object: nil,
  135. queue: nil
  136. ) {
  137. notification in
  138. if let ui = notification.userInfo {
  139. if ui["error"] as! Bool {
  140. self.hudHandler.setHudError(ui["errorMessage"] as? String)
  141. } else if ui["done"] as! Bool {
  142. self.hudHandler.setHudDone(callback: nil)
  143. } else {
  144. self.hudHandler.setHudProgress(ui["progress"] as! Int)
  145. }
  146. }
  147. }
  148. configureProgressObserver = nc.addObserver(
  149. forName: dcNotificationConfigureProgress,
  150. object: nil,
  151. queue: nil
  152. ) {
  153. notification in
  154. if let ui = notification.userInfo {
  155. if ui["error"] as! Bool {
  156. self.hudHandler.setHudError(ui["errorMessage"] as? String)
  157. } else if ui["done"] as! Bool {
  158. self.hudHandler.setHudDone(callback: nil)
  159. } else {
  160. self.hudHandler.setHudProgress(ui["progress"] as! Int)
  161. }
  162. }
  163. }
  164. }
  165. }
  166. class InputTableViewCell: UITableViewCell {
  167. lazy var inputField: UITextField = {
  168. let textField = UITextField()
  169. return textField
  170. }()
  171. init() {
  172. super.init(style: .default, reuseIdentifier: nil)
  173. setupView()
  174. }
  175. required init?(coder aDecoder: NSCoder) {
  176. fatalError("init(coder:) has not been implemented")
  177. }
  178. private func setupView() {
  179. contentView.addSubview(inputField)
  180. inputField.translatesAutoresizingMaskIntoConstraints = false
  181. inputField.centerYAnchor.constraint(equalTo: contentView.centerYAnchor, constant: 0).isActive = true
  182. inputField.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 5).isActive = true
  183. inputField.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -5).isActive = true
  184. inputField.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 100).isActive = true
  185. inputField.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: 0).isActive = true
  186. }
  187. public func getText() -> String? {
  188. return inputField.text
  189. }
  190. }
  191. class PasswordInputCell: UITableViewCell {
  192. lazy var inputField: UITextField = {
  193. let textField = UITextField()
  194. textField.isSecureTextEntry = true
  195. return textField
  196. }()
  197. // TODO: to add Eye-icon -> uncomment -> add to inputField.rightView
  198. /*
  199. lazy var makeVisibleIcon: UIImageView = {
  200. let view = UIImageView(image: )
  201. return view
  202. }()
  203. */
  204. init() {
  205. super.init(style: .default, reuseIdentifier: nil)
  206. setupView()
  207. }
  208. required init?(coder aDecoder: NSCoder) {
  209. fatalError("init(coder:) has not been implemented")
  210. }
  211. private func setupView() {
  212. contentView.addSubview(inputField)
  213. inputField.translatesAutoresizingMaskIntoConstraints = false
  214. inputField.centerYAnchor.constraint(equalTo: contentView.centerYAnchor, constant: 0).isActive = true
  215. inputField.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 5).isActive = true
  216. inputField.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -5).isActive = true
  217. inputField.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 100).isActive = true
  218. inputField.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: 0).isActive = true
  219. }
  220. public func getText() -> String? {
  221. return inputField.text
  222. }
  223. }