mirror of
https://github.com/MessageKit/MessageKit.git
synced 2026-02-06 19:03:19 +00:00
Add default DateHeader implementation and shouldDisplayHeader method
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user