MessagesDataSource & MessagesLayoutDelegate methods for MessageType.custom handling.

This commit is contained in:
Pavel Anokhov
2018-09-08 11:16:42 +03:00
parent ba9baace72
commit fe0a000fc5
4 changed files with 34 additions and 2 deletions
@@ -206,7 +206,7 @@ UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
cell.configure(with: message, at: indexPath, and: messagesCollectionView)
return cell
case .custom:
fatalError(MessageKitError.customDataUnresolvedCell)
return messagesDataSource.customCell(for: message, at: indexPath, in: messagesCollectionView)
}
}
@@ -151,7 +151,7 @@ open class MessagesCollectionViewFlowLayout: UICollectionViewFlowLayout {
case .location:
return locationMessageSizeCalculator
case .custom:
fatalError("Must return a CellSizeCalculator for MessageKind.custom(Any?)")
return messagesLayoutDelegate.cellSizeCalculator(for: message, at: indexPath, in: messagesCollectionView)
}
}
@@ -92,6 +92,18 @@ public protocol MessagesDataSource: AnyObject {
///
/// The default value returned by this method is `nil`.
func messageBottomLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString?
/// Custom collectionView cell for message with `custom` message type.
///
/// - Parameters:
/// - message: The `custom` message type
/// - indexPath: The `IndexPath` of the cell.
/// - messagesCollectionView: The `MessagesCollectionView` in which this cell will be displayed.
///
/// - Note:
/// This method will call fatalError() on default. You must override this method if you are using MessageType.custom messages.
func customCell(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> UICollectionViewCell
}
public extension MessagesDataSource {
@@ -115,4 +127,8 @@ public extension MessagesDataSource {
func messageBottomLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? {
return nil
}
func customCell(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> UICollectionViewCell {
fatalError(MessageKitError.customDataUnresolvedCell)
}
}
@@ -80,6 +80,18 @@ public protocol MessagesLayoutDelegate: AnyObject {
/// - Note:
/// The default value returned by this method is zero.
func messageBottomLabelHeight(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> CGFloat
/// Cell size calculator for messages with MessageType.custom.
///
/// - Parameters:
/// - message: The custom message
/// - indexPath: The `IndexPath` of the cell.
/// - messagesCollectionView: The `MessagesCollectionView` in which this cell will be displayed.
///
/// - Note:
/// The default implementation will throw fatalError(). You must override this method if you are using messages with MessageType.custom.
func cellSizeCalculator(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> CellSizeCalculator
}
public extension MessagesLayoutDelegate {
@@ -103,4 +115,8 @@ public extension MessagesLayoutDelegate {
func messageBottomLabelHeight(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> CGFloat {
return 0
}
func cellSizeCalculator(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> CellSizeCalculator {
fatalError("Must return a CellSizeCalculator for MessageKind.custom(Any?)")
}
}