// // InviteViewModel.swift // GMERemittance // // Created by FMI-12 on 3/1/18. // Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved. // import Foundation class InviteViewModel: ModelExtension { private var name: String! private var mobileNumber: String! private var email: String? var inviteeCreated: Box = Box(nil) var inviteeFetch: Box = Box(nil) var inviteeConnectionTimeOut: Box = Box(nil) private var newInvitee: InviteeModel = InviteeModel() private var inviteeArray: [InviteeModel] = [InviteeModel] () /** Create invitee - parameter name: name of the invitee - parameter mobileNumber: mobile number of the invitee - parameter email: email of the invitee */ func createInvitee(name: String, mobileNumber: String, email: String){ if !Reachability.isConnectedToNetwork() { self.internetConnection.value = false } else { newInvitee.name = name newInvitee.mobileNumber = mobileNumber newInvitee.email = email newInvitee.userId = getUserId() var encodedInviteeDictionary: [String: String]! do { let encodedInvitee = try JSONEncoder().encode(newInvitee) encodedInviteeDictionary = try JSONSerialization.jsonObject(with: encodedInvitee, options: .allowFragments) as? [String: String] } catch { } RestApiMananger.sharedInstance.createInvitee(param: encodedInviteeDictionary){ result in switch result { case let .success(createdJSON): do { self.newInvitee = try JSONDecoder().decode(InviteeModel.self, from: createdJSON.rawData()) self.inviteeCreated.value = true } catch { self.inviteeCreated.value = false } case let .failure(errorJSON): self.setErrorMessage(message: errorJSON["message"].stringValue) self.inviteeCreated.value = false case .updateAccessCode: RestApiMananger.sharedInstance.updateAccessCode(userId: self.getUserId(), password: self.getLoginPassword()) { result in if result != "Error"{ let uuid = "8da2e516-df9e-4aaa-b629-725150c4d8cc" UserDefaults.standard.set((result + ":" + uuid).toBase64(), forKey: "com.gmeremit.accessCode") self.createInvitee(name: name, mobileNumber: mobileNumber, email: email) } } case .logOutUser(): RestApiMananger.sharedInstance.cancelExistingNetworkCalls() self.anotherLogin.value = true case .timeOut: self.inviteeConnectionTimeOut.value = false } } } } /** Get invitee data */ func fetchInvitee(){ if !Reachability.isConnectedToNetwork() { self.internetConnection.value = false } else { RestApiMananger.sharedInstance.getInvitee(userId: getUserId()) { result in switch result { case let .success(fetchedJSON): self.inviteeArray.removeAll() guard fetchedJSON.count > 0 else { self.inviteeFetch.value = true return } if fetchedJSON.count > 0 { for i in 0 ... (fetchedJSON.count-1) { do { let invitee = try JSONDecoder().decode(InviteeModel.self, from: fetchedJSON[i].rawData()) self.inviteeArray.append(invitee) } catch { self.inviteeFetch.value = false } } } self.inviteeFetch.value = true case let .failure(errorJSON): self.setErrorMessage(message: errorJSON["message"].stringValue) self.inviteeFetch.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.fetchInvitee() } } case .logOutUser(): RestApiMananger.sharedInstance.cancelExistingNetworkCalls() self.anotherLogin.value = true case .timeOut: self.inviteeConnectionTimeOut.value = false } } } } /** Create invitee - returns: object of Invitee Model */ func getCreatedInvitee() -> InviteeModel { return newInvitee } /** - returns: total size of inviteeArray */ func getCount() -> Int { return inviteeArray.count } /** - returns: array of InviteeModel */ func getInviteeArray() -> [InviteeModel] { return inviteeArray } /** - parameter index: position of object - returns: position object of InviteeModel */ func getInvitee(index: Int) -> InviteeModel { return inviteeArray[index] } /** - parameter index: position of referral id in an array - returns: referral id */ func getReferralId(index:Int) -> String { if let getReferralId = inviteeArray[index].referralId{ return getReferralId } else { return "N/A" } } /** - parameter index: position of name in an array - returns: name */ func getName(index:Int) -> String { if let getName = inviteeArray[index].name{ return getName } else { return "N/A" } } /** - parameter index: position of mobile number in an array - returns: referral id */ func getMobileNumber(index:Int) -> String { if let getMobileNumber = inviteeArray[index].mobileNumber{ return getMobileNumber } else { return "N/A" } } /** - parameter index: position of email in an array - returns: email */ func getEmail(index:Int) -> String { if let getEmail = inviteeArray[index].email{ return getEmail } else { return "N/A" } } /** - parameter index: position of userId in an array - returns: userId */ func getUserId(index:Int) -> String { if let getUserId = inviteeArray[index].userId{ return getUserId } else { return "N/A" } } /** - parameter index: position of status in an array - returns: status */ func getStatus(index:Int) -> String { if let getStatus = inviteeArray[index].status{ return getStatus } else { return "N/A" } } } extension InviteViewModel { /** - parameter arrayofInfo: array of textfield input checking null - returns: if blank return false else true */ func allFieldsFilled(arrayofInfo: [String]) -> Bool { if arrayofInfo.count > 0{ for info in arrayofInfo { if info.isBlank { return false } } } return true } /** - parameter email: Invitee email - returns: true if valid email */ func isValidEmail(email:String) -> Bool { let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}" return NSPredicate(format:"SELF MATCHES %@", emailRegEx).evaluate(with: email) } }