// // SelectAvatarVM.swift // WOKA // // Created by MacBook Pro on 02/05/24. // import UIKit import Alamofire import FirebaseAnalytics class SelectAvatarVM{ weak var vc : SelectAvatarVC! var avatarData = [AvatarDM.ResultRecords]() // This will become true only if parent is adding child var createChildAccount : Bool? func initView(){ let color1 = #colorLiteral(red: 0.144693464, green: 0.1426281333, blue: 0.6686832905, alpha: 1) let color2 = #colorLiteral(red: 0.6901960784, green: 0.2745098039, blue: 0.7568627451, alpha: 1) vc.nextBtn.applyGradient(colors: [color1, color2], startPoint: CGPoint(x: 0, y: 0), endPoint: CGPoint(x: 0.8, y: 0)) vc.nextBtn.roundCorner() setupCell() getAvatarListing() self.vc.title = "Select your Avatar".localized(loc: AuthFunc.shareInstance.languageSelected.rawValue) } func setupCell(){ vc.collectionView.register(UINib(nibName: K.CellIdentifier.Authentication.selectAvatarCell, bundle: nil), forCellWithReuseIdentifier: K.CellIdentifier.Authentication.selectAvatarCell) vc.collectionView.delegate = self.vc vc.collectionView.dataSource = self.vc } // MARK: - Get Intrests func getAvatarListing(){ Utilities.startProgressHUD() let headers : HTTPHeaders = ["Accept-Language" : AuthFunc.shareInstance.languageSelected == .english ? "English" : "Hindi"] NetworkManager.shareInstance.apiRequest(url: APIEndPoints.Auth.avatar_listing, method: .get, headers : headers) {(result : Result, NetworkManager.APIError>) in switch result{ case .success(let data): switch data.success{ case 0: Utilities.dismissProgressHUD() let sb = UIStoryboard(name: K.StoryBoard.customAlerts, bundle: nil) let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.CustomAlerts.alertCustomVC) as! AlertCustomVC vcPush.contentLabel = data.message ?? "Unrecognised error" vcPush.mainTitleText = "Error" vcPush.yesBtnText = "Retry?" vcPush.onDoneBlock = { isDone in self.getAvatarListing() } vcPush.modalPresentationStyle = .overCurrentContext vcPush.modalTransitionStyle = .crossDissolve self.vc.present(vcPush, animated: true) return case 1: Utilities.dismissProgressHUD() if let avatarData = data.data?.result{ //for new user we will map individually in user signup and for overall we will map in newuser Analytics.logEvent(K.AnalyticsEventKeys.user_signup_iOS, parameters: nil) Analytics.logEvent(K.AnalyticsEventKeys.new_user_iOS, parameters: nil) self.avatarData = avatarData self.vc.collectionView.reloadData() } default: break } case .failure(let error): Utilities.dismissProgressHUD() let sb = UIStoryboard(name: K.StoryBoard.customAlerts, bundle: nil) let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.CustomAlerts.alertCustomVC) as! AlertCustomVC vcPush.contentLabel = error.localizedDescription vcPush.mainTitleText = "Error" vcPush.yesBtnText = "Retry?" vcPush.onDoneBlock = { isDone in self.getAvatarListing() } vcPush.modalPresentationStyle = .overCurrentContext vcPush.modalTransitionStyle = .crossDissolve self.vc.present(vcPush, animated: true) } } } func registerUser(){ /* Set UserType */ if createChildAccount == true{ AuthFunc.shareInstance.regData.user_type = "1" /* In Add child will be 1 */ AuthFunc.shareInstance.regData.add_child = "1" }else{ /* In Reg Add child will be 0 */ AuthFunc.shareInstance.regData.add_child = "0" if AuthFunc.shareInstance.userType == .adult{ AuthFunc.shareInstance.regData.user_type = "2" }else{ AuthFunc.shareInstance.regData.user_type = "1" } } /* Set Language id */ if AuthFunc.shareInstance.languageSelected == .hindi{ AuthFunc.shareInstance.regData.language_id = "2" }else{ AuthFunc.shareInstance.regData.language_id = "1" } AuthFunc.shareInstance.regData.one_signal_player_id = AuthFunc.shareInstance.getOneSignalID() Utilities.startProgressHUD() let headers : HTTPHeaders = ["device-id" : AuthFunc.shareInstance.getDeviceUUID(), "Accept-Language" : AuthFunc.shareInstance.languageSelected == .english ? "English" : "Hindi"] NetworkManager.shareInstance.apiRequest(url: APIEndPoints.Auth.child_registration, method: .post,parameters: AuthFunc.shareInstance.regData.dictionaryRepresentation, headers: headers) {(result : Result, NetworkManager.APIError>) in switch result{ case .success(let data): switch data.success{ case 0: Utilities.dismissProgressHUD() let sb = UIStoryboard(name: K.StoryBoard.customAlerts, bundle: nil) let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.CustomAlerts.alertCustomVC) as! AlertCustomVC vcPush.contentLabel = data.message ?? "Unrecognised error" vcPush.mainTitleText = "Error".localized(loc: AuthFunc.shareInstance.languageSelected.rawValue) vcPush.yesBtnText = "Retry?".localized(loc: AuthFunc.shareInstance.languageSelected.rawValue) vcPush.onDoneBlock = { isDone in self.registerUser() } vcPush.modalPresentationStyle = .overCurrentContext vcPush.modalTransitionStyle = .crossDissolve self.vc.present(vcPush, animated: true) return case 1: Utilities.dismissProgressHUD() AuthFunc.shareInstance.regData = UserRegPostModel() self.vc.toast(msg: data.message ?? "Something" , time: 2) { guard let data = data.data?.result else{return} AuthFunc.shareInstance.loginDefaults(data: data) } default: break } case .failure(let error): Utilities.dismissProgressHUD() let sb = UIStoryboard(name: K.StoryBoard.customAlerts, bundle: nil) let vcPush = sb.instantiateViewController(withIdentifier: K.StoryBoardID.CustomAlerts.alertCustomVC) as! AlertCustomVC vcPush.contentLabel = error.localizedDescription vcPush.mainTitleText = "Error" vcPush.yesBtnText = "Retry?" vcPush.onDoneBlock = { isDone in self.registerUser() } vcPush.modalPresentationStyle = .overCurrentContext vcPush.modalTransitionStyle = .crossDissolve self.vc.present(vcPush, animated: true) } } } }