// // EmailVM.swift // WOKA // // Created by MacBook Pro on 29/04/24. // import UIKit import Alamofire class EmailVM{ weak var vc : EmailVC! 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() vc.enterEmailTF.roundCorner() // Set up text field delegate vc.enterEmailTF.delegate = vc.self vc.enterEmailTF.addRightButton(title: "", tintColor: UIColor.red, btnImage: UIImage(systemName: "exclamationmark.circle.fill"), target: self, action: #selector(validationIconTapped)) vc.enterEmailTF.rightView?.isHidden = true self.vc.wokaLogoTopConstriant.constant = CheckPhoneHomeBtnOrNotch.shareInstance.topConstriant() //Checking UserType to update the text if AuthFunc.shareInstance.userType == .adult{ vc.enterEmailTF.placeholder = K.AuthenticationStringConstant.enterEmail.localized(loc: AuthFunc.shareInstance.languageSelected.rawValue) vc.emailVerifyLabel.isHidden = true vc.beSafeLabel.text = K.AuthenticationStringConstant.safeAbove.localized(loc: AuthFunc.shareInstance.languageSelected.rawValue) vc.emailLabel.text = K.AuthenticationStringConstant.emailAbove.localized(loc: AuthFunc.shareInstance.languageSelected.rawValue) }else{ vc.enterEmailTF.placeholder = K.AuthenticationStringConstant.enterParentsEmail.localized(loc: AuthFunc.shareInstance.languageSelected.rawValue) vc.beSafeLabel.text = K.AuthenticationStringConstant.safeBelow.localized(loc: AuthFunc.shareInstance.languageSelected.rawValue) vc.emailLabel.text = K.AuthenticationStringConstant.emailBelow.localized(loc: AuthFunc.shareInstance.languageSelected.rawValue) } } // Function to handle tap on validation icon @objc func validationIconTapped() { let errorView = errorViews.object(forKey: vc.enterEmailTF) if let errorView = errorView { errorView.isHidden.toggle() } } func checkEmail(){ let params: Parameters = [ "email": vc.enterEmailTF.text!, "user_type": "2" ] NetworkManager.shareInstance.apiRequest(url: APIEndPoints.Auth.check_exist_email, method: .post ,parameters: params) {(result : Result, NetworkManager.APIError>) in switch result{ case .success(let data): switch data.success{ case 0: /* user exists */ Utilities.dismissProgressHUD() self.vc.toast(msg: data.message ?? "Unrecognised error" , time: 2) case 1: /* Create New Account Send OTP Start updating the user Reg data */ AuthFunc.shareInstance.regData.email = self.vc.enterEmailTF.text! self.sendOTP() default: break } case .failure(let error): Utilities.dismissProgressHUD() self.vc.toast(msg: error.localizedDescription , time: 2) } } } func sendOTP(){ let params: Parameters = [ "email": vc.enterEmailTF.text!, "user_type": AuthFunc.shareInstance.userType == .adult ? "2" :"1" ] let headers : HTTPHeaders = ["Accept-Language" : AuthFunc.shareInstance.languageSelected == .english ? "English" : "Hindi"] Utilities.startProgressHUD() NetworkManager.shareInstance.apiRequest(url: APIEndPoints.Auth.user_email_verification, method: .post ,parameters: params,headers: headers) {(result : Result, NetworkManager.APIError>) in switch result{ case .success(let data): switch data.success{ case 0: // some error Utilities.dismissProgressHUD() self.vc.toast(msg: data.message ?? "Unrecognised error" , time: 2) case 1: // Success Utilities.dismissProgressHUD() guard let uniqueString = data.data?.unique_string else{ self.vc.toast(msg: K.ConstantString.unRecognised , time: 1) return } self.vc.toast(msg: data.message ?? "Unrecognised error" , time: 1) { self.navigateToOTP(validateString: uniqueString) } default: Utilities.dismissProgressHUD() self.vc.toast(msg: K.ConstantString.unRecognised , time: 1) } case .failure(let error): Utilities.dismissProgressHUD() self.vc.toast(msg: error.localizedDescription , time: 2) } } } private func navigateToOTP(validateString : String){ let sb = UIStoryboard(name: K.StoryBoard.authenticationSB, bundle: nil) let vc = sb.instantiateViewController(withIdentifier: K.StoryBoardID.Authentication.oTPVC) as! OTPVC vc.vm.validateString = validateString self.vc.navigationController?.pushViewController(vc, animated: true) } }