// // OptionViewController.swift // Learn Genie // // Created by Suraj Kumar Mandal on 15/09/21. // import UIKit import SideMenu class OptionViewController: UIViewController { @IBOutlet var navigationBar: UINavigationBar! @IBOutlet var optionTableView: UITableView! @IBOutlet var maxScoreLabel: UILabel! @IBOutlet var maxScoreNumberLabel: UILabel! @IBOutlet var runsLabel: UILabel! let levelData = DBManager.sharedInstance.database.objects(LevelModel.self) let scoreData = DBManager.sharedInstance.database.objects(SaveScoreBoard.self) var index = 0 var levelId = Int() var levelName = String() var maxScore = Int() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. optionTableView.delegate = self optionTableView.dataSource = self optionTableView.tableFooterView = UIView() optionTableView.allowsSelection = false setupUI() } override func viewWillAppear(_ animated: Bool) { navigationBar.topItem?.title = "\(Helper.translateText(inputText: "Scoreboard")) :\(Helper.translateText(inputText: levelName))" } func setupUI() { maxScoreLabel.text = Helper.translateText(inputText: "Max Score you can earn") runsLabel.text = Helper.translateText(inputText: "RUNS") maxScoreNumberLabel.text = String(maxScore) } func getTopicCount() -> Int { var count = Int() for data in levelData { if data.id == self.levelId { count = data.topics.count } } return count } func getTopicName(id:Int) -> 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) } } extension OptionViewController: UITableViewDelegate, UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return getTopicCount() } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { guard let cell = tableView.dequeueReusableCell(withIdentifier: "OptionsTableViewCell", for: indexPath) as? OptionsTableViewCell else { return UITableViewCell() } for level in levelData { if level.id == self.levelId { cell.optionTitleLabel.text = Helper.translateText(inputText: getTopicName(id: level.topics[indexPath.row].id)) if let row = scoreData.firstIndex(where: {$0.levelId == level.id && $0.topicId == level.topics[indexPath.row].id}) { cell.noOfWicketsLabel.text = "\(String(scoreData[row].wickets)) \(Helper.translateText(inputText: "WICKETS"))" cell.noOfRunsLabel.text = "\(String(scoreData[row].runs)) \(Helper.translateText(inputText: "RUNS"))" cell.noOfAttemptsLabel.text = "\(String(scoreData[row].noOfAttempts)) \(Helper.translateText(inputText: "Attempts"))" } } } return cell } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tableView.deselectRow(at: indexPath, animated: false) } }