MessagesDisplayDelegate.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 Foundation
  21. import MapKit
  22. /// A protocol used by the `MessagesViewController` to customize the appearance of a `MessageContentCell`.
  23. public protocol MessagesDisplayDelegate: AnyObject {
  24. // MARK: - All Messages
  25. /// Specifies the `MessageStyle` to be used for a `MessageContainerView`.
  26. ///
  27. /// - Parameters:
  28. /// - message: The `MessageType` that will be displayed by this cell.
  29. /// - indexPath: The `IndexPath` of the cell.
  30. /// - messagesCollectionView: The `MessagesCollectionView` in which this cell will be displayed.
  31. ///
  32. /// - Note:
  33. /// The default value returned by this method is `MessageStyle.bubble`.
  34. func messageStyle(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageStyle
  35. /// Specifies the background color of the `MessageContainerView`.
  36. ///
  37. /// - Parameters:
  38. /// - message: The `MessageType` that will be displayed by this cell.
  39. /// - indexPath: The `IndexPath` of the cell.
  40. /// - messagesCollectionView: The `MessagesCollectionView` in which this cell will be displayed.
  41. ///
  42. /// - Note:
  43. /// The default value is `UIColor.clear` for emoji messages.
  44. /// For all other `MessageKind` cases, the color depends on the `Sender`.
  45. ///
  46. /// Current sender: Green
  47. ///
  48. /// All other senders: Gray
  49. func backgroundColor(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> UIColor
  50. /// The section header to use for a given `IndexPath`.
  51. ///
  52. /// - Parameters:
  53. /// - message: The `MessageType` that will be displayed for this header.
  54. /// - indexPath: The `IndexPath` of the header.
  55. /// - messagesCollectionView: The `MessagesCollectionView` in which this header will be displayed.
  56. func messageHeaderView(for indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageReusableView
  57. /// The section footer to use for a given `IndexPath`.
  58. ///
  59. /// - Parameters:
  60. /// - indexPath: The `IndexPath` of the footer.
  61. /// - messagesCollectionView: The `MessagesCollectionView` in which this footer will be displayed.
  62. func messageFooterView(for indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageReusableView
  63. /// Used to configure the `AvatarView`‘s image in a `MessageContentCell` class.
  64. ///
  65. /// - Parameters:
  66. /// - avatarView: The `AvatarView` of the cell.
  67. /// - message: The `MessageType` that will be displayed by this cell.
  68. /// - indexPath: The `IndexPath` of the cell.
  69. /// - messagesCollectionView: The `MessagesCollectionView` in which this cell will be displayed.
  70. ///
  71. /// - Note:
  72. /// The default image configured by this method is `?`.
  73. func configureAvatarView(_ avatarView: AvatarView, for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView)
  74. // MARK: - Text Messages
  75. /// Specifies the color of the text for a `TextMessageCell`.
  76. ///
  77. /// - Parameters:
  78. /// - message: A `MessageType` with a `MessageKind` case of `.text` to which the color will apply.
  79. /// - indexPath: The `IndexPath` of the cell.
  80. /// - messagesCollectionView: The `MessagesCollectionView` in which this cell will be displayed.
  81. ///
  82. /// - Note:
  83. /// The default value returned by this method is determined by the messages `Sender`.
  84. ///
  85. /// Current sender: UIColor.white
  86. ///
  87. /// All other senders: UIColor.darkText
  88. func textColor(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> UIColor
  89. /// Specifies the `DetectorType`s to check for the `MessageType`'s text against.
  90. ///
  91. /// - Parameters:
  92. /// - message: A `MessageType` with a `MessageKind` case of `.text` or `.attributedText` to which the detectors will apply.
  93. /// - indexPath: The `IndexPath` of the cell.
  94. /// - messagesCollectionView: The `MessagesCollectionView` in which this cell will be displayed.
  95. ///
  96. /// - Note:
  97. /// This method returns an empty array by default.
  98. func enabledDetectors(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> [DetectorType]
  99. /// Specifies the attributes for a given `DetectorType`
  100. ///
  101. /// - Parameters:
  102. /// - detector: The `DetectorType` for the applied attributes.
  103. /// - message: A `MessageType` with a `MessageKind` case of `.text` or `.attributedText` to which the detectors will apply.
  104. /// - indexPath: The `IndexPath` of the cell.
  105. func detectorAttributes(for detector: DetectorType, and message: MessageType, at indexPath: IndexPath) -> [NSAttributedString.Key: Any]
  106. // MARK: - Location Messages
  107. /// Used to configure a `LocationMessageSnapshotOptions` instance to customize the map image on the given location message.
  108. ///
  109. /// - Parameters:
  110. /// - message: A `MessageType` with a `MessageKind` case of `.location`.
  111. /// - indexPath: The `IndexPath` of the cell.
  112. /// - messagesCollectionView: The `MessagesCollectionView` requesting the information.
  113. /// - Returns: The LocationMessageSnapshotOptions instance with the options to customize map style.
  114. func snapshotOptionsForLocation(message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> LocationMessageSnapshotOptions
  115. /// Used to configure the annoation view of the map image on the given location message.
  116. ///
  117. /// - Parameters:
  118. /// - message: A `MessageType` with a `MessageKind` case of `.location`.
  119. /// - indexPath: The `IndexPath` of the cell.
  120. /// - messagesCollectionView: The `MessagesCollectionView` requesting the information.
  121. /// - Returns: The `MKAnnotationView` to use as the annotation view.
  122. func annotationViewForLocation(message: MessageType, at indexPath: IndexPath, in messageCollectionView: MessagesCollectionView) -> MKAnnotationView?
  123. /// Ask the delegate for a custom animation block to run when whe map screenshot is ready to be displaied in the given location message.
  124. /// The animation block is called with the `UIImageView` to be animated.
  125. ///
  126. /// - Parameters:
  127. /// - message: A `MessageType` with a `MessageKind` case of `.location`.
  128. /// - indexPath: The `IndexPath` of the cell.
  129. /// - messagesCollectionView: The `MessagesCollectionView` requesting the information.
  130. /// - Returns: The animation block to use to apply the location image.
  131. func animationBlockForLocation(message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> ((UIImageView) -> Void)?
  132. // MARK: - Media Messages
  133. /// Used to configure the `UIImageView` of a `MediaMessageCell.
  134. ///
  135. /// - Parameters:
  136. /// - imageView: The `UIImageView` of the cell.
  137. /// - message: The `MessageType` that will be displayed by this cell.
  138. /// - indexPath: The `IndexPath` of the cell.
  139. /// - messagesCollectionView: The `MessagesCollectionView` in which this cell will be displayed.
  140. func configureMediaMessageImageView(_ imageView: UIImageView, for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView)
  141. }
  142. public extension MessagesDisplayDelegate {
  143. // MARK: - All Messages Defaults
  144. func messageStyle(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageStyle {
  145. return .bubble
  146. }
  147. func backgroundColor(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> UIColor {
  148. switch message.kind {
  149. case .emoji:
  150. return .clear
  151. default:
  152. guard let dataSource = messagesCollectionView.messagesDataSource else { return .white }
  153. return dataSource.isFromCurrentSender(message: message) ? .outgoingGreen : .incomingGray
  154. }
  155. }
  156. func messageHeaderView(for indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageReusableView {
  157. return messagesCollectionView.dequeueReusableHeaderView(MessageReusableView.self, for: indexPath)
  158. }
  159. func messageFooterView(for indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageReusableView {
  160. return messagesCollectionView.dequeueReusableFooterView(MessageReusableView.self, for: indexPath)
  161. }
  162. func configureAvatarView(_ avatarView: AvatarView, for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) {
  163. avatarView.initials = "?"
  164. }
  165. // MARK: - Text Messages Defaults
  166. func textColor(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> UIColor {
  167. guard let dataSource = messagesCollectionView.messagesDataSource else {
  168. fatalError(MessageKitError.nilMessagesDataSource)
  169. }
  170. return dataSource.isFromCurrentSender(message: message) ? .white : .darkText
  171. }
  172. func enabledDetectors(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> [DetectorType] {
  173. return []
  174. }
  175. func detectorAttributes(for detector: DetectorType, and message: MessageType, at indexPath: IndexPath) -> [NSAttributedString.Key: Any] {
  176. return MessageLabel.defaultAttributes
  177. }
  178. // MARK: - Location Messages Defaults
  179. func snapshotOptionsForLocation(message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> LocationMessageSnapshotOptions {
  180. return LocationMessageSnapshotOptions()
  181. }
  182. func annotationViewForLocation(message: MessageType, at indexPath: IndexPath, in messageCollectionView: MessagesCollectionView) -> MKAnnotationView? {
  183. return MKPinAnnotationView(annotation: nil, reuseIdentifier: nil)
  184. }
  185. func animationBlockForLocation(message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> ((UIImageView) -> Void)? {
  186. return nil
  187. }
  188. // MARK: - Media Message Defaults
  189. func configureMediaMessageImageView(_ imageView: UIImageView, for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) {
  190. }
  191. }