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.

246 lines
8.3 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. //
  2. // PennyTestSubmitViewController.swift
  3. // GME Remit
  4. //
  5. // Created by Mac on 11/28/18.
  6. //Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import UIKit
  9. import PMAlertController
  10. import FirebaseInstanceID
  11. class PennyTestSubmitViewController: UIViewController {
  12. struct StringConstants {
  13. let navigationTitle = "penny_test_title_text".localized()
  14. let depositReferenceText = "deposit_reference_text".localized()
  15. let viewSampleText = "view_sample_text".localized()
  16. let resendRequestText = "resend_request_text".localized()
  17. let submitText = "submit_text".localized()
  18. let notNowText = "not_now_text".localized()
  19. }
  20. // MARK: Properties
  21. var presenter: PennyTestSubmitModuleInterface?
  22. weak var parentViewcontroller: UIViewController?
  23. // MARK: IBOutlets
  24. @IBOutlet weak var dipositReferencelabel: UILabel!
  25. @IBOutlet weak var titleLabel: UILabel!
  26. @IBOutlet weak var verificationCodeTextField: UITextField!
  27. @IBOutlet weak var viewSampleButton: UIButton!
  28. @IBOutlet weak var resendButton: UIButton!
  29. @IBOutlet weak var submitButton: UIButton!
  30. @IBOutlet weak var notNowButton: UIButton!
  31. // MARK: VC's Life cycle
  32. override func viewDidLoad() {
  33. super.viewDidLoad()
  34. self.setup()
  35. }
  36. override func viewWillAppear(_ animated: Bool) {
  37. super.viewWillAppear(animated)
  38. self.setupNormalNavigation()
  39. self.navigationItem.title = StringConstants().navigationTitle
  40. self.navigationItem.hidesBackButton = true
  41. }
  42. // MARK: IBActions
  43. // MARK: Other Functions
  44. private func setup() {
  45. // all setup should be done here
  46. requestPennytestInfoDuringViewLoad()
  47. let message = UserDefaults.standard.string(forKey: AppConstants.pennyTestServerMessage)
  48. self.titleLabel.text = message
  49. configureText()
  50. }
  51. private func configureText() {
  52. self.titleLabel.text = "penny_test_promt_description_text".localized()
  53. self.dipositReferencelabel.text = StringConstants().depositReferenceText
  54. self.viewSampleButton.setTitle(StringConstants().viewSampleText, for: UIControlState.normal)
  55. self.resendButton.setTitle(StringConstants().resendRequestText, for: UIControlState.normal)
  56. self.submitButton.setTitle(StringConstants().submitText, for: UIControlState.normal)
  57. self.notNowButton.setTitle(StringConstants().notNowText, for: UIControlState.normal)
  58. }
  59. @IBAction func viewSample(_ sender: Any) {
  60. self.showSampleImage()
  61. }
  62. @IBAction func resend(_ sender: Any) {
  63. self.proceed()
  64. }
  65. @IBAction func dismiss(_ sender: Any) {
  66. self.dismiss()
  67. }
  68. private func dismiss() {
  69. parentViewcontroller?.dismiss(animated: true, completion: nil)
  70. self.dismiss(animated: true, completion: nil)
  71. if let navigation = self.navigationController {
  72. navigation.popViewController(animated: true)
  73. }
  74. }
  75. func showSampleImage() {
  76. let image = #imageLiteral(resourceName: "penny3.jpg")
  77. let alertVC = PMAlertController(title: "", description: "", image: image, style: .alert)
  78. alertVC.alertView.layer.cornerRadius = 10
  79. let action = PMAlertAction(title: "ok_text".localized(), style: .cancel)
  80. alertVC.addAction(action)
  81. self.present(alertVC, animated: true, completion: nil)
  82. }
  83. // resend
  84. private func proceed() {
  85. let userId = Utility.getMyUserName()
  86. self.showLoading()
  87. self.initiate(userId: userId, isResendRequest: "Y", success: { result in
  88. self.hideLoading()
  89. self.alertWithOk(message: result.message, title: "success_text".localized(), okTitle: "ok_text".localized(), style: UIAlertControllerStyle.alert, OkStyle: UIAlertActionStyle.default, okAction: nil)
  90. self.updatePennyTestRequestStatus()
  91. }) { (error) in
  92. self.updatePennyTestRequestStatus()
  93. self.hideLoading()
  94. self.alert(message: error.localizedDescription)
  95. }
  96. }
  97. private func requestPennytestInfoDuringViewLoad() {
  98. let message = UserDefaults.standard.string(forKey: AppConstants.pennyTestServerMessage)
  99. let userId = Utility.getMyUserName()
  100. self.showLoading()
  101. self.initiate(userId: userId, isResendRequest: "N", success: { result in
  102. self.hideLoading()
  103. self.titleLabel.text = result.message
  104. UserDefaults.standard.set(result.message, forKey: AppConstants.pennyTestServerMessage)
  105. self.updatePennyTestRequestStatus()
  106. }) { (error) in
  107. self.updatePennyTestRequestStatus()
  108. self.hideLoading()
  109. self.alert(message: error.localizedDescription)
  110. }
  111. }
  112. private func updatePennyTestRequestStatus() {
  113. let _default = UserDefaults.standard
  114. _default.set(PennyTestStatusCode.requested.rawValue, forKey: AppConstants.pennyTestStatusCode)
  115. }
  116. private func showLoading() {
  117. self.showProgressHud()
  118. }
  119. private func hideLoading() {
  120. self.hideProgressHud()
  121. }
  122. @IBAction func Verify(_ sender: UIButton) {
  123. let customerId = Utility.getMyId()
  124. let certNumber = self.verificationCodeTextField.text!.removeSpacesTrailingPreceding()
  125. if certNumber.isEmpty || certNumber.count != 4 {
  126. self.alert(message: "valid_verification_code_error".localized())
  127. return
  128. }
  129. let accountNumber = Utility.getMyPrimaryAccountNumber()
  130. let params =
  131. [
  132. "AccountNo": accountNumber,
  133. "CertNumber": certNumber,
  134. "CustomerId": customerId
  135. ]
  136. self.submit(params: params, success: { (response) in
  137. // UPDATE BALANCE
  138. let balance = response.extra ?? ""
  139. let userInfo = [SideMenuNavigationNotifications.availableBalance : balance]
  140. UserDefaults.standard.set(balance, forKey: UserKeys.availableBalance)
  141. NotificationCenter.default.post(name: self.getAvailableBalanceNotificationName(), object: nil, userInfo: userInfo)
  142. // UPDATE YEARLY LIMIT
  143. let limit = response.yearlyLimit ?? ""
  144. let userInfo2 = [AppConstants.yearlyLimitNotification : limit]
  145. NotificationCenter.default.post(name: self.getAvailableBalanceNotificationName(), object: nil, userInfo: userInfo2)
  146. // show alert and dismiss
  147. self.alertWithOk(message: response.message ?? "", title: "Success", okTitle: "Ok", style: UIAlertControllerStyle.alert
  148. , OkStyle: .default, okAction: {
  149. UserDefaults.standard.set(PennyTestStatusCode.completed.rawValue, forKey: AppConstants.pennyTestStatusCode)
  150. self.dismiss()
  151. })
  152. }) { (error) in
  153. self.alert(message: error.localizedDescription)
  154. }
  155. }
  156. func getAvailableBalanceNotificationName() -> Notification.Name {
  157. return Notification.Name.init(rawValue: SideMenuNavigationNotifications.availableBalance)
  158. }
  159. private func getFcmToken() -> String? {
  160. let token = InstanceID.instanceID().token()
  161. return token
  162. }
  163. func fetchUserInfo() {
  164. let userId = Utility.getMyUserName()
  165. var param = ["userId" : userId]
  166. param["uuid"] = Utility.getUUid() ?? ""
  167. param["appVersion"] = Utility.getAppVersion()
  168. param["phoneBrand"] = Utility.getPhoneBrand()
  169. param["phoneOs"] = Utility.getPhoneOs()
  170. param["fcmId"] = self.getFcmToken()
  171. param["osVersion"] = Utility.getOsVersion()
  172. self.fetchUserInfo(param: param, success: { user in
  173. self.navigationController?.popToRootViewController(animated: true)
  174. }) { error in
  175. self.navigationController?.popToRootViewController(animated: true)
  176. }
  177. }
  178. }
  179. // MARK: PennyTestSubmitViewInterface
  180. extension PennyTestSubmitViewController: PennyTestSubmitViewInterface {
  181. }
  182. extension PennyTestSubmitViewController: SubmitPennyTestService {
  183. }
  184. extension PennyTestSubmitViewController: InitiatePennyTestService {
  185. }
  186. extension PennyTestSubmitViewController: UserInfoService {
  187. }