// // ProfileViewModel.swift // GMERemittance // // Created by FMI-12 on 1/29/18. // Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved. // import Foundation class ProfileViewModel: ModelExtension { var userDataAvailable:Box = Box(nil) var imageSubmitted:Box = Box(nil) var profileConnectionTimeOut: Box = Box(nil) var availableBalance: String? var rewardPoint: String? var walletNumber: String? private var profileInfo: ProfileModel! private var user_id: String! private var userImageDocUrl: String? /** Api request for user Information - parameter String: UserId */ func fetchUserInfo(userId: String?) { if userId == nil { user_id = UserDefaults.standard.object(forKey: "com.gmeremit.username") as? String } else { user_id = userId! } if !Reachability.isConnectedToNetwork() { self.internetConnection.value = false } else { RestApiMananger.sharedInstance.getUserDetails(userId: self.user_id) { result in switch result { case let .success(userJSON): do { self.profileInfo = try JSONDecoder().decode(ProfileModel.self, from: userJSON.rawData()) if userId == nil { self.availableBalance = userJSON["availableBalance"].stringValue self.rewardPoint = userJSON["rewardPoint"].stringValue self.walletNumber = userJSON["walletNumber"].stringValue } self.userDataAvailable.value = true } catch { self.userDataAvailable.value = false } case let .failure(errorJSON): self.setErrorMessage(message: "Update Failed:" + errorJSON["message"].stringValue) self.userDataAvailable.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.fetchUserInfo(userId: userId) } } case .logOutUser(): RestApiMananger.sharedInstance.cancelExistingNetworkCalls() self.anotherLogin.value = true case .timeOut: self.profileConnectionTimeOut.value = false } } } } /** Submit the Image provided - parameter docType: type of document - parameter imageBase64 data */ func provideImageForSubmission(docType: String, imageBase64: String) { if !Reachability.isConnectedToNetwork() { self.internetConnection.value = false } else { let docParam = ["documentType": docType, "file": imageBase64] RestApiMananger.sharedInstance.submitDocument(param: docParam, userId: self.user_id!) { result in switch result { case let .success(imageJSON): if let docUrl = imageJSON["documentUrl"].rawString() { self.updateUserInfo(param: ["dpUrl": docUrl]) } else { self.setErrorMessage(message:"No image URL generated") self.imageSubmitted.value = false } case let .failure(errorJSON): self.setErrorMessage(message: errorJSON["message"].stringValue) self.imageSubmitted.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.provideImageForSubmission(docType: docType, imageBase64: imageBase64) } } case .logOutUser(): RestApiMananger.sharedInstance.cancelExistingNetworkCalls() self.anotherLogin.value = true case .timeOut: self.profileConnectionTimeOut.value = false } } } } /** Api request to update user information - parameter param: User information is in Dictionary */ func updateUserInfo(param: [String: String]) { if !Reachability.isConnectedToNetwork() { self.internetConnection.value = false } else { RestApiMananger.sharedInstance.updateUserDetail (userId: user_id, param: param) { result in switch result { case let .success(fetchedJSON): UserDefaults.standard.set(fetchedJSON["dpUrl"].stringValue, forKey: "com.gmeremit.dpUrl") self.imageSubmitted.value = true case let .failure(errorJSON): self.setErrorMessage(message: errorJSON["message"].stringValue) self.imageSubmitted.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.updateUserInfo(param: param) } } case .logOutUser(): RestApiMananger.sharedInstance.cancelExistingNetworkCalls() self.anotherLogin.value = true case .timeOut: self.profileConnectionTimeOut.value = false } } } } /** to get proile info - returns: profile Info */ func getProfileModel() -> ProfileModel { return profileInfo } /** To get full name - returns: Full name */ func getFullName() -> String { if profileInfo.middleName == nil { return profileInfo.firstName + " " + profileInfo.lastName } else { return profileInfo.firstName + " " + profileInfo.middleName! + " " + profileInfo.lastName } } /** To get user balance - returns: Amount */ func getAvailableBalance() -> String { if let balance = availableBalance { return balance } return "N/A" } /** To get user email - returns: email */ func getEmail() -> String { if let email = profileInfo.email { return email } return "Email: N/A" } /** To get user Phone nos. - returns: Phone nos. */ func getPhone() -> String { if let phone = profileInfo.mobileNumber { return phone } return "Phone: N/A" } /** To get user wallet Nos. - returns: wallet Nos. */ func getWalletNumber() -> String { if let walletNum = walletNumber { return walletNum } return "Wallet Number: N/A" } /** To get user bank name - returns: bank name */ func getBankName() -> String { if let bankName = profileInfo.primaryBankName { return bankName } return "Primary Bank: N/A" } //Added to determine if the user has filled kyc form func hasFilledKYC() -> Bool { if profileInfo.kyc { return true } return false } /// To deteemine verified account func isVerified() -> Bool { if profileInfo.verified! { return true } return false } /** To get user reward point - returns: points */ func getRewardPoint() -> String { if let points = rewardPoint { return points } return "N/A" } }