MessagesDataSource.swift 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. /// An object that adopts the `MessagesDataSource` protocol is responsible for providing
  22. /// the data required by a `MessagesCollectionView`.
  23. public protocol MessagesDataSource: AnyObject {
  24. /// The `Sender` of new messages in the `MessagesCollectionView`.
  25. func currentSender() -> Sender
  26. /// A helper method to determine if a given message is from the current `Sender`.
  27. ///
  28. /// - Parameters:
  29. /// - message: The message to check if it was sent by the current `Sender`.
  30. ///
  31. /// - Note:
  32. /// The default implementation of this method checks for equality between
  33. /// the message's `Sender` and the current `Sender`.
  34. func isFromCurrentSender(message: MessageType) -> Bool
  35. /// The message to be used for a `MessageCollectionViewCell` at the given `IndexPath`.
  36. ///
  37. /// - Parameters:
  38. /// - indexPath: The `IndexPath` of the cell.
  39. /// - messagesCollectionView: The `MessagesCollectionView` in which the message will be displayed.
  40. func messageForItem(at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageType
  41. /// The number of sections to be displayed in the `MessagesCollectionView`.
  42. ///
  43. /// - Parameters:
  44. /// - messagesCollectionView: The `MessagesCollectionView` in which the messages will be displayed.
  45. func numberOfSections(in messagesCollectionView: MessagesCollectionView) -> Int
  46. /// The number of cells to be displayed in the `MessagesCollectionView`.
  47. ///
  48. /// - Parameters:
  49. /// - section: The number of the section in which the cells will be displayed.
  50. /// - messagesCollectionView: The `MessagesCollectionView` in which the messages will be displayed.
  51. /// - Note:
  52. /// The default implementation of this method returns 1. Putting each message in its own section.
  53. func numberOfItems(inSection section: Int, in messagesCollectionView: MessagesCollectionView) -> Int
  54. /// The attributed text to be used for cell's top label.
  55. ///
  56. /// - Parameters:
  57. /// - message: The `MessageType` that will be displayed by this cell.
  58. /// - indexPath: The `IndexPath` of the cell.
  59. /// - messagesCollectionView: The `MessagesCollectionView` in which this cell will be displayed.
  60. ///
  61. /// The default value returned by this method is `nil`.
  62. func cellTopLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString?
  63. /// The attributed text to be used for message bubble's top label.
  64. ///
  65. /// - Parameters:
  66. /// - message: The `MessageType` that will be displayed by this cell.
  67. /// - indexPath: The `IndexPath` of the cell.
  68. /// - messagesCollectionView: The `MessagesCollectionView` in which this cell will be displayed.
  69. ///
  70. /// The default value returned by this method is `nil`.
  71. func messageTopLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString?
  72. /// The attributed text to be used for cell's bottom label.
  73. ///
  74. /// - Parameters:
  75. /// - message: The `MessageType` that will be displayed by this cell.
  76. /// - indexPath: The `IndexPath` of the cell.
  77. /// - messagesCollectionView: The `MessagesCollectionView` in which this cell will be displayed.
  78. ///
  79. /// The default value returned by this method is `nil`.
  80. func messageBottomLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString?
  81. /// Custom collectionView cell for message with `custom` message type.
  82. ///
  83. /// - Parameters:
  84. /// - message: The `custom` message type
  85. /// - indexPath: The `IndexPath` of the cell.
  86. /// - messagesCollectionView: The `MessagesCollectionView` in which this cell will be displayed.
  87. ///
  88. /// - Note:
  89. /// This method will call fatalError() on default. You must override this method if you are using MessageType.custom messages.
  90. func customCell(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> UICollectionViewCell
  91. }
  92. public extension MessagesDataSource {
  93. func isFromCurrentSender(message: MessageType) -> Bool {
  94. return message.sender == currentSender()
  95. }
  96. func numberOfItems(inSection section: Int, in messagesCollectionView: MessagesCollectionView) -> Int {
  97. return 1
  98. }
  99. func cellTopLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? {
  100. return nil
  101. }
  102. func messageTopLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? {
  103. return nil
  104. }
  105. func messageBottomLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? {
  106. return nil
  107. }
  108. func customCell(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> UICollectionViewCell {
  109. fatalError(MessageKitError.customDataUnresolvedCell)
  110. }
  111. }