OptionViewController.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // OptionViewController.swift
  3. // Learn Genie
  4. //
  5. // Created by Suraj Kumar Mandal on 15/09/21.
  6. //
  7. import UIKit
  8. import SideMenu
  9. class OptionViewController: UIViewController {
  10. @IBOutlet var navigationBar: UINavigationBar!
  11. @IBOutlet var optionTableView: UITableView!
  12. @IBOutlet var maxScoreLabel: UILabel!
  13. @IBOutlet var maxScoreNumberLabel: UILabel!
  14. @IBOutlet var runsLabel: UILabel!
  15. let levelData = DBManager.sharedInstance.database.objects(LevelModel.self)
  16. let scoreData = DBManager.sharedInstance.database.objects(SaveScoreBoard.self)
  17. var index = 0
  18. var levelId = Int()
  19. var levelName = String()
  20. var maxScore = Int()
  21. override func viewDidLoad() {
  22. super.viewDidLoad()
  23. // Do any additional setup after loading the view.
  24. optionTableView.delegate = self
  25. optionTableView.dataSource = self
  26. optionTableView.tableFooterView = UIView()
  27. optionTableView.allowsSelection = false
  28. setupUI()
  29. }
  30. override func viewWillAppear(_ animated: Bool) {
  31. navigationBar.topItem?.title = "\(Helper.translateText(inputText: "Scoreboard")) :\(Helper.translateText(inputText: levelName))"
  32. }
  33. func setupUI() {
  34. maxScoreLabel.text = Helper.translateText(inputText: "Max Score you can earn")
  35. runsLabel.text = Helper.translateText(inputText: "RUNS")
  36. maxScoreNumberLabel.text = String(maxScore)
  37. }
  38. func getTopicCount() -> Int {
  39. var count = Int()
  40. for data in levelData {
  41. if data.id == self.levelId {
  42. count = data.topics.count
  43. }
  44. }
  45. return count
  46. }
  47. func getTopicName(id:Int) -> String {
  48. let topicData = DBManager.sharedInstance.database.objects(TopicModel.self)
  49. var name = ""
  50. for item in topicData {
  51. if id == item.id {
  52. name = item.name
  53. break
  54. }
  55. }
  56. return name
  57. }
  58. /*
  59. // MARK: - Navigation
  60. // In a storyboard-based application, you will often want to do a little preparation before navigation
  61. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  62. // Get the new view controller using segue.destination.
  63. // Pass the selected object to the new view controller.
  64. }
  65. */
  66. @IBAction func menuNavAction(_ sender: Any) {
  67. let menu = storyboard!.instantiateViewController(withIdentifier: "SideMenuNavigationController") as! SideMenuNavigationController
  68. present(menu, animated: true, completion: nil)
  69. }
  70. }
  71. extension OptionViewController: UITableViewDelegate, UITableViewDataSource {
  72. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  73. return getTopicCount()
  74. }
  75. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  76. guard let cell = tableView.dequeueReusableCell(withIdentifier: "OptionsTableViewCell", for: indexPath) as? OptionsTableViewCell else {
  77. return UITableViewCell()
  78. }
  79. for level in levelData {
  80. if level.id == self.levelId {
  81. cell.optionTitleLabel.text = Helper.translateText(inputText: getTopicName(id: level.topics[indexPath.row].id))
  82. if let row = scoreData.firstIndex(where: {$0.levelId == level.id && $0.topicId == level.topics[indexPath.row].id}) {
  83. cell.noOfWicketsLabel.text = "\(String(scoreData[row].wickets)) \(Helper.translateText(inputText: "WICKETS"))"
  84. cell.noOfRunsLabel.text = "\(String(scoreData[row].runs)) \(Helper.translateText(inputText: "RUNS"))"
  85. cell.noOfAttemptsLabel.text = "\(String(scoreData[row].noOfAttempts)) \(Helper.translateText(inputText: "Attempts"))"
  86. }
  87. }
  88. }
  89. return cell
  90. }
  91. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  92. tableView.deselectRow(at: indexPath, animated: false)
  93. }
  94. }