ReactionsSummaryView.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 ReactionsSummaryView: UITableViewController {
  7. var reactions: [String: [[String: AnyObject]]] = [:]
  8. var sortedReactions: [String] = []
  9. var reactionsBackgroundView: PlaceholderView = PlaceholderView(for: .grouped)
  10. public var room: NCRoom?
  11. required init?(coder aDecoder: NSCoder) {
  12. super.init(coder: aDecoder)
  13. self.setupReactionsSummaryView()
  14. }
  15. required override init(style: UITableView.Style) {
  16. super.init(style: style)
  17. self.setupReactionsSummaryView()
  18. }
  19. override func viewDidLoad() {
  20. super.viewDidLoad()
  21. self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: NCAppBranding.themeTextColor()]
  22. self.navigationController?.navigationBar.tintColor = NCAppBranding.themeTextColor()
  23. self.navigationController?.navigationBar.barTintColor = NCAppBranding.themeColor()
  24. self.navigationController?.navigationBar.isTranslucent = false
  25. self.navigationItem.title = NSLocalizedString("Reactions", comment: "")
  26. let appearance = UINavigationBarAppearance()
  27. appearance.configureWithOpaqueBackground()
  28. appearance.titleTextAttributes = [.foregroundColor: NCAppBranding.themeTextColor()]
  29. appearance.backgroundColor = NCAppBranding.themeColor()
  30. self.navigationItem.standardAppearance = appearance
  31. self.navigationItem.compactAppearance = appearance
  32. self.navigationItem.scrollEdgeAppearance = appearance
  33. reactionsBackgroundView.placeholderView.isHidden = true
  34. reactionsBackgroundView.loadingView.startAnimating()
  35. self.tableView.backgroundView = reactionsBackgroundView
  36. self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(self.cancelButtonPressed))
  37. self.navigationItem.leftBarButtonItem?.tintColor = NCAppBranding.themeTextColor()
  38. }
  39. func cancelButtonPressed() {
  40. self.dismiss(animated: true, completion: nil)
  41. }
  42. func setupReactionsSummaryView() {
  43. self.tableView.dataSource = self
  44. self.tableView.delegate = self
  45. self.tableView.register(UINib(nibName: kShareTableCellNibName, bundle: .main), forCellReuseIdentifier: kShareCellIdentifier)
  46. self.tableView.separatorInset = UIEdgeInsets(top: 0, left: 54, bottom: 0, right: 0)
  47. }
  48. func updateReactions(reactions: [String: [[String: AnyObject]]]) {
  49. self.reactions = reactions
  50. // Sort reactions by number of reactions
  51. for (k, _) in Array(reactions).sorted(by: {$0.value.count > $1.value.count}) {
  52. self.sortedReactions.append(k)
  53. }
  54. reactionsBackgroundView.loadingView.stopAnimating()
  55. reactionsBackgroundView.loadingView.isHidden = true
  56. self.tableView.reloadData()
  57. }
  58. override func numberOfSections(in tableView: UITableView) -> Int {
  59. return self.sortedReactions.count
  60. }
  61. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  62. let reaction = self.sortedReactions[section]
  63. if let actors = self.reactions[reaction] {
  64. return actors.count
  65. }
  66. return 0
  67. }
  68. override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  69. return self.sortedReactions[section]
  70. }
  71. override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  72. return kShareTableCellHeight
  73. }
  74. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  75. let cell = tableView.dequeueReusableCell(withIdentifier: kShareCellIdentifier) as? ShareTableViewCell ??
  76. ShareTableViewCell(style: .default, reuseIdentifier: kShareCellIdentifier)
  77. let reaction = self.sortedReactions[indexPath.section]
  78. let actor = self.reactions[reaction]?[indexPath.row]
  79. // Actor name
  80. let actorDisplayName = actor?["actorDisplayName"] as? String ?? ""
  81. cell.titleLabel.text = actorDisplayName.isEmpty ? NSLocalizedString("Guest", comment: "") : actorDisplayName
  82. // Actor avatar
  83. let actorId = actor?["actorId"] as? String ?? ""
  84. let actorType = actor?["actorType"] as? String ?? ""
  85. cell.avatarImageView.setActorAvatar(forId: actorId, withType: actorType, withDisplayName: actorDisplayName, withRoomToken: self.room?.token)
  86. return cell
  87. }
  88. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  89. tableView.deselectRow(at: indexPath, animated: true)
  90. }
  91. }