// // AgentViewModel.swift // GMERemittance // // Created by FMI-12 on 2/5/18. // Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved. // import Foundation import SwiftyJSON class AgentViewModel: ModelExtension { var agentLocationAvailable: Box = Box(nil) var agentCountryListAvailable: Box = Box(nil) var agentLocationBool: Box = Box(nil) var agentConnectionTimeOut: Box = Box(nil) let user_id = UserDefaults.standard.object(forKey: "com.gmeremit.username") as? String private var agentArray = [AgentModel]() private var agentCountryList = [AgentCountryList]() private var agentNameArray = [String]() /** Fetch agent location - Parameter countryId: each country is provided with unique id - Parameter city: name of city */ func fetchAgentLocation(countryId: String, city: String) { let countryIdLocal = countryId let cityLocal = city if !Reachability.isConnectedToNetwork() { self.internetConnection.value = false } else { RestApiMananger.sharedInstance.getAgentLocationDetails(countryId: countryId, city: city) { result in switch result { case let .success(agentJSON): self.agentArray.removeAll() self.agentNameArray.removeAll() guard agentJSON.count > 0 else { self.agentLocationAvailable.value = true return } if agentJSON.count > 0 { for i in 0 ... (agentJSON.count-1) { do { let agent = try JSONDecoder().decode(AgentModel.self, from: agentJSON[i].rawData()) self.agentArray.append(agent) } catch let err { self.setErrorMessage(message: err as! String) self.agentLocationAvailable.value = false } } } self.agentArray = self.agentArray.sorted(by: { $0.name.lowercased() < $1.name.lowercased() }) if self.agentArray.count > 0{ for i in 0...(self.agentArray.count - 1) { self.agentNameArray.append(self.agentArray[i].name) } } self.agentLocationAvailable.value = true case let .failure(errorJSON): self.setErrorMessage(message: errorJSON["message"].stringValue) self.agentLocationAvailable.value = false case .updateAccessCode: RestApiMananger.sharedInstance.updateAccessCode(userId: self.user_id!, password: self.getLoginPassword()) { result in if result != "Error"{ let uuid = RestApiMananger.sharedInstance.getUUID() UserDefaults.standard.set((result + ":" + uuid).toBase64(), forKey: "com.gmeremit.accessCode") self.fetchAgentLocation(countryId: countryIdLocal, city: cityLocal) } } case .logOutUser(): RestApiMananger.sharedInstance.cancelExistingNetworkCalls() self.anotherLogin.value = true case .timeOut: self.agentConnectionTimeOut.value = false } } } } /** Fetch list of country */ func fetchListOfCountry(){ if !Reachability.isConnectedToNetwork() { self.internetConnection.value = false }else{ RestApiMananger.sharedInstance.fetchCountryList() { result in switch result { case let .success(countryListJSON): self.agentCountryList.removeAll() guard countryListJSON.count > 0 else { return } self.setCountryListDataInObject(countryList: countryListJSON) case let .failure(errorJSON): self.setErrorMessage(message: errorJSON["message"].stringValue) self.agentLocationAvailable.value = false case .updateAccessCode: RestApiMananger.sharedInstance.updateAccessCode(userId: self.user_id!, password: self.getLoginPassword()) { result in if result != "Error"{ let uuid = RestApiMananger.sharedInstance.getUUID() UserDefaults.standard.set((result + ":" + uuid).toBase64(), forKey: "com.gmeremit.accessCode") self.fetchListOfCountry() } } case .logOutUser(): RestApiMananger.sharedInstance.cancelExistingNetworkCalls() self.anotherLogin.value = true case .timeOut: self.agentConnectionTimeOut.value = false } } } } /** Set country list data in array object - Parameter: json array of country list */ func setCountryListDataInObject(countryList: JSON){ if countryList.count > 0{ for i in 0 ... (countryList.count-1) { do { let country = try JSONDecoder().decode(AgentCountryList.self, from: countryList[i].rawData()) agentCountryList.append(country) } catch let err { self.setErrorMessage(message: err as! String) self.agentLocationAvailable.value = false } } } self.agentCountryListAvailable.value = true } /** Fetch agent location - Parameter countryId: country is provided with a unique id */ func fetchAgentLocation(countryId: String){ let countryIdLocal = countryId if !Reachability.isConnectedToNetwork() { self.internetConnection.value = false } else { RestApiMananger.sharedInstance.fetchAgentLocation(countryId: countryId){ result in switch result{ case let .success(agentLocationJSON): self.agentArray.removeAll() guard agentLocationJSON.count > 0 else { return } self.setAgentLocation(agentLocation: agentLocationJSON) case let .failure(errorJSON): self.setErrorMessage(message: errorJSON["message"].stringValue) self.agentLocationBool.value = false case .updateAccessCode: RestApiMananger.sharedInstance.updateAccessCode(userId: self.user_id!, password: self.getLoginPassword()) { result in if result != "Error"{ let uuid = RestApiMananger.sharedInstance.getUUID() UserDefaults.standard.set((result + ":" + uuid).toBase64(), forKey: "com.gmeremit.accessCode") self.fetchAgentLocation(countryId: countryIdLocal) } } case .logOutUser(): RestApiMananger.sharedInstance.cancelExistingNetworkCalls() self.anotherLogin.value = true case .timeOut: self.agentConnectionTimeOut.value = false } } } } /** Set json array in model object - Parameter agentLocation: json array of agent location data */ func setAgentLocation(agentLocation: JSON){ if agentLocation.count > 0 { for i in 0 ... (agentLocation.count-1) { do { let agentLocation = try JSONDecoder().decode(AgentModel.self, from: agentLocation[i].rawData()) agentArray.append(agentLocation) } catch let err { self.setErrorMessage(message: err as! String) self.agentLocationBool.value = false } } } self.agentLocationBool.value = true } /** - returns: array of agent model */ func getAgentLocation() -> [AgentModel]{ return agentArray } /** - returns: array of agent country list */ func getCountryList() -> [AgentCountryList]{ return agentCountryList } /** - returns: array of agent model */ func getAgentArray() -> [AgentModel] { return agentArray } /** - returns: array of agent name */ func getAgentNamesArray() -> [String] { return agentNameArray } /** - returns: agent array total count */ func getAgentCount() -> Int { return agentArray.count } /** - Parameter index: position of name in agent array - returns: name */ func getAgentName(index: Int) -> String { if let name = agentArray[index].name { return name } else { return "N/A" } } /** - Parameter index: position of address in agent array - returns: address */ func getAgentAddress(index: Int) -> String { if let address = agentArray[index].address { return address } return "" } /** - Parameter index: position of phone in agent array - returns: phone */ func getAgentPhone(index: Int) -> String { if let phone = agentArray[index].phone { return phone } else { return "N/A" } } /** - Parameter index: position of latitude in agent array - returns: latitude */ func getAgentLatitude(index:Int) -> String { if let latitude = agentArray[index].latitude { return latitude } else { return "0" } } /** - Parameter index: position of longitude in agent array - returns: longitude */ func getAgentLongitude(index:Int) -> String { if let longitude = agentArray[index].longitude { return longitude } else { return "0" } } }