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.

305 lines
11 KiB

6 years ago
  1. //
  2. // AgentViewModel.swift
  3. // GMERemittance
  4. //
  5. // Created by FMI-12 on 2/5/18.
  6. // Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import Foundation
  9. import SwiftyJSON
  10. class AgentViewModel: ModelExtension {
  11. var agentLocationAvailable: Box<Bool?> = Box(nil)
  12. var agentCountryListAvailable: Box<Bool?> = Box(nil)
  13. var agentLocationBool: Box<Bool?> = Box(nil)
  14. var agentConnectionTimeOut: Box<Bool?> = Box(nil)
  15. let user_id = UserDefaults.standard.object(forKey: "com.gmeremit.username") as? String
  16. private var agentArray = [AgentModel]()
  17. private var agentCountryList = [AgentCountryList]()
  18. private var agentNameArray = [String]()
  19. /**
  20. Fetch agent location
  21. - Parameter countryId: each country is provided with unique id
  22. - Parameter city: name of city
  23. */
  24. func fetchAgentLocation(countryId: String, city: String) {
  25. let countryIdLocal = countryId
  26. let cityLocal = city
  27. if !Reachability.isConnectedToNetwork() {
  28. self.internetConnection.value = false
  29. } else {
  30. RestApiMananger.sharedInstance.getAgentLocationDetails(countryId: countryId, city: city) { result in
  31. switch result {
  32. case let .success(agentJSON):
  33. self.agentArray.removeAll()
  34. self.agentNameArray.removeAll()
  35. guard agentJSON.count > 0 else {
  36. self.agentLocationAvailable.value = true
  37. return
  38. }
  39. if agentJSON.count > 0 {
  40. for i in 0 ... (agentJSON.count-1) {
  41. do {
  42. let agent = try JSONDecoder().decode(AgentModel.self, from: agentJSON[i].rawData())
  43. self.agentArray.append(agent)
  44. } catch let err {
  45. self.setErrorMessage(message: err as! String)
  46. self.agentLocationAvailable.value = false
  47. }
  48. }
  49. }
  50. self.agentArray = self.agentArray.sorted(by: { $0.name.lowercased() < $1.name.lowercased() })
  51. if self.agentArray.count > 0{
  52. for i in 0...(self.agentArray.count - 1) {
  53. self.agentNameArray.append(self.agentArray[i].name)
  54. }
  55. }
  56. self.agentLocationAvailable.value = true
  57. case let .failure(errorJSON):
  58. self.setErrorMessage(message: errorJSON["message"].stringValue)
  59. self.agentLocationAvailable.value = false
  60. case .updateAccessCode:
  61. RestApiMananger.sharedInstance.updateAccessCode(userId: self.user_id!, password: self.getLoginPassword()) {
  62. result in
  63. if result != "Error"{
  64. let uuid = RestApiMananger.sharedInstance.getUUID()
  65. UserDefaults.standard.set((result + ":" + uuid).toBase64(), forKey: "com.gmeremit.accessCode")
  66. self.fetchAgentLocation(countryId: countryIdLocal, city: cityLocal)
  67. }
  68. }
  69. case .logOutUser():
  70. RestApiMananger.sharedInstance.cancelExistingNetworkCalls()
  71. self.anotherLogin.value = true
  72. case .timeOut:
  73. self.agentConnectionTimeOut.value = false
  74. }
  75. }
  76. }
  77. }
  78. /**
  79. Fetch list of country
  80. */
  81. func fetchListOfCountry(){
  82. if !Reachability.isConnectedToNetwork() {
  83. self.internetConnection.value = false
  84. }else{
  85. RestApiMananger.sharedInstance.fetchCountryList() { result in
  86. switch result {
  87. case let .success(countryListJSON):
  88. self.agentCountryList.removeAll()
  89. guard countryListJSON.count > 0 else {
  90. return
  91. }
  92. self.setCountryListDataInObject(countryList: countryListJSON)
  93. case let .failure(errorJSON):
  94. self.setErrorMessage(message: errorJSON["message"].stringValue)
  95. self.agentLocationAvailable.value = false
  96. case .updateAccessCode: RestApiMananger.sharedInstance.updateAccessCode(userId: self.user_id!, password: self.getLoginPassword()) {
  97. result in
  98. if result != "Error"{
  99. let uuid = RestApiMananger.sharedInstance.getUUID()
  100. UserDefaults.standard.set((result + ":" + uuid).toBase64(), forKey: "com.gmeremit.accessCode")
  101. self.fetchListOfCountry()
  102. }
  103. }
  104. case .logOutUser():
  105. RestApiMananger.sharedInstance.cancelExistingNetworkCalls()
  106. self.anotherLogin.value = true
  107. case .timeOut:
  108. self.agentConnectionTimeOut.value = false
  109. }
  110. }
  111. }
  112. }
  113. /**
  114. Set country list data in array object
  115. - Parameter: json array of country list
  116. */
  117. func setCountryListDataInObject(countryList: JSON){
  118. if countryList.count > 0{
  119. for i in 0 ... (countryList.count-1) {
  120. do {
  121. let country = try JSONDecoder().decode(AgentCountryList.self, from: countryList[i].rawData())
  122. agentCountryList.append(country)
  123. } catch let err {
  124. self.setErrorMessage(message: err as! String)
  125. self.agentLocationAvailable.value = false
  126. }
  127. }
  128. }
  129. self.agentCountryListAvailable.value = true
  130. }
  131. /**
  132. Fetch agent location
  133. - Parameter countryId: country is provided with a unique id
  134. */
  135. func fetchAgentLocation(countryId: String){
  136. let countryIdLocal = countryId
  137. if !Reachability.isConnectedToNetwork() {
  138. self.internetConnection.value = false
  139. } else {
  140. RestApiMananger.sharedInstance.fetchAgentLocation(countryId: countryId){
  141. result in
  142. switch result{
  143. case let .success(agentLocationJSON):
  144. self.agentArray.removeAll()
  145. guard agentLocationJSON.count > 0 else {
  146. return
  147. }
  148. self.setAgentLocation(agentLocation: agentLocationJSON)
  149. case let .failure(errorJSON):
  150. self.setErrorMessage(message: errorJSON["message"].stringValue)
  151. self.agentLocationBool.value = false
  152. case .updateAccessCode:
  153. RestApiMananger.sharedInstance.updateAccessCode(userId: self.user_id!, password: self.getLoginPassword()) {
  154. result in
  155. if result != "Error"{
  156. let uuid = RestApiMananger.sharedInstance.getUUID()
  157. UserDefaults.standard.set((result + ":" + uuid).toBase64(), forKey: "com.gmeremit.accessCode")
  158. self.fetchAgentLocation(countryId: countryIdLocal)
  159. }
  160. }
  161. case .logOutUser():
  162. RestApiMananger.sharedInstance.cancelExistingNetworkCalls()
  163. self.anotherLogin.value = true
  164. case .timeOut:
  165. self.agentConnectionTimeOut.value = false
  166. }
  167. }
  168. }
  169. }
  170. /**
  171. Set json array in model object
  172. - Parameter agentLocation: json array of agent location data
  173. */
  174. func setAgentLocation(agentLocation: JSON){
  175. if agentLocation.count > 0 {
  176. for i in 0 ... (agentLocation.count-1) {
  177. do {
  178. let agentLocation = try JSONDecoder().decode(AgentModel.self, from: agentLocation[i].rawData())
  179. agentArray.append(agentLocation)
  180. } catch let err {
  181. self.setErrorMessage(message: err as! String)
  182. self.agentLocationBool.value = false
  183. }
  184. }
  185. }
  186. self.agentLocationBool.value = true
  187. }
  188. /**
  189. - returns: array of agent model
  190. */
  191. func getAgentLocation() -> [AgentModel]{
  192. return agentArray
  193. }
  194. /**
  195. - returns: array of agent country list
  196. */
  197. func getCountryList() -> [AgentCountryList]{
  198. return agentCountryList
  199. }
  200. /**
  201. - returns: array of agent model
  202. */
  203. func getAgentArray() -> [AgentModel] {
  204. return agentArray
  205. }
  206. /**
  207. - returns: array of agent name
  208. */
  209. func getAgentNamesArray() -> [String] {
  210. return agentNameArray
  211. }
  212. /**
  213. - returns: agent array total count
  214. */
  215. func getAgentCount() -> Int {
  216. return agentArray.count
  217. }
  218. /**
  219. - Parameter index: position of name in agent array
  220. - returns: name
  221. */
  222. func getAgentName(index: Int) -> String {
  223. if let name = agentArray[index].name {
  224. return name
  225. } else {
  226. return "N/A"
  227. }
  228. }
  229. /**
  230. - Parameter index: position of address in agent array
  231. - returns: address
  232. */
  233. func getAgentAddress(index: Int) -> String {
  234. if let address = agentArray[index].address {
  235. return address
  236. }
  237. return ""
  238. }
  239. /**
  240. - Parameter index: position of phone in agent array
  241. - returns: phone
  242. */
  243. func getAgentPhone(index: Int) -> String {
  244. if let phone = agentArray[index].phone {
  245. return phone
  246. }
  247. else {
  248. return "N/A"
  249. }
  250. }
  251. /**
  252. - Parameter index: position of latitude in agent array
  253. - returns: latitude
  254. */
  255. func getAgentLatitude(index:Int) -> String {
  256. if let latitude = agentArray[index].latitude {
  257. return latitude
  258. } else {
  259. return "0"
  260. }
  261. }
  262. /**
  263. - Parameter index: position of longitude in agent array
  264. - returns: longitude
  265. */
  266. func getAgentLongitude(index:Int) -> String {
  267. if let longitude = agentArray[index].longitude {
  268. return longitude
  269. } else {
  270. return "0"
  271. }
  272. }
  273. }