Files
Woka_Native_iOS/WOKA/OnBoarding Module/ViewModel/SplashVM.swift

123 lines
4.7 KiB
Swift
Raw Normal View History

2024-05-02 13:20:40 +05:30
//
// SplashVM.swift
// WOKA
//
// Created by MacBook Pro on 25/04/24.
//
import UIKit
import AVFoundation
import Alamofire
2024-05-02 13:20:40 +05:30
class SplashVM{
weak var vc : SplashVC!
var player: AVAudioPlayer?
func initView(){
vc.activityIndicator.hidesWhenStopped = true
2024-05-02 13:20:40 +05:30
let color1 = #colorLiteral(red: 0.144693464, green: 0.1426281333, blue: 0.6686832905, alpha: 1)
let color2 = #colorLiteral(red: 0.4862745098, green: 0.1960784314, blue: 0.7019607843, alpha: 1)
vc.hindiBtn.applyGradient(colors: [color1, color2], startPoint: CGPoint(x: 0, y: 0), endPoint: CGPoint(x: 0, y: 0.8))
vc.englishBtn.applyGradient(colors: [color1, color2], startPoint: CGPoint(x: 0, y: 0), endPoint: CGPoint(x: 0, y: 0.8))
vc.hindiBtn.roundCorner()
vc.englishBtn.roundCorner()
selectTheme()
// check if user has selected the default language
if let language = AuthFunc.shareInstance.getDefaultLanguage(){
AuthFunc.shareInstance.languageSelected = language
}else{
AuthFunc.shareInstance.setDefaultLanguage(language: .english)
}
}
// Play initial sound
func playSplashSound() {
guard let path = Bundle.main.path(forResource: K.StaticFilesString.wokaSplashSound, ofType:"m4a") else {
return }
let url = URL(fileURLWithPath: path)
do {
player = try AVAudioPlayer(contentsOf: url)
player?.play()
} catch let error {
print(error.localizedDescription)
}
}
func selectTheme(){
let theme = UserDefaults.standard.string(forKey: K.UserDefaultsStruct.themeDefault)
if theme != nil{
(theme == ThemeSelect.theme1.rawValue) ? (AuthFunc.shareInstance.selectedTheme = .theme1) : (AuthFunc.shareInstance.selectedTheme = .theme2)
}else{
AuthFunc.shareInstance.selectedTheme = .theme1
}
2024-05-02 13:20:40 +05:30
}
// MARK: - Get the user data if loginned
func getUserData(){
if AuthFunc.shareInstance.getUserType() == 3{
//setusertype
AuthFunc.shareInstance.userData = UserDataDM.ResultData(id: nil, birthdate: nil, email: nil, avtar: nil, userType: "3", languageMasterID: nil, lastLogin: nil, rememberToken: nil, childDetail: nil, language: nil, alreadyLoggedIn: nil, isDeactive: nil)
UIApplication.setRootView(SideMenuController.instantiate(from: .Home))
return
}
/*
If user is guest then dont do the nw call
*/
if AuthFunc.shareInstance.getUserType() == 3{
UIApplication.setRootView(SideMenuController.instantiate(from: .Home))
return
}
let headers : HTTPHeaders = ["Accept-Language" : AuthFunc.shareInstance.languageSelected == .english ? "English" : "Hindi",
"access-token": AuthFunc.shareInstance.getAccessToken()]
startStopIndicator(start: true)
NetworkManager.shareInstance.apiRequest(url: APIEndPoints.Auth.get_user_data, method: .get, headers: headers) { [weak self](result : Result<BaseResponseModel<UserDataDM>, NetworkManager.APIError>) in
switch result{
case .success(let data):
guard let self else{return}
switch data.success{
case 0:
startStopIndicator(start: false)
self.vc.toast(msg: data.message ?? "Unrecognised error" , time: 2)
case 1:
startStopIndicator(start: false,hide: true)
guard let data = data.data?.result else{return}
AuthFunc.shareInstance.userData = data
UIApplication.setRootView(SideMenuController.instantiate(from: .Home))
print("User Token --> ", AuthFunc.shareInstance.getAccessToken())
default:
break
}
case .failure(let error):
guard let self else{return}
startStopIndicator(start: false)
self.vc.toast(msg: error.localizedDescription , time: 2)
}
}
}
// handling activity indicator
func startStopIndicator(start : Bool , hide : Bool = false){
if hide{
vc.activityIndicator.stopAnimating()
vc.activityIndicator.isHidden = true
vc.retryBtn.isHidden = true
return
}
if start{
vc.activityIndicator.isHidden = false
vc.activityIndicator.startAnimating()
vc.retryBtn.isHidden = true
}else{
vc.activityIndicator.stopAnimating()
vc.activityIndicator.isHidden = true
vc.retryBtn.isHidden = false
}
}
2024-05-02 13:20:40 +05:30
}