ViewAnswersViewController.swift 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //
  2. // ViewAnswersViewController.swift
  3. // LMS
  4. //
  5. // Created by Suraj Kumar Mandal on 20/10/23.
  6. //
  7. import UIKit
  8. class ViewAnswersViewController: UIViewController {
  9. @IBOutlet var answersTableView: UITableView!
  10. @IBOutlet var continueButton: UIButton!
  11. var viewModel = ViewAnswersViewModel()
  12. var answersModel = [AssessmentResultModel]()
  13. var userId = Int()
  14. var sessionId = String()
  15. var assessmentId = Int()
  16. override func viewDidLoad() {
  17. super.viewDidLoad()
  18. // Do any additional setup after loading the view.
  19. answersTableView.delegate = self
  20. answersTableView.dataSource = self
  21. // Enable self-sizing cells
  22. answersTableView.rowHeight = UITableView.automaticDimension
  23. answersTableView.estimatedRowHeight = 60 // You can adjust the estimated height as needed
  24. }
  25. override func viewWillAppear(_ animated: Bool) {
  26. getSubmittedAnswers()
  27. }
  28. func getSubmittedAnswers() {
  29. if Reachability.isConnectedToNetwork() {
  30. viewModel.getSubmittedAnswers(userId: userId, sessionId: sessionId, assessmentId: assessmentId) {
  31. // Update your UI components with the responseData
  32. self.answersModel = self.viewModel.answersModel
  33. print(self.answersModel)
  34. self.answersTableView.reloadData()
  35. }
  36. } else {
  37. Alert.showInternetFailureAlert(on: self)
  38. }
  39. }
  40. /*
  41. // MARK: - Navigation
  42. // In a storyboard-based application, you will often want to do a little preparation before navigation
  43. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  44. // Get the new view controller using segue.destination.
  45. // Pass the selected object to the new view controller.
  46. }
  47. */
  48. @IBAction func continueAction(_ sender: Any) {
  49. let vc = self.storyboard?.instantiateViewController(withIdentifier: "NewAssessmentViewController") as! NewAssessmentViewController
  50. self.navigationController?.pushViewController(vc, animated: true)
  51. }
  52. }
  53. extension ViewAnswersViewController: UITableViewDelegate, UITableViewDataSource {
  54. // MARK: - UITableViewDataSource
  55. func numberOfSections(in tableView: UITableView) -> Int {
  56. return answersModel.count
  57. }
  58. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  59. // Add 1 for the question cell and 4 for the choice cells
  60. let choiceCount = answersModel[section].options?.count ?? 0
  61. return choiceCount + 1
  62. }
  63. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  64. let answer = answersModel[indexPath.section]
  65. if indexPath.row == 0 {
  66. guard let cell = tableView.dequeueReusableCell(withIdentifier: "ViewAnswerQuestionTableViewCell", for: indexPath) as? ViewAnswerQuestionTableViewCell else {
  67. return UITableViewCell()
  68. }
  69. cell.questionLabel.text = "Q. \(answer.question ?? "")"
  70. return cell
  71. } else {
  72. guard let cell = tableView.dequeueReusableCell(withIdentifier: "ViewAnswerOptionsTableViewCell", for: indexPath) as? ViewAnswerOptionsTableViewCell else {
  73. return UITableViewCell()
  74. }
  75. let options = answer.options?[indexPath.row - 1].trimmingCharacters(in: .whitespaces)
  76. let selectedAnswer = answer.selectedAnswers?[0].trimmingCharacters(in: .whitespaces) as? String ?? ""
  77. let correctAnswer = answer.answers?[0] as? String ?? ""
  78. if options == selectedAnswer && options == correctAnswer {
  79. cell.choiceLabel.textColor = #colorLiteral(red: 0.4666666687, green: 0.7647058964, blue: 0.2666666806, alpha: 1)
  80. cell.dotImageView.tintColor = #colorLiteral(red: 0.4666666687, green: 0.7647058964, blue: 0.2666666806, alpha: 1)
  81. } else if options != selectedAnswer && options == correctAnswer {
  82. cell.choiceLabel.textColor = #colorLiteral(red: 0.4666666687, green: 0.7647058964, blue: 0.2666666806, alpha: 1)
  83. cell.dotImageView.tintColor = #colorLiteral(red: 0.4666666687, green: 0.7647058964, blue: 0.2666666806, alpha: 1)
  84. } else if options == selectedAnswer && options != correctAnswer {
  85. cell.choiceLabel.textColor = #colorLiteral(red: 0.7450980544, green: 0.1568627506, blue: 0.07450980693, alpha: 1)
  86. cell.dotImageView.tintColor = #colorLiteral(red: 0.7450980544, green: 0.1568627506, blue: 0.07450980693, alpha: 1)
  87. } else {
  88. cell.choiceLabel.textColor = UIColor.black
  89. cell.dotImageView.tintColor = UIColor.black
  90. }
  91. cell.choiceLabel?.text = answer.options?[indexPath.row - 1]
  92. return cell
  93. }
  94. }
  95. // MARK: - UITableViewDelegate
  96. func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
  97. return 60.0 // An estimated height for your cells
  98. }
  99. // func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  100. // let answer = answersModel[indexPath.section]
  101. //
  102. // if indexPath.row == 0 {
  103. // // Calculate the dynamic height based on the label's content.
  104. // let cell = tableView.dequeueReusableCell(withIdentifier: "ViewAnswerQuestionTableViewCell") as! ViewAnswerQuestionTableViewCell
  105. // cell.questionLabel.text = "Q. \(answer.question ?? "")"
  106. // cell.setNeedsLayout()
  107. // cell.layoutIfNeeded()
  108. // let labelSize = cell.questionLabel.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
  109. // return labelSize.height + 10.0 // Adjust this value for spacing as needed
  110. // } else {
  111. // // Calculate the dynamic height based on the label's content.
  112. // let cell = tableView.dequeueReusableCell(withIdentifier: "ViewAnswerOptionsTableViewCell") as! ViewAnswerOptionsTableViewCell
  113. // cell.choiceLabel.text = answer.options?[indexPath.row - 1]
  114. // cell.setNeedsLayout()
  115. // cell.layoutIfNeeded()
  116. // let labelSize = cell.choiceLabel.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)
  117. // return labelSize.height + 10.0 // Adjust this value for spacing as needed
  118. // }
  119. // }
  120. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  121. let answer = answersModel[indexPath.section]
  122. if indexPath.row == 0 {
  123. // Calculate the height based on label content and constraints in your custom cell
  124. let cell = tableView.dequeueReusableCell(withIdentifier: "ViewAnswerQuestionTableViewCell") as! ViewAnswerQuestionTableViewCell
  125. // Set the cell's label text to the data for the current row
  126. cell.questionLabel.text = "Q. \(answer.question ?? "")"
  127. // Calculate the required height based on the label's content
  128. let labelSize = cell.questionLabel.sizeThatFits(CGSize(width: cell.questionLabel.frame.size.width, height: CGFloat.greatestFiniteMagnitude))
  129. return labelSize.height + 10
  130. } else {
  131. let cell = tableView.dequeueReusableCell(withIdentifier: "ViewAnswerOptionsTableViewCell") as! ViewAnswerOptionsTableViewCell
  132. cell.choiceLabel.text = answer.options?[indexPath.row - 1]
  133. // Calculate the required height based on the label's content
  134. let labelSize = cell.choiceLabel.sizeThatFits(CGSize(width: cell.choiceLabel.frame.size.width, height: CGFloat.greatestFiniteMagnitude))
  135. return labelSize.height + 10 // Add extra space for padding, adjust as needed
  136. }
  137. }
  138. }