RestoreSafeForgotIdViewController.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2018-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import UIKit
  21. class RestoreSafeForgotIdViewController: IDCreationPageViewController {
  22. @IBOutlet weak var descriptionLabel: UILabel!
  23. @IBOutlet weak var mobileNumberField: SetupTextField!
  24. @IBOutlet weak var emailAddressField: SetupTextField!
  25. @IBOutlet weak var cancelButton: SetupButton!
  26. @IBOutlet weak var okButton: SetupButton!
  27. var keyboardResize: KeyboardResizeCenterY?
  28. var locatedIdentities: [String]?
  29. var selectedIdentity: String?
  30. override func viewDidLoad() {
  31. super.viewDidLoad()
  32. self.hideKeyboardWhenTappedAround()
  33. self.keyboardResize = KeyboardResizeCenterY(parent: self.view, resize: self.mainContentView)
  34. self.descriptionLabel.text = NSLocalizedString("safe_search_id_title", comment: "")
  35. self.mobileNumberField.delegate = self
  36. self.mobileNumberField.placeholder = NSLocalizedString("safe_linked_mobile", comment: "")
  37. self.emailAddressField.delegate = self
  38. self.emailAddressField.placeholder = NSLocalizedString("safe_linked_email", comment: "")
  39. self.cancelButton.setTitle(NSLocalizedString("cancel", comment: ""), for: .normal)
  40. self.okButton.setTitle(NSLocalizedString("ok", comment: ""), for: .normal)
  41. self.okButton.deactivated = true
  42. }
  43. // MARK: - Navigation
  44. override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
  45. guard identifier == "okSafeForgotId" else {
  46. return true
  47. }
  48. return self.selectedIdentity != nil
  49. }
  50. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  51. if let forgotIdChooseViewController = segue.destination as? RestoreSafeForgotIdChooseViewController {
  52. self.mainContentView.isHidden = true
  53. forgotIdChooseViewController.ids = self.locatedIdentities
  54. }
  55. }
  56. // MARK: - Private methods
  57. private func searchIdentity() {
  58. if let email = self.emailAddressField.text,
  59. let mobileNo = self.mobileNumberField.text,
  60. email.count > 0 || mobileNo.count > 0 {
  61. let queue = DispatchQueue.global()
  62. let semaphore = DispatchSemaphore(value: 0)
  63. queue.async {
  64. ContactStore.shared()?.linkedIdentities(email, mobileNo: mobileNo, onCompletion: { (result) in
  65. if let result: Array = result {
  66. self.locatedIdentities = result.map({ (item) -> String in
  67. var itemName: String = ""
  68. if let itemDic = item as? Dictionary<String, String> {
  69. let identity = itemDic["identity"]!
  70. if itemDic.keys.contains("emailHash") {
  71. itemName = "\(identity) (\(email))"
  72. } else if itemDic.keys.contains("mobileNoHash") {
  73. itemName = "\(identity) (\(mobileNo))"
  74. }
  75. }
  76. return itemName
  77. })
  78. semaphore.signal()
  79. }
  80. })
  81. }
  82. semaphore.wait()
  83. if let identities = self.locatedIdentities {
  84. if identities.count > 1 {
  85. self.performSegue(withIdentifier: "SafeForgotIdChoose", sender: self)
  86. } else if identities.count == 1 {
  87. let id: String = identities[0]
  88. self.selectedIdentity = String(id[id.startIndex..<id.index(id.startIndex, offsetBy: 8)])
  89. self.performSegue(withIdentifier: "okSafeForgotId", sender: self)
  90. } else {
  91. let alert = IntroQuestionViewHelper(parent: self, onAnswer: nil)
  92. alert.showAlert(BundleUtil.localizedString(forKey: "safe_no_id_found"))
  93. }
  94. }
  95. }
  96. }
  97. }
  98. extension RestoreSafeForgotIdViewController: SetupTextFieldDelegate {
  99. func editingChangedTextField(_ sender: SetupTextField, forEvent event: UIEvent) {
  100. if let mobile = self.mobileNumberField.text, mobile.count > 0 {
  101. self.okButton.deactivated = false
  102. } else if let email = self.emailAddressField.text, email.count > 0 {
  103. self.okButton.deactivated = false
  104. } else {
  105. self.okButton.deactivated = true
  106. }
  107. }
  108. func primaryActionTriggered(_ sender: SetupTextField, forEvent event: UIEvent) {
  109. if sender == self.mobileNumberField {
  110. self.emailAddressField.becomeFirstResponder()
  111. } else if sender == self.emailAddressField {
  112. self.searchIdentity()
  113. }
  114. }
  115. }
  116. extension RestoreSafeForgotIdViewController {
  117. @IBAction func touchDownButton(_ sender: UIButton, forEvent event: UIEvent) {
  118. guard sender == okButton else {
  119. return
  120. }
  121. self.searchIdentity()
  122. }
  123. @IBAction func cancelSafeForgotIdChoose(_ segue: UIStoryboardSegue) {
  124. self.mainContentView.isHidden = false
  125. }
  126. @IBAction func choosenSafeForgotIdChoose(_ segue: UIStoryboardSegue) {
  127. self.mainContentView.isHidden = false
  128. if let forgotIdChooseViewController = segue.source as? RestoreSafeForgotIdChooseViewController {
  129. self.selectedIdentity = forgotIdChooseViewController.choosenId
  130. self.performSegue(withIdentifier: "okSafeForgotId", sender: self)
  131. }
  132. }
  133. }