Files
Woka_Native_iOS/WOKA/Authentication/ViewModel/SelectAvatarVM.swift
BilalKhanWDI 6ee8786f65 - Updated the flow of auth.
- Finalised the Localization for the auth flow.
2024-05-14 11:12:28 +05:30

156 lines
6.8 KiB
Swift

//
// SelectAvatarVM.swift
// WOKA
//
// Created by MacBook Pro on 02/05/24.
//
import UIKit
import Alamofire
class SelectAvatarVM{
weak var vc : SelectAvatarVC!
var avatarData = [AvatarDM.ResultRecords]()
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<BaseResponseModel<AvatarDM>, 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{
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 AuthFunc.shareInstance.userType == .adult{
AuthFunc.shareInstance.regData.user_type = "2"
}else{
AuthFunc.shareInstance.regData.user_type = "1"
}
/*
In Reg Add child will be 0
*/
AuthFunc.shareInstance.regData.add_child = "0"
/*
Set Language id
*/
if AuthFunc.shareInstance.languageSelected == .hindi{
AuthFunc.shareInstance.regData.language_id = "2"
}else{
AuthFunc.shareInstance.regData.language_id = "1"
}
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<BaseResponseModel<UserDataDM>, 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.getAvatarListing()
}
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)
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)
}
}
}
}