SelectLanguageViewController.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // SelectLanguageViewController.swift
  3. // Learn Genie
  4. //
  5. // Created by Suraj Kumar Mandal on 20/08/21.
  6. //
  7. import UIKit
  8. class SelectLanguageViewController: UIViewController {
  9. @IBOutlet weak var selectLanguageTF: UITextField!
  10. var selectedLanguage: String?
  11. let data = DBManager.sharedInstance.getObjects(type: LanguageModel.self)
  12. let languageArray = [String]()
  13. fileprivate let picker = ToolbarPickerView()
  14. override func viewDidLoad() {
  15. super.viewDidLoad()
  16. // Do any additional setup after loading the view.
  17. }
  18. override func viewWillAppear(_ animated: Bool) {
  19. createPickerView()
  20. }
  21. func createPickerView() {
  22. self.selectLanguageTF.inputView = self.picker
  23. self.selectLanguageTF.inputAccessoryView = self.picker.toolbar
  24. self.picker.delegate = self
  25. self.picker.dataSource = self
  26. self.picker.toolbarDelegate = self
  27. self.picker.reloadAllComponents()
  28. }
  29. /*
  30. // MARK: - Navigation
  31. // In a storyboard-based application, you will often want to do a little preparation before navigation
  32. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  33. // Get the new view controller using segue.destination.
  34. // Pass the selected object to the new view controller.
  35. }
  36. */
  37. }
  38. extension SelectLanguageViewController: UIPickerViewDelegate, UIPickerViewDataSource {
  39. // MARK: UIPickerView Delegation
  40. func numberOfComponents(in pickerView: UIPickerView) -> Int {
  41. return 1
  42. }
  43. func pickerView( _ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
  44. return data?.count ?? 0
  45. }
  46. func pickerView( _ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
  47. let data = DBManager.sharedInstance.getObjects(type: LanguageModel.self)? [row] as! LanguageModel
  48. return data.name
  49. }
  50. func pickerView( _ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
  51. let languageData = DBManager.sharedInstance.getObjects(type: LanguageModel.self)? [row] as! LanguageModel
  52. selectLanguageTF.text = languageData.name
  53. UserDefaultsConstant.setIntValueInUserDefaults(objValue: languageData.id, for: Constant.languageId)
  54. UserDefaultsConstant.setValueInUserDefaults(objValue: languageData.shortName, for: Constant.languageShortName)
  55. UserDefaultsConstant.setValueInUserDefaults(objValue: languageData.name, for: Constant.languageName)
  56. }
  57. }
  58. extension SelectLanguageViewController: ToolbarPickerViewDelegate {
  59. func didTapDone() {
  60. let row = self.picker.selectedRow(inComponent: 0)
  61. self.picker.selectRow(row, inComponent: 0, animated: false)
  62. let data = DBManager.sharedInstance.getObjects(type: LanguageModel.self)? [row] as! LanguageModel
  63. self.selectLanguageTF.text = data.name
  64. UserDefaultsConstant.setIntValueInUserDefaults(objValue: data.id, for: Constant.languageId)
  65. UserDefaultsConstant.setValueInUserDefaults(objValue: data.shortName, for: Constant.languageShortName)
  66. UserDefaultsConstant.setValueInUserDefaults(objValue: data.name, for: Constant.languageName)
  67. self.selectLanguageTF.resignFirstResponder()
  68. if UserDefaults.standard.bool(forKey: Constant.welcomeSlider) {
  69. DispatchQueue.main.async {
  70. let welcomeVC = self.storyboard?.instantiateViewController(withIdentifier:"WelcomeSliderViewController" ) as! WelcomeSliderViewController
  71. self.navigationController?.pushViewController(welcomeVC, animated: true)
  72. }
  73. } else {
  74. DispatchQueue.main.async {
  75. self.navigationController?.popViewController(animated: true)
  76. }
  77. }
  78. }
  79. func didTapCancel() {
  80. self.selectLanguageTF.text = nil
  81. self.selectLanguageTF.resignFirstResponder()
  82. }
  83. }