MessagesViewController+DataSource.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. MIT License
  3. Copyright (c) 2017-2018 MessageKit
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. */
  20. import UIKit
  21. extension MessagesViewController: UICollectionViewDataSource {
  22. open func numberOfSections(in collectionView: UICollectionView) -> Int {
  23. guard let collectionView = collectionView as? MessagesCollectionView else {
  24. fatalError(MessageKitError.notMessagesCollectionView)
  25. }
  26. // Each message is its own section
  27. return collectionView.messagesDataSource?.numberOfMessages(in: collectionView) ?? 0
  28. }
  29. open func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  30. guard let collectionView = collectionView as? MessagesCollectionView else {
  31. fatalError(MessageKitError.notMessagesCollectionView)
  32. }
  33. let messageCount = collectionView.messagesDataSource?.numberOfMessages(in: collectionView) ?? 0
  34. // There will only ever be 1 message per section
  35. return messageCount > 0 ? 1 : 0
  36. }
  37. open func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  38. guard let messagesCollectionView = collectionView as? MessagesCollectionView else {
  39. fatalError(MessageKitError.notMessagesCollectionView)
  40. }
  41. guard let messagesDataSource = messagesCollectionView.messagesDataSource else {
  42. fatalError(MessageKitError.nilMessagesDataSource)
  43. }
  44. let message = messagesDataSource.messageForItem(at: indexPath, in: messagesCollectionView)
  45. switch message.data {
  46. case .text, .attributedText, .emoji:
  47. let cell = messagesCollectionView.dequeueReusableCell(TextMessageCell.self, for: indexPath)
  48. cell.configure(with: message, at: indexPath, and: messagesCollectionView)
  49. return cell
  50. case .photo, .video:
  51. let cell = messagesCollectionView.dequeueReusableCell(MediaMessageCell.self, for: indexPath)
  52. cell.configure(with: message, at: indexPath, and: messagesCollectionView)
  53. return cell
  54. case .location:
  55. let cell = messagesCollectionView.dequeueReusableCell(LocationMessageCell.self, for: indexPath)
  56. cell.configure(with: message, at: indexPath, and: messagesCollectionView)
  57. return cell
  58. }
  59. }
  60. open func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  61. guard let messagesCollectionView = collectionView as? MessagesCollectionView else {
  62. fatalError(MessageKitError.notMessagesCollectionView)
  63. }
  64. guard let dataSource = messagesCollectionView.messagesDataSource else {
  65. fatalError(MessageKitError.nilMessagesDataSource)
  66. }
  67. guard let displayDelegate = messagesCollectionView.messagesDisplayDelegate else {
  68. fatalError(MessageKitError.nilMessagesDisplayDelegate)
  69. }
  70. let message = dataSource.messageForItem(at: indexPath, in: messagesCollectionView)
  71. switch kind {
  72. case UICollectionElementKindSectionHeader:
  73. return displayDelegate.messageHeaderView(for: message, at: indexPath, in: messagesCollectionView)
  74. case UICollectionElementKindSectionFooter:
  75. return displayDelegate.messageFooterView(for: message, at: indexPath, in: messagesCollectionView)
  76. default:
  77. fatalError(MessageKitError.unrecognizedSectionKind)
  78. }
  79. }
  80. }