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.

269 lines
8.4 KiB

6 years ago
  1. //
  2. // InviteViewModel.swift
  3. // GMERemittance
  4. //
  5. // Created by FMI-12 on 3/1/18.
  6. // Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import Foundation
  9. class InviteViewModel: ModelExtension {
  10. private var name: String!
  11. private var mobileNumber: String!
  12. private var email: String?
  13. var inviteeCreated: Box<Bool?> = Box(nil)
  14. var inviteeFetch: Box<Bool?> = Box(nil)
  15. var inviteeConnectionTimeOut: Box<Bool?> = Box(nil)
  16. private var newInvitee: InviteeModel = InviteeModel()
  17. private var inviteeArray: [InviteeModel] = [InviteeModel] ()
  18. /**
  19. Create invitee
  20. - parameter name: name of the invitee
  21. - parameter mobileNumber: mobile number of the invitee
  22. - parameter email: email of the invitee
  23. */
  24. func createInvitee(name: String, mobileNumber: String, email: String){
  25. if !Reachability.isConnectedToNetwork() {
  26. self.internetConnection.value = false
  27. } else {
  28. newInvitee.name = name
  29. newInvitee.mobileNumber = mobileNumber
  30. newInvitee.email = email
  31. newInvitee.userId = getUserId()
  32. var encodedInviteeDictionary: [String: String]!
  33. do {
  34. let encodedInvitee = try JSONEncoder().encode(newInvitee)
  35. encodedInviteeDictionary = try JSONSerialization.jsonObject(with: encodedInvitee, options: .allowFragments) as? [String: String]
  36. } catch {
  37. }
  38. RestApiMananger.sharedInstance.createInvitee(param: encodedInviteeDictionary){
  39. result in
  40. switch result {
  41. case let .success(createdJSON):
  42. do {
  43. self.newInvitee = try JSONDecoder().decode(InviteeModel.self, from: createdJSON.rawData())
  44. self.inviteeCreated.value = true
  45. } catch {
  46. self.inviteeCreated.value = false
  47. }
  48. case let .failure(errorJSON):
  49. self.setErrorMessage(message: errorJSON["message"].stringValue)
  50. self.inviteeCreated.value = false
  51. case .updateAccessCode:
  52. RestApiMananger.sharedInstance.updateAccessCode(userId: self.getUserId(), password: self.getLoginPassword()) {
  53. result in
  54. if result != "Error"{
  55. let uuid = "8da2e516-df9e-4aaa-b629-725150c4d8cc"
  56. UserDefaults.standard.set((result + ":" + uuid).toBase64(), forKey: "com.gmeremit.accessCode")
  57. self.createInvitee(name: name, mobileNumber: mobileNumber, email: email)
  58. }
  59. }
  60. case .logOutUser():
  61. RestApiMananger.sharedInstance.cancelExistingNetworkCalls()
  62. self.anotherLogin.value = true
  63. case .timeOut:
  64. self.inviteeConnectionTimeOut.value = false
  65. }
  66. }
  67. }
  68. }
  69. /**
  70. Get invitee data
  71. */
  72. func fetchInvitee(){
  73. if !Reachability.isConnectedToNetwork() {
  74. self.internetConnection.value = false
  75. } else {
  76. RestApiMananger.sharedInstance.getInvitee(userId: getUserId()) { result in
  77. switch result {
  78. case let .success(fetchedJSON):
  79. self.inviteeArray.removeAll()
  80. guard fetchedJSON.count > 0 else {
  81. self.inviteeFetch.value = true
  82. return
  83. }
  84. if fetchedJSON.count > 0 {
  85. for i in 0 ... (fetchedJSON.count-1) {
  86. do {
  87. let invitee = try JSONDecoder().decode(InviteeModel.self, from: fetchedJSON[i].rawData())
  88. self.inviteeArray.append(invitee)
  89. } catch {
  90. self.inviteeFetch.value = false
  91. }
  92. }
  93. }
  94. self.inviteeFetch.value = true
  95. case let .failure(errorJSON):
  96. self.setErrorMessage(message: errorJSON["message"].stringValue)
  97. self.inviteeFetch.value = false
  98. case .updateAccessCode:
  99. RestApiMananger.sharedInstance.updateAccessCode(userId: self.getUserId(), password: self.getLoginPassword()) {
  100. result in
  101. if result != "Error"{
  102. UserDefaults.standard.set((result + ":" + RestApiMananger.sharedInstance.getUUID()).toBase64(), forKey: "com.gmeremit.accessCode")
  103. self.fetchInvitee()
  104. }
  105. }
  106. case .logOutUser():
  107. RestApiMananger.sharedInstance.cancelExistingNetworkCalls()
  108. self.anotherLogin.value = true
  109. case .timeOut:
  110. self.inviteeConnectionTimeOut.value = false
  111. }
  112. }
  113. }
  114. }
  115. /**
  116. Create invitee
  117. - returns: object of Invitee Model
  118. */
  119. func getCreatedInvitee() -> InviteeModel {
  120. return newInvitee
  121. }
  122. /**
  123. - returns: total size of inviteeArray
  124. */
  125. func getCount() -> Int {
  126. return inviteeArray.count
  127. }
  128. /**
  129. - returns: array of InviteeModel
  130. */
  131. func getInviteeArray() -> [InviteeModel] {
  132. return inviteeArray
  133. }
  134. /**
  135. - parameter index: position of object
  136. - returns: position object of InviteeModel
  137. */
  138. func getInvitee(index: Int) -> InviteeModel {
  139. return inviteeArray[index]
  140. }
  141. /**
  142. - parameter index: position of referral id in an array
  143. - returns: referral id
  144. */
  145. func getReferralId(index:Int) -> String {
  146. if let getReferralId = inviteeArray[index].referralId{
  147. return getReferralId
  148. }
  149. else {
  150. return "N/A"
  151. }
  152. }
  153. /**
  154. - parameter index: position of name in an array
  155. - returns: name
  156. */
  157. func getName(index:Int) -> String {
  158. if let getName = inviteeArray[index].name{
  159. return getName
  160. }
  161. else {
  162. return "N/A"
  163. }
  164. }
  165. /**
  166. - parameter index: position of mobile number in an array
  167. - returns: referral id
  168. */
  169. func getMobileNumber(index:Int) -> String {
  170. if let getMobileNumber = inviteeArray[index].mobileNumber{
  171. return getMobileNumber
  172. }
  173. else {
  174. return "N/A"
  175. }
  176. }
  177. /**
  178. - parameter index: position of email in an array
  179. - returns: email
  180. */
  181. func getEmail(index:Int) -> String {
  182. if let getEmail = inviteeArray[index].email{
  183. return getEmail
  184. }
  185. else {
  186. return "N/A"
  187. }
  188. }
  189. /**
  190. - parameter index: position of userId in an array
  191. - returns: userId
  192. */
  193. func getUserId(index:Int) -> String {
  194. if let getUserId = inviteeArray[index].userId{
  195. return getUserId
  196. }
  197. else {
  198. return "N/A"
  199. }
  200. }
  201. /**
  202. - parameter index: position of status in an array
  203. - returns: status
  204. */
  205. func getStatus(index:Int) -> String {
  206. if let getStatus = inviteeArray[index].status{
  207. return getStatus
  208. }
  209. else {
  210. return "N/A"
  211. }
  212. }
  213. }
  214. extension InviteViewModel {
  215. /**
  216. - parameter arrayofInfo: array of textfield input checking null
  217. - returns: if blank return false else true
  218. */
  219. func allFieldsFilled(arrayofInfo: [String]) -> Bool {
  220. if arrayofInfo.count > 0{
  221. for info in arrayofInfo {
  222. if info.isBlank {
  223. return false
  224. }
  225. }
  226. }
  227. return true
  228. }
  229. /**
  230. - parameter email: Invitee email
  231. - returns: true if valid email
  232. */
  233. func isValidEmail(email:String) -> Bool {
  234. let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}"
  235. return NSPredicate(format:"SELF MATCHES %@", emailRegEx).evaluate(with: email)
  236. }
  237. }