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.

183 lines
6.1 KiB

6 years ago
  1. //
  2. // WithdrawViewController.swift
  3. // GMERemittance
  4. //
  5. // Created by Fm-user on 1/12/18.
  6. // Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import UIKit
  9. class WithdrawViewController: UIViewController {
  10. @IBOutlet weak var textViewWithdraw: UITextView!
  11. @IBOutlet weak var textFieldAmount: UITextField!
  12. private var withdrawviewmodel = WithdrawViewModel()
  13. private var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
  14. override func viewDidLoad() {
  15. super.viewDidLoad()
  16. setUpNavBar(id: 201, title: "Withdraw")
  17. setUpAnotherLoginListener(genericviewmodel: withdrawviewmodel)
  18. hideKeyboardWhenTappedAround()
  19. // Do any additional setup after loading the view.
  20. textViewWithdraw.delegate = self
  21. textViewWithdraw.text = "Reason for withdraw"
  22. textViewWithdraw.textColor = UIColor(red:0.78, green:0.78, blue:0.80, alpha:1.0)
  23. withdrawviewmodel.withdrawConnectionTimeOut.value = nil
  24. /**
  25. connection timeout
  26. */
  27. withdrawviewmodel.withdrawConnectionTimeOut.bind { [unowned self] in
  28. guard $0 != nil else {
  29. return
  30. }
  31. self.enableUserInteractions()
  32. self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
  33. self.popUpMessage(value: 20)
  34. }
  35. withdrawviewmodel.internetConnection.value = nil
  36. /**
  37. internet check
  38. */
  39. withdrawviewmodel.internetConnection.bind { [unowned self] in
  40. guard $0 != nil else {
  41. return
  42. }
  43. self.enableUserInteractions()
  44. self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
  45. self.popUpMessage(value: 15)
  46. }
  47. /**
  48. Update the for send and withdraw request
  49. */
  50. withdrawviewmodel.requestProcessed.bind { [unowned self] in
  51. guard $0 != nil else {
  52. return
  53. }
  54. self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
  55. self.enableUserInteractions()
  56. guard $0! else {
  57. self.popUpMessageError(value: 10, message: self.withdrawviewmodel.getErrorMessage())
  58. return
  59. }
  60. guard let navController = self.navigationController else {
  61. return
  62. }
  63. if let popUpViewController = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "popUpInfo") as? PopUpGeneralInfo {
  64. popUpViewController.indexValue = 3
  65. navController.addChildViewController(popUpViewController)
  66. popUpViewController.view.frame = navController.view.bounds
  67. navController.view.addSubview(popUpViewController.view)
  68. popUpViewController.didMove(toParentViewController: navController)
  69. }
  70. }
  71. }
  72. override func didReceiveMemoryWarning() {
  73. super.didReceiveMemoryWarning()
  74. // Dispose of any resources that can be recreated.
  75. }
  76. @IBAction func submitWithdrawRequest(_ sender: Any) {
  77. withdrawviewmodel.setParam(amount: textFieldAmount.text!, reason: textViewWithdraw.text)
  78. switch withdrawviewmodel.validateAmountandReason() {
  79. case .Valid:
  80. confirmRequest()
  81. case .InValid(let error):
  82. self.popUpMessageError(value: 11, message: error)
  83. }
  84. }
  85. /**
  86. Conforming for request
  87. */
  88. func confirmRequest() {
  89. let alertController = UIAlertController(title: "Enter your login password", message: nil, preferredStyle: .alert)
  90. alertController.addTextField { (textField : UITextField!) -> Void in
  91. textField.placeholder = "Password"
  92. textField.isSecureTextEntry = true
  93. textField.tag = 51
  94. textField.delegate = self
  95. }
  96. let confirmAction = UIAlertAction(title: "Confirm", style: .default, handler: {
  97. alert -> Void in
  98. let valueTextField = alertController.textFields![0] as UITextField
  99. if valueTextField.text != "" {
  100. self.disableUserInteractions()
  101. self.showActivityIndicator(activityIndicator: self.activityIndicator)
  102. self.withdrawviewmodel.sendWithdrawRequest(password: valueTextField.text!)
  103. } else {
  104. self.popUpMessageError(value: 11, message: "Password was missing")
  105. }
  106. })
  107. let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: {
  108. (action : UIAlertAction!) -> Void in
  109. })
  110. cancelAction.setValue(UIColor.black, forKey: "titleTextColor")
  111. confirmAction.setValue(UIColor(hex:0xEC1C24), forKey: "titleTextColor")
  112. alertController.addAction(cancelAction)
  113. alertController.addAction(confirmAction)
  114. self.present(alertController, animated: true, completion: nil)
  115. }
  116. }
  117. extension WithdrawViewController: UITextViewDelegate {
  118. func textViewDidBeginEditing(_ textViewWithdraw: UITextView) {
  119. if textViewWithdraw.textColor == UIColor(red:0.78, green:0.78, blue:0.80, alpha:1.0) {
  120. textViewWithdraw.text = nil
  121. textViewWithdraw.textColor = UIColor(red:0.29, green:0.29, blue:0.29, alpha:1.0)
  122. }
  123. }
  124. func textViewDidEndEditing(_ textViewWithdraw: UITextView) {
  125. if textViewWithdraw.text.isEmpty {
  126. textViewWithdraw.text = "Reason for withdraw"
  127. textViewWithdraw.textColor = UIColor(red:0.78, green:0.78, blue:0.80, alpha:1.0)
  128. }
  129. }
  130. }
  131. extension WithdrawViewController: UITextFieldDelegate {
  132. func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
  133. if textField.tag == 51 {
  134. textField.tag = 0
  135. return false
  136. }
  137. return true
  138. }
  139. }