mirror of
https://github.com/MessageKit/MessageKit.git
synced 2026-02-06 19:03:19 +00:00
100 lines
4.5 KiB
Swift
100 lines
4.5 KiB
Swift
/*
|
|
MIT License
|
|
|
|
Copyright (c) 2017-2018 MessageKit
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
SOFTWARE.
|
|
*/
|
|
|
|
import UIKit
|
|
|
|
extension MessagesViewController: UICollectionViewDataSource {
|
|
|
|
open func numberOfSections(in collectionView: UICollectionView) -> Int {
|
|
guard let collectionView = collectionView as? MessagesCollectionView else {
|
|
fatalError(MessageKitError.notMessagesCollectionView)
|
|
}
|
|
// Each message is its own section
|
|
return collectionView.messagesDataSource?.numberOfMessages(in: collectionView) ?? 0
|
|
}
|
|
|
|
open func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
|
guard let collectionView = collectionView as? MessagesCollectionView else {
|
|
fatalError(MessageKitError.notMessagesCollectionView)
|
|
}
|
|
let messageCount = collectionView.messagesDataSource?.numberOfMessages(in: collectionView) ?? 0
|
|
// There will only ever be 1 message per section
|
|
return messageCount > 0 ? 1 : 0
|
|
}
|
|
|
|
open func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
|
|
|
guard let messagesCollectionView = collectionView as? MessagesCollectionView else {
|
|
fatalError(MessageKitError.notMessagesCollectionView)
|
|
}
|
|
|
|
guard let messagesDataSource = messagesCollectionView.messagesDataSource else {
|
|
fatalError(MessageKitError.nilMessagesDataSource)
|
|
}
|
|
|
|
let message = messagesDataSource.messageForItem(at: indexPath, in: messagesCollectionView)
|
|
|
|
switch message.data {
|
|
case .text, .attributedText, .emoji:
|
|
let cell = messagesCollectionView.dequeueReusableCell(TextMessageCell.self, for: indexPath)
|
|
cell.configure(with: message, at: indexPath, and: messagesCollectionView)
|
|
return cell
|
|
case .photo, .video:
|
|
let cell = messagesCollectionView.dequeueReusableCell(MediaMessageCell.self, for: indexPath)
|
|
cell.configure(with: message, at: indexPath, and: messagesCollectionView)
|
|
return cell
|
|
case .location:
|
|
let cell = messagesCollectionView.dequeueReusableCell(LocationMessageCell.self, for: indexPath)
|
|
cell.configure(with: message, at: indexPath, and: messagesCollectionView)
|
|
return cell
|
|
}
|
|
}
|
|
|
|
open func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
|
|
|
|
guard let messagesCollectionView = collectionView as? MessagesCollectionView else {
|
|
fatalError(MessageKitError.notMessagesCollectionView)
|
|
}
|
|
|
|
guard let dataSource = messagesCollectionView.messagesDataSource else {
|
|
fatalError(MessageKitError.nilMessagesDataSource)
|
|
}
|
|
|
|
guard let displayDelegate = messagesCollectionView.messagesDisplayDelegate else {
|
|
fatalError(MessageKitError.nilMessagesDisplayDelegate)
|
|
}
|
|
|
|
let message = dataSource.messageForItem(at: indexPath, in: messagesCollectionView)
|
|
|
|
switch kind {
|
|
case UICollectionElementKindSectionHeader:
|
|
return displayDelegate.messageHeaderView(for: message, at: indexPath, in: messagesCollectionView)
|
|
case UICollectionElementKindSectionFooter:
|
|
return displayDelegate.messageFooterView(for: message, at: indexPath, in: messagesCollectionView)
|
|
default:
|
|
fatalError(MessageKitError.unrecognizedSectionKind)
|
|
}
|
|
}
|
|
}
|