ReactionsView.swift 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //
  2. // SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  3. // SPDX-License-Identifier: GPL-3.0-or-later
  4. //
  5. import UIKit
  6. @objc protocol ReactionsViewDelegate {
  7. func didSelectReaction(reaction: NCChatReaction)
  8. }
  9. @objcMembers class ReactionsView: UICollectionView, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
  10. public weak var reactionsDelegate: ReactionsViewDelegate?
  11. var reactions: [NCChatReaction] = []
  12. required init?(coder aDecoder: NSCoder) {
  13. super.init(coder: aDecoder)
  14. self.setupReactionView()
  15. }
  16. required override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
  17. super.init(frame: frame, collectionViewLayout: layout)
  18. self.setupReactionView()
  19. }
  20. func setupReactionView() {
  21. self.dataSource = self
  22. self.delegate = self
  23. self.register(UINib(nibName: "ReactionsViewCell", bundle: .main), forCellWithReuseIdentifier: "ReactionCellIdentifier")
  24. self.backgroundColor = .clear
  25. self.showsHorizontalScrollIndicator = false
  26. }
  27. func updateReactions(reactions: [NCChatReaction]) {
  28. self.reactions = reactions
  29. self.reloadData()
  30. }
  31. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  32. return reactions.count
  33. }
  34. func collectionView(_ collectionView: UICollectionView, numberOfSections section: Int) -> Int {
  35. return 1
  36. }
  37. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  38. return 8
  39. }
  40. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  41. return 8
  42. }
  43. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  44. if indexPath.row < reactions.count {
  45. return ReactionsViewCell().sizeForReaction(reaction: reactions[indexPath.row])
  46. }
  47. return CGSize(width: 50, height: 30)
  48. }
  49. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  50. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ReactionCellIdentifier", for: indexPath) as? ReactionsViewCell
  51. if indexPath.row < reactions.count {
  52. cell?.setReaction(reaction: reactions[indexPath.row])
  53. }
  54. return cell ?? UICollectionViewCell()
  55. }
  56. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  57. if indexPath.row < reactions.count {
  58. self.reactionsDelegate?.didSelectReaction(reaction: reactions[indexPath.row])
  59. }
  60. }
  61. }