// // NotificationViewModel.swift // GMERemittance // // Created by FMI-12 on 3/9/18. // Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved. // import Foundation class NotificationViewModel: ModelExtension { let userId = UserDefaults.standard.object(forKey: "com.gmeremit.username") as? String var notificationFetch: Box = Box(nil) var notificationPatch: Box = Box(nil) var notificationConnectionTimeOut: Box = Box(nil) var notificationArray: [NotificationModel] = [NotificationModel] () private var viewedNotification: NotificationModel = NotificationModel() /** Api request for notification for user */ func fetchNotification(){ if !Reachability.isConnectedToNetwork() { self.internetConnection.value = false } else { RestApiMananger.sharedInstance.getNotification(userId: userId!) { result in switch result { case let .success(fetchedJSON): self.notificationArray.removeAll() guard fetchedJSON.count > 0 else { self.notificationFetch.value = true return } for i in 0 ... (fetchedJSON.count-1) { do { let notification = try JSONDecoder().decode(NotificationModel.self, from: fetchedJSON[i].rawData()) self.notificationArray.append(notification) } catch { self.notificationFetch.value = false } } self.notificationFetch.value = true case let .failure(errorJSON): self.setErrorMessage(message: errorJSON["message"].stringValue) self.notificationFetch.value = false case .updateAccessCode: RestApiMananger.sharedInstance.updateAccessCode(userId: self.userId!, password: self.getLoginPassword()) { result in if result != "Error"{ let uuid = RestApiMananger.sharedInstance.getUUID() UserDefaults.standard.set((result + ":" + uuid).toBase64(), forKey: "com.gmeremit.accessCode") self.fetchNotification() } } case .logOutUser(): RestApiMananger.sharedInstance.cancelExistingNetworkCalls() self.anotherLogin.value = true case .timeOut: self.notificationConnectionTimeOut.value = false } } } } /** Notification Pagination - parameter page: page number - parameter size: Size of notification list */ func fetchNotification(page:Int,size:Int){ if !Reachability.isConnectedToNetwork() { self.internetConnection.value = false } else { RestApiMananger.sharedInstance.getNotification(userId: userId!,page: page,size: size) { result in switch result { case let .success(fetchedJSON): self.notificationArray.removeAll() guard fetchedJSON.count > 0 else { self.notificationFetch.value = true return } for i in 0 ... (fetchedJSON.count-1) { do { let notification = try JSONDecoder().decode(NotificationModel.self, from: fetchedJSON[i].rawData()) self.notificationArray.append(notification) } catch { self.notificationFetch.value = false } } self.notificationFetch.value = true case let .failure(errorJSON): self.setErrorMessage(message: errorJSON["message"].stringValue) self.notificationFetch.value = false case .updateAccessCode: RestApiMananger.sharedInstance.updateAccessCode(userId: self.userId!, password: self.getLoginPassword()) { result in if result != "Error"{ let uuid = RestApiMananger.sharedInstance.getUUID() UserDefaults.standard.set((result + ":" + uuid).toBase64(), forKey: "com.gmeremit.accessCode") self.fetchNotification(page:page, size:size) } } case .logOutUser(): RestApiMananger.sharedInstance.cancelExistingNetworkCalls() self.anotherLogin.value = true case .timeOut: self.notificationConnectionTimeOut.value = false } } } } /** Api request for clicked notifications - parameter notificaitonId: notification id number - parameter viewed: status of notification */ func patchNotification(notificaitonId: String, viewed: String){ if !Reachability.isConnectedToNetwork() { self.internetConnection.value = false } else { RestApiMananger.sharedInstance.patchNotification(notificationId: notificaitonId, viewed: viewed){ result in switch result { case .success(_): self.notificationPatch.value = true case let .failure(errorJSON): self.setErrorMessage(message: errorJSON["message"].stringValue) self.notificationPatch.value = false case .updateAccessCode: RestApiMananger.sharedInstance.updateAccessCode(userId: self.userId!, password: self.getLoginPassword()) { result in if result != "Error"{ let uuid = RestApiMananger.sharedInstance.getUUID() UserDefaults.standard.set((result + ":" + uuid).toBase64(), forKey: "com.gmeremit.accessCode") self.patchNotification(notificaitonId: notificaitonId, viewed: viewed) } } case .logOutUser(): RestApiMananger.sharedInstance.cancelExistingNetworkCalls() self.anotherLogin.value = true case .timeOut: return self.notificationConnectionTimeOut.value = false } } } } /** returns: array count */ func getCount() -> Int { return notificationArray.count } /** returns: notification array */ func getNotificationArray() -> [NotificationModel] { return notificationArray } /** To get notification - parameter index: position of notification in an array - returns: notification */ func getNotification(index: Int) -> NotificationModel { return notificationArray[index] } /** To get notification id - parameter index: position of notification Id in an array - returns: notification id */ func getNotificationId(index:Int) -> String { if let getNotificationId = notificationArray[index].notificationId{ return getNotificationId } else { return "N/A" } } /** To get notification date - parameter index: position of notification date in an array - returns: notification date */ func getDate(index:Int) -> String { if let getDate = notificationArray[index].createdDate{ return getDate } else { return "N/A" } } /** To get notification message - parameter index: position of notification message in an array - returns: notification message */ func getNotificationMessage(index:Int) -> String { if let getNotificationMessage = notificationArray[index].notificationMessage{ return getNotificationMessage } else { return "N/A" } } /** To get notifier image url - parameter index: position of notifier image url in an array - returns: url */ func getNotifireImageURL(index: Int) -> String { if let getNotifireImageURL = notificationArray[index].notifierUrl{ return getNotifireImageURL } else { return "" } } /** To get notification code - parameter index: position of notification code in an array - returns: notification code */ func getNotificationCode(index: Int) -> String { if let getNotificationCode = notificationArray[index].notificationCode{ return getNotificationCode } else { return "N/A" } } /** To get notification - parameter index: position of notification in an array - returns: notification */ func showNotification() -> Bool { for i in 0..