PollResultsDetailsViewController.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  3. // SPDX-License-Identifier: GPL-3.0-or-later
  4. //
  5. import UIKit
  6. @objcMembers class PollResultsDetailsViewController: UITableViewController {
  7. struct PollResultDetail {
  8. let actorDisplayName: String
  9. let actorId: String
  10. let actorType: String
  11. let optionId: Int
  12. init(dictionary: [String: Any]) {
  13. self.actorDisplayName = dictionary["actorDisplayName"] as? String ?? ""
  14. self.actorId = dictionary["actorId"] as? String ?? ""
  15. self.actorType = dictionary["actorType"] as? String ?? ""
  16. self.optionId = dictionary["optionId"] as? Int ?? 0
  17. }
  18. }
  19. var poll: NCPoll
  20. var resultsDetails: [Int: [PollResultDetail]] = [:]
  21. var sortedOptions: [Int] = []
  22. public var room: NCRoom
  23. required init?(coder aDecoder: NSCoder) {
  24. self.poll = NCPoll()
  25. self.room = NCRoom()
  26. super.init(coder: aDecoder)
  27. self.setupPollResultsDetailsView()
  28. }
  29. init(poll: NCPoll, room: NCRoom) {
  30. self.poll = poll
  31. self.room = room
  32. super.init(style: .insetGrouped)
  33. self.setupPollResultsDetailsView()
  34. }
  35. override func viewDidLoad() {
  36. super.viewDidLoad()
  37. self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: NCAppBranding.themeTextColor()]
  38. self.navigationController?.navigationBar.tintColor = NCAppBranding.themeTextColor()
  39. self.navigationController?.navigationBar.barTintColor = NCAppBranding.themeColor()
  40. self.navigationController?.navigationBar.isTranslucent = false
  41. self.navigationItem.title = NSLocalizedString("Poll results", comment: "")
  42. let appearance = UINavigationBarAppearance()
  43. appearance.configureWithOpaqueBackground()
  44. appearance.titleTextAttributes = [.foregroundColor: NCAppBranding.themeTextColor()]
  45. appearance.backgroundColor = NCAppBranding.themeColor()
  46. self.navigationItem.standardAppearance = appearance
  47. self.navigationItem.compactAppearance = appearance
  48. self.navigationItem.scrollEdgeAppearance = appearance
  49. }
  50. func cancelButtonPressed() {
  51. self.dismiss(animated: true, completion: nil)
  52. }
  53. func setupPollResultsDetailsView() {
  54. self.tableView.dataSource = self
  55. self.tableView.delegate = self
  56. self.tableView.register(UINib(nibName: kShareTableCellNibName, bundle: .main), forCellReuseIdentifier: kShareCellIdentifier)
  57. self.tableView.separatorInset = UIEdgeInsets(top: 0, left: 54, bottom: 0, right: 0)
  58. // Create resultsDetails dictionary
  59. for detail in poll.details {
  60. if let resultDetail = detail as? [String: Any] {
  61. let pollResultDetail = PollResultDetail(dictionary: resultDetail)
  62. if var value = resultsDetails[pollResultDetail.optionId] {
  63. value.append(pollResultDetail)
  64. resultsDetails[pollResultDetail.optionId] = value
  65. } else {
  66. resultsDetails[pollResultDetail.optionId] = [pollResultDetail]
  67. }
  68. }
  69. }
  70. sortedOptions = Array(resultsDetails.keys).sorted()
  71. }
  72. override func numberOfSections(in tableView: UITableView) -> Int {
  73. return sortedOptions.count
  74. }
  75. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  76. let option = sortedOptions[section]
  77. if let optionDetails = resultsDetails[option] {
  78. return optionDetails.count
  79. }
  80. return 0
  81. }
  82. override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  83. let option = sortedOptions[section]
  84. return poll.options[option] as? String
  85. }
  86. override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  87. return kShareTableCellHeight
  88. }
  89. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  90. let cell = tableView.dequeueReusableCell(withIdentifier: kShareCellIdentifier) as? ShareTableViewCell ??
  91. ShareTableViewCell(style: .default, reuseIdentifier: kShareCellIdentifier)
  92. let option = sortedOptions[indexPath.section]
  93. let optionDetails = resultsDetails[option]
  94. guard let detail = optionDetails?[indexPath.row] else {return UITableViewCell()}
  95. // Actor name
  96. cell.titleLabel.text = detail.actorDisplayName
  97. // Actor avatar
  98. cell.avatarImageView.setActorAvatar(forId: detail.actorId, withType: detail.actorType, withDisplayName: detail.actorDisplayName, withRoomToken: self.room.token)
  99. return cell
  100. }
  101. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  102. tableView.deselectRow(at: indexPath, animated: true)
  103. }
  104. }