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.

79 lines
2.9 KiB

6 years ago
  1. //
  2. // WithdrawViewModel.swift
  3. // GMERemittance
  4. //
  5. // Created by Sujal on 2/13/18.
  6. // Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import Foundation
  9. class WithdrawViewModel: ModelExtension {
  10. var amount: String!
  11. var reason: String!
  12. var requestProcessed: Box<Bool?> = Box(nil)
  13. var withdrawConnectionTimeOut: Box<Bool?> = Box(nil)
  14. let user_id = UserDefaults.standard.object(forKey: "com.gmeremit.username") as? String
  15. /**
  16. To check validation for withdraw
  17. */
  18. func validateAmountandReason() -> isValid {
  19. if amount.isBlank || reason.isBlank || reason == "Reason for withdraw" {
  20. return .InValid("Please fill all the fields")
  21. }
  22. return .Valid
  23. }
  24. /**
  25. To set amount and reason
  26. - parametet amount: withdraw amount
  27. - parameter reason: reason for withdraw
  28. */
  29. func setParam(amount: String, reason: String) {
  30. self.amount = amount.removeSpacesTrailingPreceding()
  31. self.reason = reason.removeSpacesTrailingPreceding()
  32. }
  33. /**
  34. To request for withdraw
  35. - parametet password: to comfirm with request user should type account password
  36. */
  37. func sendWithdrawRequest(password: String) {
  38. let passwordLocal = password
  39. let userId = UserDefaults.standard.object(forKey: "com.gmeremit.username") as? String
  40. let params: [String: String] = ["userId": userId!, "amount": amount, "reason": reason, "txnpassword": password]
  41. if !Reachability.isConnectedToNetwork() {
  42. self.internetConnection.value = false
  43. } else {
  44. RestApiMananger.sharedInstance.withdrawBalance(requestDetails: params) { result in
  45. switch result {
  46. case .success(_):
  47. self.requestProcessed.value = true
  48. case let .failure(errorJSON):
  49. self.setErrorMessage(message: errorJSON["message"].stringValue)
  50. self.requestProcessed.value = false
  51. case .updateAccessCode:
  52. RestApiMananger.sharedInstance.updateAccessCode(userId: self.user_id!, password: self.getLoginPassword()) {
  53. result in
  54. if result != "Error"{
  55. let uuid = RestApiMananger.sharedInstance.getUUID()
  56. UserDefaults.standard.set((result + ":" + uuid).toBase64(), forKey: "com.gmeremit.accessCode")
  57. self.sendWithdrawRequest(password: passwordLocal)
  58. }
  59. }
  60. case .logOutUser():
  61. RestApiMananger.sharedInstance.cancelExistingNetworkCalls()
  62. self.anotherLogin.value = true
  63. case .timeOut:
  64. self.withdrawConnectionTimeOut.value = false
  65. }
  66. }
  67. }
  68. }
  69. }