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.

119 lines
3.9 KiB

6 years ago
  1. //
  2. // TrackTransferViewModel.swift
  3. // GMERemittance
  4. //
  5. // Created by Fm-user on 2/5/18.
  6. // Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import Foundation
  9. import Alamofire
  10. class TrackRecipientViewModel: ModelExtension {
  11. var recipientListObtained: Box<Bool?> = Box(nil)
  12. var recipientArray = [Recipient]()
  13. var recipientName: String?
  14. var trackRecipientTimeOut: Box<Bool?> = Box(nil)
  15. var request: URLRequest!
  16. /**
  17. Api request for recipient list
  18. - parameter String: Reciver name
  19. */
  20. func fetchRecipientList(queryString: String) {
  21. if !Reachability.isConnectedToNetwork() {
  22. self.internetConnection.value = false
  23. } else {
  24. let query_string = queryString
  25. RestApiMananger.sharedInstance.getFilteredRecipientList(queryString: queryString, userId: getUserId()) { result in
  26. switch result {
  27. case let .success(fetchedJSON):
  28. self.recipientArray.removeAll()
  29. guard fetchedJSON.count > 0 else {
  30. self.recipientListObtained.value = true
  31. return
  32. }
  33. for i in 0 ... (fetchedJSON.count-1) {
  34. do {
  35. let recipient = try JSONDecoder().decode(Recipient.self, from: fetchedJSON[i].rawData())
  36. self.recipientArray.append(recipient)
  37. } catch {
  38. self.recipientListObtained.value = false
  39. }
  40. }
  41. self.recipientListObtained.value = true
  42. case let .failure(errorJSON):
  43. self.setErrorMessage(message: errorJSON["message"].stringValue)
  44. self.recipientListObtained.value = false
  45. case .updateAccessCode:
  46. RestApiMananger.sharedInstance.updateAccessCode(userId: self.getUserId(), password: self.getLoginPassword()) {
  47. result in
  48. if result != "Error"{
  49. UserDefaults.standard.set((result + ":" + RestApiMananger.sharedInstance.getUUID()).toBase64(), forKey: "com.gmeremit.accessCode")
  50. self.fetchRecipientList(queryString: query_string)
  51. }
  52. }
  53. case .logOutUser():
  54. RestApiMananger.sharedInstance.cancelExistingNetworkCalls()
  55. self.anotherLogin.value = true
  56. case .timeOut:
  57. self.trackRecipientTimeOut.value = false
  58. }
  59. }
  60. }
  61. }
  62. /**
  63. empty the array
  64. */
  65. func clearArray() {
  66. recipientArray.removeAll()
  67. }
  68. /**
  69. get the count of array
  70. */
  71. func getRecipientCount() -> Int {
  72. return recipientArray.count
  73. }
  74. /**
  75. - parameter index: position of recipient in an array
  76. - returns: recipient
  77. */
  78. func getFilteredRecipientList(index: Int) -> Recipient {
  79. return recipientArray[index]
  80. }
  81. /**
  82. - parameter index: position of recipient name in an array
  83. - returns: recipient name
  84. */
  85. func getRecipientName(index: Int) -> String {
  86. var recipientName: String?
  87. recipientName = self.recipientArray[index].getFullName()
  88. if let name = recipientName{
  89. return name
  90. }
  91. return "nil"
  92. }
  93. /**
  94. - parameter index: position of recipient phone in an array
  95. - returns: recipient phone
  96. */
  97. func getRecipientPhone(index: Int) -> String {
  98. var phoneNumber: String?
  99. phoneNumber = self.recipientArray[index].mobileNumber
  100. if let number = phoneNumber{
  101. return number
  102. }
  103. return "nil"
  104. }
  105. }