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.

131 lines
4.0 KiB

6 years ago
  1. //
  2. // MoneyRequestViewModel.swift
  3. // GMERemittance
  4. //
  5. // Created by Sujal on 3/5/18.
  6. // Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import Foundation
  9. class MoneyRequestViewModel: ModelExtension {
  10. var moneyRequests: [Transaction]?
  11. var requestRejected: Box<Bool?> = Box(nil)
  12. var moneyRequestConnectionTimeOut: Box<Bool?> = Box(nil)
  13. private var password: String!
  14. private let user_id = UserDefaults.standard.object(forKey: "com.gmeremit.username") as! String
  15. }
  16. extension MoneyRequestViewModel {
  17. /**
  18. To set password for money request
  19. - parameter password: user id password
  20. */
  21. func setPassword(password: String) {
  22. self.password = password
  23. }
  24. /**
  25. To get the password
  26. - returns: password
  27. */
  28. func getPassword() -> String {
  29. return password
  30. }
  31. /**
  32. To get data for transaction
  33. - parameter transactionID: transaction id
  34. - parameter transferType: transferType
  35. - returns: transaction info
  36. */
  37. func getParam(transactionID: String, transferType: String) -> [String: String] {
  38. let param = ["userId": user_id,
  39. "transferType": transferType,
  40. "transactionId": transactionID,
  41. "txnPassword": getPassword()]
  42. return param
  43. }
  44. /**
  45. Set the money request list
  46. - parameter requests: transaction array
  47. */
  48. func setMoneyRequests(requests: [Transaction]) {
  49. moneyRequests = requests
  50. }
  51. /**
  52. Get the money request list
  53. - returns: transaction array
  54. */
  55. func getMoneyRequests() -> [Transaction] {
  56. if moneyRequests != nil {
  57. return moneyRequests!
  58. }
  59. return []
  60. }
  61. /**
  62. To get the money request
  63. - parameter atIndex: position of Transaction in an array
  64. - returns: transaction
  65. */
  66. func getMoneyRequest(atIndex: Int) -> Transaction {
  67. return moneyRequests![atIndex]
  68. }
  69. /**
  70. To remove the money request
  71. - parameter atIndex: position of Transaction in an array
  72. */
  73. func removeMoneyRequest(atIndex: Int) {
  74. moneyRequests?.remove(at: atIndex)
  75. }
  76. /**
  77. Api request for reject the money request
  78. - parameter transactionID: String id for transaction
  79. - parameter transferType: String transfer type
  80. */
  81. func rejectRequest(transactionID: String, transferType: String) {
  82. if !Reachability.isConnectedToNetwork() {
  83. self.internetConnection.value = false
  84. } else {
  85. let param = getParam(transactionID: transactionID, transferType: transferType)
  86. RestApiMananger.sharedInstance.rejectMoneyRequest(param: param) { result in
  87. switch result {
  88. case .success(_):
  89. self.requestRejected.value = true
  90. case let .failure(errorJSON):
  91. self.setErrorMessage(message: errorJSON["message"].stringValue)
  92. self.requestRejected.value = false
  93. case .updateAccessCode:
  94. return
  95. RestApiMananger.sharedInstance.updateAccessCode(userId: self.user_id, password: self.getLoginPassword()) {
  96. result in
  97. if result != "Error"{
  98. let uuid = RestApiMananger.sharedInstance.getUUID()
  99. UserDefaults.standard.set((result + ":" + uuid).toBase64(), forKey: "com.gmeremit.accessCode")
  100. self.rejectRequest(transactionID: transactionID, transferType: transferType)
  101. }
  102. }
  103. case .logOutUser():
  104. RestApiMananger.sharedInstance.cancelExistingNetworkCalls()
  105. self.anotherLogin.value = true
  106. case .timeOut:
  107. self.moneyRequestConnectionTimeOut.value = false
  108. }
  109. }
  110. }
  111. }
  112. }