// // SelectLanguageViewController.swift // Learn Genie // // Created by Suraj Kumar Mandal on 20/08/21. // import UIKit class SelectLanguageViewController: UIViewController { @IBOutlet weak var selectLanguageTF: UITextField! var selectedLanguage: String? let data = DBManager.sharedInstance.getObjects(type: LanguageModel.self) let languageArray = [String]() fileprivate let picker = ToolbarPickerView() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } override func viewWillAppear(_ animated: Bool) { createPickerView() } func createPickerView() { self.selectLanguageTF.inputView = self.picker self.selectLanguageTF.inputAccessoryView = self.picker.toolbar self.picker.delegate = self self.picker.dataSource = self self.picker.toolbarDelegate = self self.picker.reloadAllComponents() } /* // 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 SelectLanguageViewController: UIPickerViewDelegate, UIPickerViewDataSource { // MARK: UIPickerView Delegation func numberOfComponents(in pickerView: UIPickerView) -> Int { return 1 } func pickerView( _ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { return data?.count ?? 0 } func pickerView( _ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { let data = DBManager.sharedInstance.getObjects(type: LanguageModel.self)? [row] as! LanguageModel return data.name } func pickerView( _ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { let languageData = DBManager.sharedInstance.getObjects(type: LanguageModel.self)? [row] as! LanguageModel selectLanguageTF.text = languageData.name UserDefaultsConstant.setIntValueInUserDefaults(objValue: languageData.id, for: Constant.languageId) UserDefaultsConstant.setValueInUserDefaults(objValue: languageData.shortName, for: Constant.languageShortName) UserDefaultsConstant.setValueInUserDefaults(objValue: languageData.name, for: Constant.languageName) } } extension SelectLanguageViewController: ToolbarPickerViewDelegate { func didTapDone() { let row = self.picker.selectedRow(inComponent: 0) self.picker.selectRow(row, inComponent: 0, animated: false) let data = DBManager.sharedInstance.getObjects(type: LanguageModel.self)? [row] as! LanguageModel self.selectLanguageTF.text = data.name UserDefaultsConstant.setIntValueInUserDefaults(objValue: data.id, for: Constant.languageId) UserDefaultsConstant.setValueInUserDefaults(objValue: data.shortName, for: Constant.languageShortName) UserDefaultsConstant.setValueInUserDefaults(objValue: data.name, for: Constant.languageName) self.selectLanguageTF.resignFirstResponder() if UserDefaults.standard.bool(forKey: Constant.welcomeSlider) { DispatchQueue.main.async { let welcomeVC = self.storyboard?.instantiateViewController(withIdentifier:"WelcomeSliderViewController" ) as! WelcomeSliderViewController self.navigationController?.pushViewController(welcomeVC, animated: true) } } else { DispatchQueue.main.async { self.navigationController?.popViewController(animated: true) } } } func didTapCancel() { self.selectLanguageTF.text = nil self.selectLanguageTF.resignFirstResponder() } }