// // TrackTransferViewModel.swift // GMERemittance // // Created by Fm-user on 2/5/18. // Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved. // import Foundation import Alamofire class TrackRecipientViewModel: ModelExtension { var recipientListObtained: Box = Box(nil) var recipientArray = [Recipient]() var recipientName: String? var trackRecipientTimeOut: Box = Box(nil) var request: URLRequest! /** Api request for recipient list - parameter String: Reciver name */ func fetchRecipientList(queryString: String) { if !Reachability.isConnectedToNetwork() { self.internetConnection.value = false } else { let query_string = queryString RestApiMananger.sharedInstance.getFilteredRecipientList(queryString: queryString, userId: getUserId()) { result in switch result { case let .success(fetchedJSON): self.recipientArray.removeAll() guard fetchedJSON.count > 0 else { self.recipientListObtained.value = true return } for i in 0 ... (fetchedJSON.count-1) { do { let recipient = try JSONDecoder().decode(Recipient.self, from: fetchedJSON[i].rawData()) self.recipientArray.append(recipient) } catch { self.recipientListObtained.value = false } } self.recipientListObtained.value = true case let .failure(errorJSON): self.setErrorMessage(message: errorJSON["message"].stringValue) self.recipientListObtained.value = false case .updateAccessCode: RestApiMananger.sharedInstance.updateAccessCode(userId: self.getUserId(), password: self.getLoginPassword()) { result in if result != "Error"{ UserDefaults.standard.set((result + ":" + RestApiMananger.sharedInstance.getUUID()).toBase64(), forKey: "com.gmeremit.accessCode") self.fetchRecipientList(queryString: query_string) } } case .logOutUser(): RestApiMananger.sharedInstance.cancelExistingNetworkCalls() self.anotherLogin.value = true case .timeOut: self.trackRecipientTimeOut.value = false } } } } /** empty the array */ func clearArray() { recipientArray.removeAll() } /** get the count of array */ func getRecipientCount() -> Int { return recipientArray.count } /** - parameter index: position of recipient in an array - returns: recipient */ func getFilteredRecipientList(index: Int) -> Recipient { return recipientArray[index] } /** - parameter index: position of recipient name in an array - returns: recipient name */ func getRecipientName(index: Int) -> String { var recipientName: String? recipientName = self.recipientArray[index].getFullName() if let name = recipientName{ return name } return "nil" } /** - parameter index: position of recipient phone in an array - returns: recipient phone */ func getRecipientPhone(index: Int) -> String { var phoneNumber: String? phoneNumber = self.recipientArray[index].mobileNumber if let number = phoneNumber{ return number } return "nil" } }