mirror of
https://github.com/MessageKit/MessageKit.git
synced 2026-02-06 19:03:19 +00:00
149 lines
5.6 KiB
Swift
149 lines
5.6 KiB
Swift
/*
|
|
MIT License
|
|
|
|
Copyright (c) 2017 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
|
|
|
|
open class MessageCollectionViewCell: UICollectionViewCell, CollectionViewReusable {
|
|
|
|
open class func reuseIdentifier() -> String {
|
|
return "messagekit.cell.base-cell"
|
|
}
|
|
|
|
open var avatarView = AvatarView()
|
|
|
|
open var messageContainerView: MessageContainerView = {
|
|
let containerView = MessageContainerView()
|
|
containerView.clipsToBounds = true
|
|
containerView.layer.masksToBounds = true
|
|
return containerView
|
|
}()
|
|
|
|
open var cellTopLabel: UILabel = {
|
|
let label = UILabel()
|
|
label.numberOfLines = 0
|
|
return label
|
|
}()
|
|
|
|
open var cellBottomLabel: UILabel = {
|
|
let label = UILabel()
|
|
label.numberOfLines = 0
|
|
return label
|
|
}()
|
|
|
|
open weak var delegate: MessageCellDelegate?
|
|
|
|
override public init(frame: CGRect) {
|
|
super.init(frame: frame)
|
|
contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
|
|
setupSubviews()
|
|
setupGestureRecognizers()
|
|
}
|
|
|
|
required public init?(coder aDecoder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
open func setupSubviews() {
|
|
contentView.addSubview(messageContainerView)
|
|
contentView.addSubview(avatarView)
|
|
contentView.addSubview(cellTopLabel)
|
|
contentView.addSubview(cellBottomLabel)
|
|
}
|
|
|
|
open override func prepareForReuse() {
|
|
cellTopLabel.text = nil
|
|
cellTopLabel.attributedText = nil
|
|
cellBottomLabel.text = nil
|
|
cellBottomLabel.attributedText = nil
|
|
}
|
|
|
|
// MARK: - Configuration
|
|
|
|
open override func apply(_ layoutAttributes: UICollectionViewLayoutAttributes) {
|
|
super.apply(layoutAttributes)
|
|
if let attributes = layoutAttributes as? MessagesCollectionViewLayoutAttributes {
|
|
avatarView.frame = attributes.avatarFrame
|
|
cellTopLabel.frame = attributes.topLabelFrame
|
|
cellBottomLabel.frame = attributes.bottomLabelFrame
|
|
messageContainerView.frame = attributes.messageContainerFrame
|
|
}
|
|
}
|
|
|
|
open func configure(with message: MessageType, at indexPath: IndexPath, and messagesCollectionView: MessagesCollectionView) {
|
|
guard let dataSource = messagesCollectionView.messagesDataSource else {
|
|
fatalError("MessagesDataSource is not set.")
|
|
}
|
|
guard let displayDelegate = messagesCollectionView.messagesDisplayDelegate else {
|
|
fatalError("MessagesDisplayDelegate is not set.")
|
|
}
|
|
|
|
delegate = messagesCollectionView.messageCellDelegate
|
|
|
|
let messageColor = displayDelegate.backgroundColor(for: message, at: indexPath, in: messagesCollectionView)
|
|
let messageStyle = displayDelegate.messageStyle(for: message, at: indexPath, in: messagesCollectionView)
|
|
|
|
messageContainerView.backgroundColor = messageColor
|
|
messageContainerView.style = messageStyle
|
|
|
|
let avatar = dataSource.avatar(for: message, at: indexPath, in: messagesCollectionView)
|
|
let topText = dataSource.cellTopLabelAttributedText(for: message, at: indexPath)
|
|
let bottomText = dataSource.cellBottomLabelAttributedText(for: message, at: indexPath)
|
|
|
|
avatarView.set(avatar: avatar)
|
|
cellTopLabel.attributedText = topText
|
|
cellBottomLabel.attributedText = bottomText
|
|
}
|
|
|
|
func setupGestureRecognizers() {
|
|
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTapGesture(_:)))
|
|
contentView.addGestureRecognizer(tapGesture)
|
|
}
|
|
|
|
/// Handle tap gesture on contentView and its subviews like messageContainerView, cellTopLabel, cellBottomLabel, avatarView ....
|
|
@objc
|
|
open func handleTapGesture(_ gesture: UIGestureRecognizer) {
|
|
guard gesture.state == .ended else { return }
|
|
|
|
let touchLocation = gesture.location(in: self)
|
|
|
|
switch true {
|
|
case messageContainerView.frame.contains(touchLocation) && !cellContentView(canHandle: convert(touchLocation, to: messageContainerView)):
|
|
delegate?.didTapMessage(in: self)
|
|
case avatarView.frame.contains(touchLocation):
|
|
delegate?.didTapAvatar(in: self)
|
|
case cellTopLabel.frame.contains(touchLocation):
|
|
delegate?.didTapTopLabel(in: self)
|
|
case cellBottomLabel.frame.contains(touchLocation):
|
|
delegate?.didTapBottomLabel(in: self)
|
|
default:
|
|
break
|
|
}
|
|
}
|
|
|
|
/// Handle `ContentView`'s tap gesture, return false when `ContentView` doesn't needs to handle gesture
|
|
open func cellContentView(canHandle touchPoint: CGPoint) -> Bool {
|
|
return false
|
|
}
|
|
}
|