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
9.5 KiB

6 years ago
  1. //
  2. // NotificationViewModel.swift
  3. // GMERemittance
  4. //
  5. // Created by FMI-12 on 3/9/18.
  6. // Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved.
  7. //
  8. import Foundation
  9. class NotificationViewModel: ModelExtension {
  10. let userId = UserDefaults.standard.object(forKey: "com.gmeremit.username") as? String
  11. var notificationFetch: Box<Bool?> = Box(nil)
  12. var notificationPatch: Box<Bool?> = Box(nil)
  13. var notificationConnectionTimeOut: Box<Bool?> = Box(nil)
  14. var notificationArray: [NotificationModel] = [NotificationModel] ()
  15. private var viewedNotification: NotificationModel = NotificationModel()
  16. /**
  17. Api request for notification for user
  18. */
  19. func fetchNotification(){
  20. if !Reachability.isConnectedToNetwork() {
  21. self.internetConnection.value = false
  22. } else {
  23. RestApiMananger.sharedInstance.getNotification(userId: userId!) { result in
  24. switch result {
  25. case let .success(fetchedJSON):
  26. self.notificationArray.removeAll()
  27. guard fetchedJSON.count > 0 else {
  28. self.notificationFetch.value = true
  29. return
  30. }
  31. for i in 0 ... (fetchedJSON.count-1) {
  32. do {
  33. let notification = try JSONDecoder().decode(NotificationModel.self, from: fetchedJSON[i].rawData())
  34. self.notificationArray.append(notification)
  35. } catch {
  36. self.notificationFetch.value = false
  37. }
  38. }
  39. self.notificationFetch.value = true
  40. case let .failure(errorJSON):
  41. self.setErrorMessage(message: errorJSON["message"].stringValue)
  42. self.notificationFetch.value = false
  43. case .updateAccessCode:
  44. RestApiMananger.sharedInstance.updateAccessCode(userId: self.userId!, password: self.getLoginPassword()) {
  45. result in
  46. if result != "Error"{
  47. let uuid = RestApiMananger.sharedInstance.getUUID()
  48. UserDefaults.standard.set((result + ":" + uuid).toBase64(), forKey: "com.gmeremit.accessCode")
  49. self.fetchNotification()
  50. }
  51. }
  52. case .logOutUser():
  53. RestApiMananger.sharedInstance.cancelExistingNetworkCalls()
  54. self.anotherLogin.value = true
  55. case .timeOut:
  56. self.notificationConnectionTimeOut.value = false
  57. }
  58. }
  59. }
  60. }
  61. /**
  62. Notification Pagination
  63. - parameter page: page number
  64. - parameter size: Size of notification list
  65. */
  66. func fetchNotification(page:Int,size:Int){
  67. if !Reachability.isConnectedToNetwork() {
  68. self.internetConnection.value = false
  69. } else {
  70. RestApiMananger.sharedInstance.getNotification(userId: userId!,page: page,size: size) { result in
  71. switch result {
  72. case let .success(fetchedJSON):
  73. self.notificationArray.removeAll()
  74. guard fetchedJSON.count > 0 else {
  75. self.notificationFetch.value = true
  76. return
  77. }
  78. for i in 0 ... (fetchedJSON.count-1) {
  79. do {
  80. let notification = try JSONDecoder().decode(NotificationModel.self, from: fetchedJSON[i].rawData())
  81. self.notificationArray.append(notification)
  82. } catch {
  83. self.notificationFetch.value = false
  84. }
  85. }
  86. self.notificationFetch.value = true
  87. case let .failure(errorJSON):
  88. self.setErrorMessage(message: errorJSON["message"].stringValue)
  89. self.notificationFetch.value = false
  90. case .updateAccessCode:
  91. RestApiMananger.sharedInstance.updateAccessCode(userId: self.userId!, password: self.getLoginPassword()) {
  92. result in
  93. if result != "Error"{
  94. let uuid = RestApiMananger.sharedInstance.getUUID()
  95. UserDefaults.standard.set((result + ":" + uuid).toBase64(), forKey: "com.gmeremit.accessCode")
  96. self.fetchNotification(page:page, size:size)
  97. }
  98. }
  99. case .logOutUser():
  100. RestApiMananger.sharedInstance.cancelExistingNetworkCalls()
  101. self.anotherLogin.value = true
  102. case .timeOut:
  103. self.notificationConnectionTimeOut.value = false
  104. }
  105. }
  106. }
  107. }
  108. /**
  109. Api request for clicked notifications
  110. - parameter notificaitonId: notification id number
  111. - parameter viewed: status of notification
  112. */
  113. func patchNotification(notificaitonId: String, viewed: String){
  114. if !Reachability.isConnectedToNetwork() {
  115. self.internetConnection.value = false
  116. } else {
  117. RestApiMananger.sharedInstance.patchNotification(notificationId: notificaitonId, viewed: viewed){ result in
  118. switch result {
  119. case .success(_):
  120. self.notificationPatch.value = true
  121. case let .failure(errorJSON):
  122. self.setErrorMessage(message: errorJSON["message"].stringValue)
  123. self.notificationPatch.value = false
  124. case .updateAccessCode:
  125. RestApiMananger.sharedInstance.updateAccessCode(userId: self.userId!, password: self.getLoginPassword()) {
  126. result in
  127. if result != "Error"{
  128. let uuid = RestApiMananger.sharedInstance.getUUID()
  129. UserDefaults.standard.set((result + ":" + uuid).toBase64(), forKey: "com.gmeremit.accessCode")
  130. self.patchNotification(notificaitonId: notificaitonId, viewed: viewed)
  131. }
  132. }
  133. case .logOutUser():
  134. RestApiMananger.sharedInstance.cancelExistingNetworkCalls()
  135. self.anotherLogin.value = true
  136. case .timeOut:
  137. return self.notificationConnectionTimeOut.value = false
  138. }
  139. }
  140. }
  141. }
  142. /**
  143. returns: array count
  144. */
  145. func getCount() -> Int {
  146. return notificationArray.count
  147. }
  148. /**
  149. returns: notification array
  150. */
  151. func getNotificationArray() -> [NotificationModel] {
  152. return notificationArray
  153. }
  154. /**
  155. To get notification
  156. - parameter index: position of notification in an array
  157. - returns: notification
  158. */
  159. func getNotification(index: Int) -> NotificationModel {
  160. return notificationArray[index]
  161. }
  162. /**
  163. To get notification id
  164. - parameter index: position of notification Id in an array
  165. - returns: notification id
  166. */
  167. func getNotificationId(index:Int) -> String {
  168. if let getNotificationId = notificationArray[index].notificationId{
  169. return getNotificationId
  170. }
  171. else {
  172. return "N/A"
  173. }
  174. }
  175. /**
  176. To get notification date
  177. - parameter index: position of notification date in an array
  178. - returns: notification date
  179. */
  180. func getDate(index:Int) -> String {
  181. if let getDate = notificationArray[index].createdDate{
  182. return getDate
  183. }
  184. else {
  185. return "N/A"
  186. }
  187. }
  188. /**
  189. To get notification message
  190. - parameter index: position of notification message in an array
  191. - returns: notification message
  192. */
  193. func getNotificationMessage(index:Int) -> String {
  194. if let getNotificationMessage = notificationArray[index].notificationMessage{
  195. return getNotificationMessage
  196. }
  197. else {
  198. return "N/A"
  199. }
  200. }
  201. /**
  202. To get notifier image url
  203. - parameter index: position of notifier image url in an array
  204. - returns: url
  205. */
  206. func getNotifireImageURL(index: Int) -> String {
  207. if let getNotifireImageURL = notificationArray[index].notifierUrl{
  208. return getNotifireImageURL
  209. } else {
  210. return ""
  211. }
  212. }
  213. /**
  214. To get notification code
  215. - parameter index: position of notification code in an array
  216. - returns: notification code
  217. */
  218. func getNotificationCode(index: Int) -> String {
  219. if let getNotificationCode = notificationArray[index].notificationCode{
  220. return getNotificationCode
  221. } else {
  222. return "N/A"
  223. }
  224. }
  225. /**
  226. To get notification
  227. - parameter index: position of notification in an array
  228. - returns: notification
  229. */
  230. func showNotification() -> Bool {
  231. for i in 0..<notificationArray.count {
  232. if !notificationArray[i].viewed{
  233. return true
  234. }
  235. }
  236. return false
  237. }
  238. }