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.

252 lines
6.6 KiB

6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. //
  2. // SendMoneyCodeViewController.swift
  3. // GME Remit
  4. //
  5. // Created by gme_2 on 27/02/2019.
  6. //Copyright © 2019 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import UIKit
  9. import PMAlertController
  10. import Hex
  11. class SendMoneyCodeViewController: UIViewController {
  12. // MARK: IBOutlets
  13. @IBOutlet weak var otpCodeTitlelabel: UILabel!
  14. @IBOutlet weak var titleLabel: UILabel!
  15. @IBOutlet weak var verificationCodeTextField: UITextField!
  16. @IBOutlet weak var resendButton: UIButton!
  17. @IBOutlet weak var submitButton: UIButton!
  18. @IBOutlet weak var notNowButton: UIButton!
  19. @IBOutlet weak var counterLabel: UILabel!
  20. @IBOutlet weak var counterTitleLabel: UILabel!
  21. @IBOutlet weak var counterStackView: UIStackView!
  22. @IBOutlet weak var backGourndBorderView: UIView!
  23. var timer: Timer?
  24. let expiryTime = 5 // second
  25. var count: Int = 5 {
  26. didSet {
  27. if ( count <= 0) {
  28. count = expiryTime // two minutes passed
  29. self.timer?.invalidate()
  30. self.counterStackView.isHidden = true
  31. self.resendButton.isUserInteractionEnabled = true
  32. self.resendButton.isHidden = false
  33. } else {
  34. let result = count.quotientAndRemainder(dividingBy: 60)
  35. self.counterLabel.text = "\(result.quotient): \(result.remainder)"
  36. self.resendButton.isUserInteractionEnabled = false
  37. self.resendButton.isHidden = true
  38. }
  39. }
  40. }
  41. // MARK: Properties
  42. var mNumberTransKeyView : TransKeyView!
  43. var numberCipherString : String!
  44. var presenter: SendMoneyCodeModuleInterface?
  45. var completion: ((String) -> Void)?
  46. var type: CodeType = .sendMoney
  47. var newValue: String?
  48. private var requestModel: SendMoneyRequestModel?
  49. var encryptedNumber: String? {
  50. didSet {
  51. touchSubmit(UIButton())
  52. }
  53. }
  54. var isShowNumber : Bool!
  55. var isTranskeyShowing : Bool!
  56. // MARK: VC's Life cycle
  57. override func viewDidLoad() {
  58. super.viewDidLoad()
  59. self.setup()
  60. switch type {
  61. case .sendMoney:
  62. self.requestOtpCode()
  63. case .editProfile:
  64. startTimer()
  65. }
  66. }
  67. override func viewWillAppear(_ animated: Bool) {
  68. super.viewWillAppear(animated)
  69. backGourndBorderView.bottomToOrigin(duration: 0.1)
  70. }
  71. override func viewDidDisappear(_ animated: Bool) {
  72. super.viewDidDisappear(animated)
  73. }
  74. // MARK: IBActions
  75. @IBAction func touchSubmit(_ sender: UIButton) {
  76. if let number = encryptedNumber {
  77. self.dismiss(animated: true) {
  78. self.completion?(number)
  79. }
  80. } else {
  81. alert(type: .error, message: "4-digit OTP code is required.")
  82. }
  83. }
  84. // MARK: Other Functions
  85. private func setup() {
  86. // all setup should be done here
  87. self.titleLabel.text = "enter_otp_password_text".localized()
  88. self.verificationCodeTextField.placeholder = "otp_password_hint_text".localized()
  89. self.resendButton.setTitle("resend_request_text".localized(), for: UIControl.State.normal)
  90. self.submitButton.setTitle("submit_text".localized(), for: .normal)
  91. self.notNowButton.setTitle("not_now_text".localized(), for: .normal)
  92. verificationCodeTextField.delegate = self
  93. submitButton.layer.cornerRadius = 5
  94. submitButton.backgroundColor = .themeRed
  95. resendButton.setTitleColor(.themeRed, for: .normal)
  96. counterLabel.textColor = .themeRed
  97. }
  98. private func configureText() {
  99. self.titleLabel.text = "penny_test_promt_description_text".localized()
  100. self.otpCodeTitlelabel.text = "otp__Code_text".localized()
  101. self.resendButton.setTitle("resend_text".localized(), for: UIControl.State.normal)
  102. self.submitButton.setTitle("submit_text".localized(), for: UIControl.State.normal)
  103. self.notNowButton.setTitle("not_now_text".localized(), for: UIControl.State.normal)
  104. self.counterTitleLabel.text = "resend_code_text".localized()
  105. }
  106. @IBAction func viewSample(_ sender: Any) {
  107. self.showSampleImage()
  108. }
  109. @IBAction func resend(_ sender: Any) {
  110. switch type {
  111. case .sendMoney:
  112. self.requestOtpCode()
  113. case .editProfile:
  114. presenter?.changeProfileResendOTP(newValue: newValue ?? "")
  115. }
  116. }
  117. @IBAction func dismiss(_ sender: Any) {
  118. backGourndBorderView.originToBottom(duration: 0.1) {
  119. self.dismiss()
  120. }
  121. }
  122. private func dismiss() {
  123. self.dismiss(animated: true, completion: nil)
  124. }
  125. func showSampleImage() {
  126. let image = #imageLiteral(resourceName: "penny3.jpg")
  127. let alertVC = PMAlertController(title: "", description: "", image: image, style: .alert)
  128. alertVC.alertView.layer.cornerRadius = 5
  129. let action = PMAlertAction(title: "ok_text".localized(), style: .cancel)
  130. alertVC.addAction(action)
  131. self.present(alertVC, animated: true, completion: nil)
  132. }
  133. // resend
  134. private func requestOtpCode() {
  135. // request otp
  136. self.presenter?.viewIsReady()
  137. self.startTimer()
  138. }
  139. private func startTimer() {
  140. self.timer = Timer.scheduledTimer(
  141. timeInterval: 1,
  142. target: self,
  143. selector: #selector(self.updateCounter),
  144. userInfo: nil,
  145. repeats: true
  146. )
  147. self.timer?.fire()
  148. self.resendButton.isUserInteractionEnabled = false
  149. self.counterStackView.isHidden = false
  150. }
  151. @objc func updateCounter() {
  152. self.count -= 1
  153. }
  154. private func requestOtpCodeRetryCount() {
  155. }
  156. }
  157. // MARK: SendMoneyCodeViewInterface
  158. extension SendMoneyCodeViewController: SendMoneyCodeViewInterface {
  159. func successChangeProfileInformation() {
  160. self.alertWithOk(
  161. type: .success,
  162. message: "Type 4-digit OTP number",
  163. title: "new OTP number has re-sent",
  164. okTitle: "OK"
  165. )
  166. startTimer()
  167. }
  168. func showLoading() {
  169. self.showProgressHud()
  170. }
  171. func hideLoading() {
  172. self.hideProgressHud()
  173. }
  174. func show(message: String) {
  175. self.titleLabel.text = message
  176. }
  177. func show(error: String) {
  178. self.alert(type: .error, message: error)
  179. }
  180. func expiredError(error: Error) {
  181. alert(type: .error, message: error.localizedDescription, title: "Error") {
  182. self.dismiss()
  183. }
  184. }
  185. }
  186. // MARK: - UITextFieldDelegate
  187. extension SendMoneyCodeViewController: UITextFieldDelegate {
  188. func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
  189. let secureKeypad = SecureKeypad(target: self, keypadType: .numberic)
  190. secureKeypad.delegate = self
  191. secureKeypad.present(animated: true)
  192. return false
  193. }
  194. }
  195. // MARK: - OtpCodeRequestApiService
  196. extension SendMoneyCodeViewController: OtpCodeRequestApiService {}
  197. // MARK: - SecureKeypadDelegate
  198. extension SendMoneyCodeViewController: SecureKeypadDelegate {
  199. func didComplete(_ encryptedString: String, garbagePassword: String, length: Int) {
  200. if length != 0 {
  201. self.encryptedNumber = encryptedString
  202. }
  203. }
  204. }