You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
5.2 KiB

6 years ago
  1. //
  2. // FillSignUpCodeController.swift
  3. // GMERemittance
  4. //
  5. // Created by Sujal on 12/11/17.
  6. // Copyright © 2017 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import Foundation
  9. import UIKit
  10. class FillSignUpCodeController: UIViewController {
  11. @IBOutlet var labelEnterCodeHeader: UILabel!
  12. @IBOutlet weak var buttonSubmitCode: UIButton!
  13. private var fillsignupcodeviewmodel = FillCodeViewModel()
  14. private var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
  15. var deviceORpassword: Int? //1 for device, 2 for password
  16. var redirectToOTP: String?
  17. @IBOutlet weak var textFieldCode: UITextField!
  18. @IBAction func cancelAction(_ sender: Any) {
  19. // navigationController?.popViewController(animated: true)
  20. }
  21. @IBAction func requestCode(_ sender: Any) {
  22. if deviceORpassword == 1 {
  23. fillsignupcodeviewmodel.setRecoveryType(recoveryType: "device")
  24. } else if deviceORpassword == 2 {
  25. fillsignupcodeviewmodel.setRecoveryType(recoveryType: "password")
  26. }
  27. fillsignupcodeviewmodel.requestCode()
  28. //self.popUpMessageError(value: 11, message: "Please enter the code sent to your email")
  29. }
  30. @IBAction func submitCode(_ sender: Any) {
  31. let signupcode: String = textFieldCode.text!
  32. fillsignupcodeviewmodel.setCode(code: signupcode)
  33. switch fillsignupcodeviewmodel.checkCodeLength() {
  34. case .Valid:
  35. disableUserInteractions()
  36. showActivityIndicator(activityIndicator: activityIndicator)
  37. if deviceORpassword == 1 {
  38. fillsignupcodeviewmodel.authenticate()
  39. } else if deviceORpassword == 2 {
  40. fillsignupcodeviewmodel.enablePasswordChange()
  41. } else {
  42. self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
  43. }
  44. case .InValid(let error):
  45. self.popUpMessageError(value: 11, message: error)
  46. }
  47. }
  48. override func viewDidLoad() {
  49. super.viewDidLoad()
  50. setUpNavBar(id: 100, title: "")
  51. let userId = UserDefaults.standard.object(forKey: "com.gmeremit.username") as! String
  52. if let _ = Int(userId) {
  53. labelEnterCodeHeader.text = "Enter Four-Digit Code send via sms"
  54. } else {
  55. labelEnterCodeHeader.text = "Enter Four-Digit Code send via email"
  56. }
  57. if self.redirectToOTP != nil{
  58. self.popUpMessageInfo(value: 16, title: "Alert", message: self.redirectToOTP!)
  59. }
  60. if deviceORpassword == 1 {
  61. buttonSubmitCode.setTitle("Done", for: .normal)
  62. } else if deviceORpassword == 2 {
  63. buttonSubmitCode.setTitle("Recovery Password", for: .normal)
  64. }
  65. self.hideKeyboardWhenTappedAround()
  66. textFieldCode.delegate = self
  67. fillsignupcodeviewmodel.setUserId(userId: UserDefaults.standard.object(forKey: "com.gmeremit.username") as! String)
  68. fillsignupcodeviewmodel.fillCodeConnectionTimeOut.value = nil
  69. /**
  70. connection timeout
  71. */
  72. fillsignupcodeviewmodel.fillCodeConnectionTimeOut.bind { [unowned self] in
  73. guard $0 != nil else {
  74. return
  75. }
  76. self.enableUserInteractions()
  77. self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
  78. self.popUpMessage(value: 20)
  79. }
  80. fillsignupcodeviewmodel.internetConnection.value = nil
  81. /**
  82. Internet check
  83. */
  84. fillsignupcodeviewmodel.internetConnection.bind { [unowned self] in
  85. guard $0 != nil else {
  86. return
  87. }
  88. self.enableUserInteractions()
  89. self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
  90. self.popUpMessage(value: 15)
  91. }
  92. /**
  93. Update authenticate code
  94. */
  95. fillsignupcodeviewmodel.authenticated.bind { [unowned self] in
  96. guard $0 != nil else {
  97. return
  98. }
  99. self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
  100. self.enableUserInteractions()
  101. guard $0! else {
  102. self.popUpMessageError(value: 10, message: self.fillsignupcodeviewmodel.getErrorMessage())
  103. return
  104. }
  105. if self.deviceORpassword == 1 {
  106. self.performSegue(withIdentifier: "loginNormalMode", sender: nil)
  107. } else if self.deviceORpassword == 2 {
  108. self.performSegue(withIdentifier: "changePassword", sender: nil)
  109. }
  110. }
  111. }
  112. }
  113. extension FillSignUpCodeController: UITextFieldDelegate {
  114. func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
  115. guard let code = textField.text else {
  116. return true
  117. }
  118. let length = code.count + string.count - range.length
  119. return length <= 4
  120. }
  121. }