Add default DateHeader implementation and shouldDisplayHeader method

This commit is contained in:
Steven Deutsch
2017-09-06 05:28:31 -05:00
parent 8468c9baba
commit 35f73cd2e3
2 changed files with 17 additions and 2 deletions
+2
View File
@@ -36,6 +36,8 @@ open class MessagesCollectionView: UICollectionView {
open weak var messageLabelDelegate: MessageLabelDelegate?
open var showsDateHeaderAfterTimeInterval: TimeInterval = 3600
private var indexPathForLastItem: IndexPath? {
let lastSection = numberOfSections > 0 ? numberOfSections - 1 : 0
+15 -2
View File
@@ -37,6 +37,8 @@ public protocol MessagesDisplayDataSource: class, MessagesDataSource {
func avatarPosition(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> AvatarPosition
func messageHeaderView(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageHeaderView?
func shouldDisplayHeader(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> Bool
func messageFooterView(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageFooterView?
@@ -69,9 +71,20 @@ public extension MessagesDisplayDataSource {
}
func messageHeaderView(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageHeaderView? {
return nil
let header = messagesCollectionView.dequeueMessageHeaderView(withReuseIdentifier: "MessageDateHeader", for: indexPath) as? MessageDateHeader
header?.dateLabel.text = MessageKitDateFormatter.shared.string(from: message.sentDate)
return header
}
func shouldDisplayHeader(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> Bool {
if indexPath.section == 0 { return false }
let previousSection = indexPath.section - 1
let previousIndexPath = IndexPath(item: 0, section: previousSection)
let previousMessage = messageForItem(at: previousIndexPath, in: messagesCollectionView)
let timeIntervalSinceLastMessage = message.sentDate.timeIntervalSince(previousMessage.sentDate)
return timeIntervalSinceLastMessage >= messagesCollectionView.showsDateHeaderAfterTimeInterval
}
func messageFooterView(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageFooterView? {
return nil
}