// // LearningModulesViewController.swift // Learn Genie // // Created by Suraj Kumar Mandal on 13/09/21. // import UIKit import SideMenu class LearningModulesViewController: UIViewController { @IBOutlet var learningModuleTableView: UITableView! @IBOutlet var navigationBar: UINavigationBar! @IBOutlet var optionView: UIView! @IBOutlet var topicHeadingLabel: UILabel! @IBOutlet var topicsTableView: UITableView! @IBOutlet var topicsTableHeightConstraint: NSLayoutConstraint! let levelData = DBManager.sharedInstance.database.objects(LevelModel.self) var index = 0 override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. learningModuleTableView.delegate = self learningModuleTableView.dataSource = self learningModuleTableView.tableFooterView = UIView() topicsTableView.delegate = self topicsTableView.dataSource = self topicsTableView.tableFooterView = UIView() topicsTableView.layer.cornerRadius = 15 optionView.isHidden = true print(levelData.count) for level in levelData { if level.name != "Introduction" { print("Level name: \(level.name)") } } } override func viewWillAppear(_ animated: Bool) { learningModuleTableView.reloadData() topicsTableView.reloadData() self.topicsTableView.layoutIfNeeded() self.topicsTableView.heightAnchor.constraint(equalToConstant: self.topicsTableView.contentSize.height).isActive = true navigationBar.topItem?.title = Helper.translateText(inputText: "Learning Modules") } func convertBytesToImage(byteString: String) -> UIImage? { let strings = byteString.components(separatedBy: ",") var bytes = [UInt8]() for i in 0.. String { let topicData = DBManager.sharedInstance.database.objects(TopicModel.self) var name = "" for item in topicData { if id == item.id { name = item.name break } } return name } /* // 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. } */ @IBAction func menuNavAction(_ sender: Any) { let menu = storyboard!.instantiateViewController(withIdentifier: "SideMenuNavigationController") as! SideMenuNavigationController present(menu, animated: true, completion: nil) } @IBAction func dismissOptionViewAction(_ sender: Any) { optionView.isHidden = true } } extension LearningModulesViewController: UITableViewDelegate, UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if tableView == learningModuleTableView { return levelData.count } else { print("Index: \(index)") print("Count: \(levelData[index])") return levelData[index].topics.count } } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { if tableView == learningModuleTableView { guard let cell = tableView.dequeueReusableCell(withIdentifier: "LearningModulesTableViewCell", for: indexPath) as? LearningModulesTableViewCell else { return UITableViewCell() } if levelData[indexPath.row].name == "Introduction" { cell.isHidden = true } else { cell.headLabel.text = Helper.translateText(inputText: levelData[indexPath.row].name) cell.contentLabel.text = Helper.translateText(inputText: Constant.learningModuleContentArray[indexPath.row]) let strFetched = levelData[indexPath.row].iconBytes cell.itemImageView.image = convertBytesToImage(byteString: strFetched) } return cell } else { guard let cell = tableView.dequeueReusableCell(withIdentifier: "OptionsTableViewCell", for: indexPath) as? OptionsTableViewCell else { return UITableViewCell() } cell.optionTitleLabel.text = Helper.translateText(inputText: getTopicName(id: levelData[index].topics[indexPath.row].id)) return cell } } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { DispatchQueue.main.async { [self] in if tableView == learningModuleTableView { self.index = indexPath.row self.topicHeadingLabel.text = Helper.translateText(inputText: levelData[index].name) topicsTableView.reloadData() optionView.isHidden = false } else { let introVC = self.storyboard?.instantiateViewController(withIdentifier: "IntroductionViewController") as! IntroductionViewController introVC.topicName = getTopicName(id: levelData[index].topics[indexPath.row].id) introVC.topicId = levelData[index].topics[indexPath.row].id introVC.mainTopicIndex = index introVC.topicIndex = indexPath.row self.navigationController?.pushViewController(introVC, animated: false) } } } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { var rowHeight:CGFloat = 0.0 if tableView == learningModuleTableView { if levelData[indexPath.row].name == "Introduction" { rowHeight = 0.0 } else { rowHeight = 160.0 //or whatever you like } } else { rowHeight = UITableView.automaticDimension } return rowHeight } func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { if tableView == learningModuleTableView { } else { self.topicsTableHeightConstraint.constant = self.topicsTableView.contentSize.height tableView.layoutIfNeeded() } } }