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.

266 lines
8.8 KiB

6 years ago
  1. //
  2. // ProfileViewModel.swift
  3. // GMERemittance
  4. //
  5. // Created by FMI-12 on 1/29/18.
  6. // Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import Foundation
  9. class ProfileViewModel: ModelExtension {
  10. var userDataAvailable:Box<Bool?> = Box(nil)
  11. var imageSubmitted:Box<Bool?> = Box(nil)
  12. var profileConnectionTimeOut: Box<Bool?> = Box(nil)
  13. var availableBalance: String?
  14. var rewardPoint: String?
  15. var walletNumber: String?
  16. private var profileInfo: ProfileModel!
  17. private var user_id: String!
  18. private var userImageDocUrl: String?
  19. /**
  20. Api request for user Information
  21. - parameter String: UserId
  22. */
  23. func fetchUserInfo(userId: String?) {
  24. if userId == nil {
  25. user_id = UserDefaults.standard.object(forKey: "com.gmeremit.username") as? String
  26. } else {
  27. user_id = userId!
  28. }
  29. if !Reachability.isConnectedToNetwork() {
  30. self.internetConnection.value = false
  31. } else {
  32. RestApiMananger.sharedInstance.getUserDetails(userId: self.user_id) { result in
  33. switch result {
  34. case let .success(userJSON):
  35. do {
  36. self.profileInfo = try JSONDecoder().decode(ProfileModel.self, from: userJSON.rawData())
  37. if userId == nil {
  38. self.availableBalance = userJSON["availableBalance"].stringValue
  39. self.rewardPoint = userJSON["rewardPoint"].stringValue
  40. self.walletNumber = userJSON["walletNumber"].stringValue
  41. }
  42. self.userDataAvailable.value = true
  43. } catch {
  44. self.userDataAvailable.value = false
  45. }
  46. case let .failure(errorJSON):
  47. self.setErrorMessage(message: "Update Failed:" + errorJSON["message"].stringValue)
  48. self.userDataAvailable.value = false
  49. case .updateAccessCode:
  50. RestApiMananger.sharedInstance.updateAccessCode(userId: self.user_id!, password: self.getLoginPassword()) {
  51. result in
  52. if result != "Error"{
  53. let uuid = RestApiMananger.sharedInstance.getUUID()
  54. UserDefaults.standard.set((result + ":" + uuid).toBase64(), forKey: "com.gmeremit.accessCode")
  55. self.fetchUserInfo(userId: userId)
  56. }
  57. }
  58. case .logOutUser():
  59. RestApiMananger.sharedInstance.cancelExistingNetworkCalls()
  60. self.anotherLogin.value = true
  61. case .timeOut:
  62. self.profileConnectionTimeOut.value = false
  63. }
  64. }
  65. }
  66. }
  67. /**
  68. Submit the Image provided
  69. - parameter docType: type of document
  70. - parameter imageBase64 data
  71. */
  72. func provideImageForSubmission(docType: String, imageBase64: String) {
  73. if !Reachability.isConnectedToNetwork() {
  74. self.internetConnection.value = false
  75. } else {
  76. let docParam = ["documentType": docType, "file": imageBase64]
  77. RestApiMananger.sharedInstance.submitDocument(param: docParam, userId: self.user_id!) { result in
  78. switch result {
  79. case let .success(imageJSON):
  80. if let docUrl = imageJSON["documentUrl"].rawString() {
  81. self.updateUserInfo(param: ["dpUrl": docUrl])
  82. } else {
  83. self.setErrorMessage(message:"No image URL generated")
  84. self.imageSubmitted.value = false
  85. }
  86. case let .failure(errorJSON):
  87. self.setErrorMessage(message: errorJSON["message"].stringValue)
  88. self.imageSubmitted.value = false
  89. case .updateAccessCode:
  90. RestApiMananger.sharedInstance.updateAccessCode(userId: self.user_id!, password: self.getLoginPassword()) {
  91. result in
  92. if result != "Error"{
  93. let uuid = RestApiMananger.sharedInstance.getUUID()
  94. UserDefaults.standard.set((result + ":" + uuid).toBase64(), forKey: "com.gmeremit.accessCode")
  95. self.provideImageForSubmission(docType: docType, imageBase64: imageBase64)
  96. }
  97. }
  98. case .logOutUser():
  99. RestApiMananger.sharedInstance.cancelExistingNetworkCalls()
  100. self.anotherLogin.value = true
  101. case .timeOut:
  102. self.profileConnectionTimeOut.value = false
  103. }
  104. }
  105. }
  106. }
  107. /**
  108. Api request to update user information
  109. - parameter param: User information is in Dictionary
  110. */
  111. func updateUserInfo(param: [String: String]) {
  112. if !Reachability.isConnectedToNetwork() {
  113. self.internetConnection.value = false
  114. } else {
  115. RestApiMananger.sharedInstance.updateUserDetail (userId: user_id, param: param) { result in
  116. switch result {
  117. case let .success(fetchedJSON):
  118. UserDefaults.standard.set(fetchedJSON["dpUrl"].stringValue, forKey: "com.gmeremit.dpUrl")
  119. self.imageSubmitted.value = true
  120. case let .failure(errorJSON):
  121. self.setErrorMessage(message: errorJSON["message"].stringValue)
  122. self.imageSubmitted.value = false
  123. case .updateAccessCode:
  124. RestApiMananger.sharedInstance.updateAccessCode(userId: self.user_id!, password: self.getLoginPassword()) {
  125. result in
  126. if result != "Error"{
  127. let uuid = RestApiMananger.sharedInstance.getUUID()
  128. UserDefaults.standard.set((result + ":" + uuid).toBase64(), forKey: "com.gmeremit.accessCode")
  129. self.updateUserInfo(param: param)
  130. }
  131. }
  132. case .logOutUser():
  133. RestApiMananger.sharedInstance.cancelExistingNetworkCalls()
  134. self.anotherLogin.value = true
  135. case .timeOut:
  136. self.profileConnectionTimeOut.value = false
  137. }
  138. }
  139. }
  140. }
  141. /**
  142. to get proile info
  143. - returns: profile Info
  144. */
  145. func getProfileModel() -> ProfileModel {
  146. return profileInfo
  147. }
  148. /**
  149. To get full name
  150. - returns: Full name
  151. */
  152. func getFullName() -> String {
  153. if profileInfo.middleName == nil {
  154. return profileInfo.firstName + " " + profileInfo.lastName
  155. } else {
  156. return profileInfo.firstName + " " + profileInfo.middleName! + " " + profileInfo.lastName
  157. }
  158. }
  159. /**
  160. To get user balance
  161. - returns: Amount
  162. */
  163. func getAvailableBalance() -> String {
  164. if let balance = availableBalance {
  165. return balance
  166. }
  167. return "N/A"
  168. }
  169. /**
  170. To get user email
  171. - returns: email
  172. */
  173. func getEmail() -> String {
  174. if let email = profileInfo.email {
  175. return email
  176. }
  177. return "Email: N/A"
  178. }
  179. /**
  180. To get user Phone nos.
  181. - returns: Phone nos.
  182. */
  183. func getPhone() -> String {
  184. if let phone = profileInfo.mobileNumber {
  185. return phone
  186. }
  187. return "Phone: N/A"
  188. }
  189. /**
  190. To get user wallet Nos.
  191. - returns: wallet Nos.
  192. */
  193. func getWalletNumber() -> String {
  194. if let walletNum = walletNumber {
  195. return walletNum
  196. }
  197. return "Wallet Number: N/A"
  198. }
  199. /**
  200. To get user bank name
  201. - returns: bank name
  202. */
  203. func getBankName() -> String {
  204. if let bankName = profileInfo.primaryBankName {
  205. return bankName
  206. }
  207. return "Primary Bank: N/A"
  208. }
  209. //Added to determine if the user has filled kyc form
  210. func hasFilledKYC() -> Bool {
  211. if profileInfo.kyc {
  212. return true
  213. }
  214. return false
  215. }
  216. /// To deteemine verified account
  217. func isVerified() -> Bool {
  218. if profileInfo.verified! {
  219. return true
  220. }
  221. return false
  222. }
  223. /**
  224. To get user reward point
  225. - returns: points
  226. */
  227. func getRewardPoint() -> String {
  228. if let points = rewardPoint {
  229. return points
  230. }
  231. return "N/A"
  232. }
  233. }