MessagesCollectionViewFlowLayout.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. import AVFoundation
  22. /// The layout object used by `MessagesCollectionView` to determine the size of all
  23. /// framework provided `MessageCollectionViewCell` subclasses.
  24. open class MessagesCollectionViewFlowLayout: UICollectionViewFlowLayout {
  25. open override class var layoutAttributesClass: AnyClass {
  26. return MessagesCollectionViewLayoutAttributes.self
  27. }
  28. /// The `MessagesCollectionView` that owns this layout object.
  29. public var messagesCollectionView: MessagesCollectionView {
  30. guard let messagesCollectionView = collectionView as? MessagesCollectionView else {
  31. fatalError(MessageKitError.layoutUsedOnForeignType)
  32. }
  33. return messagesCollectionView
  34. }
  35. /// The `MessagesDataSource` for the layout's collection view.
  36. public var messagesDataSource: MessagesDataSource {
  37. guard let messagesDataSource = messagesCollectionView.messagesDataSource else {
  38. fatalError(MessageKitError.nilMessagesDataSource)
  39. }
  40. return messagesDataSource
  41. }
  42. /// The `MessagesLayoutDelegate` for the layout's collection view.
  43. public var messagesLayoutDelegate: MessagesLayoutDelegate {
  44. guard let messagesLayoutDelegate = messagesCollectionView.messagesLayoutDelegate else {
  45. fatalError(MessageKitError.nilMessagesLayoutDelegate)
  46. }
  47. return messagesLayoutDelegate
  48. }
  49. public var itemWidth: CGFloat {
  50. guard let collectionView = collectionView else { return 0 }
  51. return collectionView.frame.width - sectionInset.left - sectionInset.right
  52. }
  53. // MARK: - Initializers
  54. public override init() {
  55. super.init()
  56. setupView()
  57. setupObserver()
  58. }
  59. required public init?(coder aDecoder: NSCoder) {
  60. super.init(coder: aDecoder)
  61. setupView()
  62. setupObserver()
  63. }
  64. deinit {
  65. NotificationCenter.default.removeObserver(self)
  66. }
  67. // MARK: - Methods
  68. private func setupView() {
  69. sectionInset = UIEdgeInsets(top: 4, left: 8, bottom: 4, right: 8)
  70. }
  71. private func setupObserver() {
  72. NotificationCenter.default.addObserver(self, selector: #selector(MessagesCollectionViewFlowLayout.handleOrientationChange(_:)), name: UIDevice.orientationDidChangeNotification, object: nil)
  73. }
  74. // MARK: - Attributes
  75. open override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
  76. guard let attributesArray = super.layoutAttributesForElements(in: rect) as? [MessagesCollectionViewLayoutAttributes] else {
  77. return nil
  78. }
  79. for attributes in attributesArray where attributes.representedElementCategory == .cell {
  80. let cellSizeCalculator = cellSizeCalculatorForItem(at: attributes.indexPath)
  81. cellSizeCalculator.configure(attributes: attributes)
  82. }
  83. return attributesArray
  84. }
  85. open override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
  86. guard let attributes = super.layoutAttributesForItem(at: indexPath) as? MessagesCollectionViewLayoutAttributes else {
  87. return nil
  88. }
  89. if attributes.representedElementCategory == .cell {
  90. let cellSizeCalculator = cellSizeCalculatorForItem(at: attributes.indexPath)
  91. cellSizeCalculator.configure(attributes: attributes)
  92. }
  93. return attributes
  94. }
  95. // MARK: - Layout Invalidation
  96. open override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
  97. return collectionView?.bounds.width != newBounds.width
  98. }
  99. open override func invalidationContext(forBoundsChange newBounds: CGRect) -> UICollectionViewLayoutInvalidationContext {
  100. let context = super.invalidationContext(forBoundsChange: newBounds)
  101. guard let flowLayoutContext = context as? UICollectionViewFlowLayoutInvalidationContext else { return context }
  102. flowLayoutContext.invalidateFlowLayoutDelegateMetrics = shouldInvalidateLayout(forBoundsChange: newBounds)
  103. return flowLayoutContext
  104. }
  105. @objc
  106. private func handleOrientationChange(_ notification: Notification) {
  107. invalidateLayout()
  108. }
  109. // MARK: - Cell Sizing
  110. lazy open var textMessageSizeCalculator = TextMessageSizeCalculator(layout: self)
  111. lazy open var attributedTextMessageSizeCalculator = TextMessageSizeCalculator(layout: self)
  112. lazy open var emojiMessageSizeCalculator: TextMessageSizeCalculator = {
  113. let sizeCalculator = TextMessageSizeCalculator(layout: self)
  114. sizeCalculator.messageLabelFont = UIFont.systemFont(ofSize: sizeCalculator.messageLabelFont.pointSize * 2)
  115. return sizeCalculator
  116. }()
  117. lazy open var photoMessageSizeCalculator = MediaMessageSizeCalculator(layout: self)
  118. lazy open var videoMessageSizeCalculator = MediaMessageSizeCalculator(layout: self)
  119. lazy open var locationMessageSizeCalculator = LocationMessageSizeCalculator(layout: self)
  120. /// - Note:
  121. /// If you override this method, remember to call MessageLayoutDelegate's customCellSizeCalculator(for:at:in:) method for MessageKind.custom messages, if necessary
  122. open func cellSizeCalculatorForItem(at indexPath: IndexPath) -> CellSizeCalculator {
  123. let message = messagesDataSource.messageForItem(at: indexPath, in: messagesCollectionView)
  124. switch message.kind {
  125. case .text:
  126. return textMessageSizeCalculator
  127. case .attributedText:
  128. return attributedTextMessageSizeCalculator
  129. case .emoji:
  130. return emojiMessageSizeCalculator
  131. case .photo:
  132. return photoMessageSizeCalculator
  133. case .video:
  134. return videoMessageSizeCalculator
  135. case .location:
  136. return locationMessageSizeCalculator
  137. case .custom:
  138. return messagesLayoutDelegate.customCellSizeCalculator(for: message, at: indexPath, in: messagesCollectionView)
  139. }
  140. }
  141. open func sizeForItem(at indexPath: IndexPath) -> CGSize {
  142. let calculator = cellSizeCalculatorForItem(at: indexPath)
  143. return calculator.sizeForItem(at: indexPath)
  144. }
  145. /// Set `incomingAvatarSize` of all `MessageSizeCalculator`s
  146. public func setMessageIncomingAvatarSize(_ newSize: CGSize) {
  147. messageSizeCalculators().forEach { $0.incomingAvatarSize = newSize }
  148. }
  149. /// Set `outgoingAvatarSize` of all `MessageSizeCalculator`s
  150. public func setMessageOutgoingAvatarSize(_ newSize: CGSize) {
  151. messageSizeCalculators().forEach { $0.outgoingAvatarSize = newSize }
  152. }
  153. /// Set `incomingAvatarPosition` of all `MessageSizeCalculator`s
  154. public func setMessageIncomingAvatarPosition(_ newPosition: AvatarPosition) {
  155. messageSizeCalculators().forEach { $0.incomingAvatarPosition = newPosition }
  156. }
  157. /// Set `outgoingAvatarPosition` of all `MessageSizeCalculator`s
  158. public func setMessageOutgoingAvatarPosition(_ newPosition: AvatarPosition) {
  159. messageSizeCalculators().forEach { $0.outgoingAvatarPosition = newPosition }
  160. }
  161. /// Set `incomingMessagePadding` of all `MessageSizeCalculator`s
  162. public func setMessageIncomingMessagePadding(_ newPadding: UIEdgeInsets) {
  163. messageSizeCalculators().forEach { $0.incomingMessagePadding = newPadding }
  164. }
  165. /// Set `outgoingMessagePadding` of all `MessageSizeCalculator`s
  166. public func setMessageOutgoingMessagePadding(_ newPadding: UIEdgeInsets) {
  167. messageSizeCalculators().forEach { $0.outgoingMessagePadding = newPadding }
  168. }
  169. /// Set `incomingCellTopLabelAlignment` of all `MessageSizeCalculator`s
  170. public func setMessageIncomingCellTopLabelAlignment(_ newAlignment: LabelAlignment) {
  171. messageSizeCalculators().forEach { $0.incomingCellTopLabelAlignment = newAlignment }
  172. }
  173. /// Set `outgoingCellTopLabelAlignment` of all `MessageSizeCalculator`s
  174. public func setMessageOutgoingCellTopLabelAlignment(_ newAlignment: LabelAlignment) {
  175. messageSizeCalculators().forEach { $0.outgoingCellTopLabelAlignment = newAlignment }
  176. }
  177. /// Set `incomingMessageTopLabelAlignment` of all `MessageSizeCalculator`s
  178. public func setMessageIncomingMessageTopLabelAlignment(_ newAlignment: LabelAlignment) {
  179. messageSizeCalculators().forEach { $0.incomingMessageTopLabelAlignment = newAlignment }
  180. }
  181. /// Set `outgoingMessageTopLabelAlignment` of all `MessageSizeCalculator`s
  182. public func setMessageOutgoingMessageTopLabelAlignment(_ newAlignment: LabelAlignment) {
  183. messageSizeCalculators().forEach { $0.outgoingMessageTopLabelAlignment = newAlignment }
  184. }
  185. /// Set `incomingMessageBottomLabelAlignment` of all `MessageSizeCalculator`s
  186. public func setMessageIncomingMessageBottomLabelAlignment(_ newAlignment: LabelAlignment) {
  187. messageSizeCalculators().forEach { $0.incomingMessageBottomLabelAlignment = newAlignment }
  188. }
  189. /// Set `outgoingMessageBottomLabelAlignment` of all `MessageSizeCalculator`s
  190. public func setMessageOutgoingMessageBottomLabelAlignment(_ newAlignment: LabelAlignment) {
  191. messageSizeCalculators().forEach { $0.outgoingMessageBottomLabelAlignment = newAlignment }
  192. }
  193. /// Set `incomingAccessoryViewSize` of all `MessageSizeCalculator`s
  194. public func setMessageIncomingAccessoryViewSize(_ newSize: CGSize) {
  195. messageSizeCalculators().forEach { $0.incomingAccessoryViewSize = newSize }
  196. }
  197. /// Set `outgoingAvatarSize` of all `MessageSizeCalculator`s
  198. public func setMessageOutgoingAccessoryViewSize(_ newSize: CGSize) {
  199. messageSizeCalculators().forEach { $0.outgoingAccessoryViewSize = newSize }
  200. }
  201. /// Set `incomingAccessoryViewSize` of all `MessageSizeCalculator`s
  202. public func setMessageIncomingAccessoryViewPadding(_ newPadding: HorizontalEdgeInsets) {
  203. messageSizeCalculators().forEach { $0.incomingAccessoryViewPadding = newPadding }
  204. }
  205. /// Set `outgoingAvatarSize` of all `MessageSizeCalculator`s
  206. public func setMessageOutgoingAccessoryViewPadding(_ newPadding: HorizontalEdgeInsets) {
  207. messageSizeCalculators().forEach { $0.outgoingAccessoryViewPadding = newPadding }
  208. }
  209. /// Get all `MessageSizeCalculator`s
  210. open func messageSizeCalculators() -> [MessageSizeCalculator] {
  211. return [textMessageSizeCalculator, attributedTextMessageSizeCalculator, emojiMessageSizeCalculator, photoMessageSizeCalculator, videoMessageSizeCalculator, locationMessageSizeCalculator]
  212. }
  213. }