// // SplashScreenViewController.swift // Learn Genie // // Created by Suraj Kumar Mandal on 12/08/21. // import UIKit import FirebaseAuth import GoogleSignIn import Toast_Swift import AuthenticationServices import SwiftUI class SplashScreenViewController: UIViewController { @IBOutlet weak var activityIndicator: UIActivityIndicatorView! @IBOutlet var waitLabel: UILabel! var viewModel = SplashScreenViewModel() let group = DispatchGroup() var isWaiting = false override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. viewModel.delegate = self self.isWaiting = true //Set default language to english if UserDefaultsConstant.getIntValueFromUserDefults(for: Constant.languageId) == nil { UserDefaultsConstant.setIntValueInUserDefaults(objValue: 43, for: Constant.languageId) UserDefaultsConstant.setValueInUserDefaults(objValue: "en", for: Constant.languageShortName) UserDefaultsConstant.setValueInUserDefaults(objValue: "English", for: Constant.languageName) } //Condition for app in case of fist time launch if UIApplication.isFirstLaunch() { UserDefaults.standard.set(true, forKey: Constant.welcomeSlider) UserDefaults.standard.synchronize() UserDefaults.standard.set(true, forKey: Constant.introduction) UserDefaults.standard.synchronize() } firstTimeAppRun() //Animate activity indicator in 5 sec delay DispatchQueue.global(qos: .background).asyncAfter(deadline: .now() + 2) { DispatchQueue.main.async { self.activityIndicator.isHidden = false self.activityIndicator.color = .black self.activityIndicator.startAnimating() } } // DispatchQueue.global(qos: .background).asyncAfter(deadline: .now() + 5) { // DispatchQueue.main.async { // self.waitLabel.isHidden = false // self.waitLabel.text = "Downloading data, Please wait..." //// self.activityIndicator.stopAnimating() //// self.navigation() // } // } } override func viewWillAppear(_ animated: Bool) { activityIndicator.isHidden = true waitLabel.isHidden = true } func firstTimeAppRun() { if UserDefaults.standard.bool(forKey: Constant.welcomeSlider) { if Reachability.isConnectedToNetwork() { //callAPI() viewModel.generateCookie() } else { Alert.showInternetFailureAlert(on: self) } } else { DispatchQueue.global(qos: .background).asyncAfter(deadline: .now() + 5) { DispatchQueue.main.async { self.navigation() } } } } func callAPI() { group.enter() viewModel.getRole() group.enter() viewModel.getLanguage() group.enter() viewModel.getTranslation(offset: 0) group.enter() viewModel.getState(offset: 0) group.enter() viewModel.getDistrict(offset: 0) group.enter() viewModel.getTopic(offset: 0) group.notify(queue: .main, execute: { // Step3: Update the UI self.isWaiting = false print("All API called") self.activityIndicator.stopAnimating() self.navigation() }) } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destination. // Pass the selected object to the new view controller. } */ } extension SplashScreenViewController : SplashScreenViewProtocol { func callDataAPI() { self.callAPI() } func groupLeave() { group.leave() } func stopActivityIndicator() { activityIndicator.stopAnimating() } func showToastMessage(message: String) { self.view.makeToast(message) } func navigation() { if UserDefaults.standard.bool(forKey: Constant.welcomeSlider) { DispatchQueue.main.async { let selectLanguageVC = self.storyboard?.instantiateViewController(withIdentifier:"SelectLanguageViewController" ) as! SelectLanguageViewController self.navigationController?.pushViewController(selectLanguageVC, animated: true) } } else { if UserDefaultsConstant.getValueFromUserDefults(for: Constant.signInMethod) as! String == "google" { GIDSignIn.sharedInstance.restorePreviousSignIn { user, error in if error != nil || user == nil { // Show the app's signed-out state. DispatchQueue.main.async { let loginVC = self.storyboard?.instantiateViewController(withIdentifier:"LoginViewController" ) as! LoginViewController self.navigationController?.pushViewController(loginVC, animated: true) } } else { let userModel = DBManager.sharedInstance.database.objects(UserDetailsModel.self) if userModel.contains(where: {$0.email == user?.profile?.email}) { //Navigate to Home DispatchQueue.main.async { let loaderVC = self.storyboard?.instantiateViewController(withIdentifier:"LoadingAnimationViewController" ) as! LoadingAnimationViewController self.navigationController?.pushViewController(loaderVC, animated: true) } } else { //Navigate to User details DispatchQueue.main.async { let loaderVC = self.storyboard?.instantiateViewController(withIdentifier:"UserDetailsViewController" ) as! UserDetailsViewController self.navigationController?.pushViewController(loaderVC, animated: true) } } } } } else { let userIdentifier = UserDefaultsConstant.getValueFromUserDefults(for: Constant.userIdentifier) as! String let appleIDProvider = ASAuthorizationAppleIDProvider() appleIDProvider.getCredentialState(forUserID: userIdentifier) { (credentialState, error) in switch credentialState { case .authorized: DispatchQueue.main.async { let loaderVC = self.storyboard?.instantiateViewController(withIdentifier:"LoadingAnimationViewController" ) as! LoadingAnimationViewController self.navigationController?.pushViewController(loaderVC, animated: true) } // let userModel = DBManager.sharedInstance.database.objects(UserDetailsModel.self) // if userModel.contains(where: {$0.appleIdentifier == userIdentifier}) { // //Navigate to Home // DispatchQueue.main.async { // let loaderVC = self.storyboard?.instantiateViewController(withIdentifier:"LoadingAnimationViewController" ) as! LoadingAnimationViewController // self.navigationController?.pushViewController(loaderVC, animated: true) // } // } // else { // //Navigate to User details // DispatchQueue.main.async { // let userDetailsVC = self.storyboard?.instantiateViewController(withIdentifier:"UserDetailsViewController" ) as! UserDetailsViewController // self.navigationController?.pushViewController(userDetailsVC, animated: true) // } // } // The Apple ID credential is valid. break case .revoked, .notFound: // The Apple ID credential is either revoked or was not found, so show the sign-in UI. DispatchQueue.main.async { let loginVC = self.storyboard?.instantiateViewController(withIdentifier:"LoginViewController" ) as! LoginViewController self.navigationController?.pushViewController(loginVC, animated: true) } default: break } } } } } }