mirror of
https://github.com/MessageKit/MessageKit.git
synced 2026-02-06 19:03:19 +00:00
Update Podspec and clean up code
This commit is contained in:
@@ -105,13 +105,13 @@ class ConversationViewController: MessagesViewController, MessagesDataSource, Me
|
||||
}
|
||||
|
||||
extension ConversationViewController: MessageInputBarDelegate {
|
||||
|
||||
|
||||
func sendButtonPressed(sender: UIButton) {
|
||||
// Maybe we should pass the UITextView or text with the sender?
|
||||
guard let message = messageInputBar.inputTextView.text else { return }
|
||||
|
||||
func sendButtonPressed(sender: UIButton, textView: UITextView) {
|
||||
|
||||
guard let message = textView.text else { return }
|
||||
|
||||
messages.append(MockMessage(text: message, sender: currentSender(), id: NSUUID().uuidString))
|
||||
|
||||
messagesCollectionView.reloadData()
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
Pod::Spec.new do |s|
|
||||
s.name = 'MessageKit'
|
||||
s.version = '0.0.1'
|
||||
s.version = '0.1.0'
|
||||
s.license = { :type => "MIT", :file => "LICENSE" }
|
||||
|
||||
s.summary = 'An elegant messages UI library for iOS.'
|
||||
|
||||
@@ -46,6 +46,6 @@ public struct Avatar {
|
||||
case false:
|
||||
return image ?? placeholderImage
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,6 +49,7 @@ open class MessageCollectionViewCell: UICollectionViewCell {
|
||||
}()
|
||||
|
||||
open let messageLabel: UILabel = {
|
||||
|
||||
let messageLabel = UILabel()
|
||||
messageLabel.numberOfLines = 0
|
||||
messageLabel.backgroundColor = .clear
|
||||
@@ -62,8 +63,6 @@ open class MessageCollectionViewCell: UICollectionViewCell {
|
||||
override public init(frame: CGRect) {
|
||||
super.init(frame: frame)
|
||||
setupSubviews()
|
||||
//setupConstraints()
|
||||
//contentView.backgroundColor = .purple
|
||||
}
|
||||
|
||||
required public init?(coder aDecoder: NSCoder) {
|
||||
@@ -73,15 +72,17 @@ open class MessageCollectionViewCell: UICollectionViewCell {
|
||||
// MARK: - Methods
|
||||
|
||||
private func setupSubviews() {
|
||||
|
||||
contentView.addSubview(messageContainerView)
|
||||
contentView.addSubview(avatarImageView)
|
||||
messageContainerView.addSubview(messageLabel)
|
||||
|
||||
}
|
||||
|
||||
override open func apply(_ layoutAttributes: UICollectionViewLayoutAttributes) {
|
||||
super.apply(layoutAttributes)
|
||||
|
||||
if let attributes = layoutAttributes as? MessagesCollectionViewLayoutAttributes {
|
||||
guard let attributes = layoutAttributes as? MessagesCollectionViewLayoutAttributes else { return }
|
||||
|
||||
messageLabel.font = attributes.messageFont
|
||||
|
||||
@@ -89,11 +90,10 @@ open class MessageCollectionViewCell: UICollectionViewCell {
|
||||
setMessageContainerFrameFor(attributes: attributes)
|
||||
setMessageLabelFor(attributes: attributes)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private func setMessageContainerFrameFor(attributes: MessagesCollectionViewLayoutAttributes) {
|
||||
|
||||
switch attributes.direction {
|
||||
case .incoming:
|
||||
let x = attributes.avatarSize.width + attributes.avatarContainerSpacing
|
||||
@@ -102,9 +102,11 @@ open class MessageCollectionViewCell: UICollectionViewCell {
|
||||
let x = contentView.frame.width - attributes.avatarSize.width - attributes.avatarContainerSpacing - attributes.messageContainerSize.width
|
||||
messageContainerView.frame = CGRect(x: x, y: 0, width: attributes.messageContainerSize.width, height: attributes.messageContainerSize.height)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private func setAvatarFrameFor(attributes: MessagesCollectionViewLayoutAttributes) {
|
||||
|
||||
switch attributes.direction {
|
||||
case .incoming:
|
||||
let y = frame.height - attributes.avatarSize.height - attributes.avatarBottomSpacing
|
||||
@@ -114,20 +116,25 @@ open class MessageCollectionViewCell: UICollectionViewCell {
|
||||
let x = contentView.frame.width - attributes.avatarSize.width
|
||||
avatarImageView.frame = CGRect(x: x, y: y, width: attributes.avatarSize.width, height: attributes.avatarSize.height)
|
||||
}
|
||||
|
||||
avatarImageView.layer.cornerRadius = avatarImageView.frame.width / 2
|
||||
|
||||
}
|
||||
|
||||
private func setMessageLabelFor(attributes: MessagesCollectionViewLayoutAttributes) {
|
||||
|
||||
let frame = CGRect(x: 0, y: 0, width: attributes.messageContainerSize.width, height: attributes.messageContainerSize.height)
|
||||
let insetFrame = UIEdgeInsetsInsetRect(frame, attributes.messageContainerInsets)
|
||||
messageLabel.frame = insetFrame
|
||||
|
||||
}
|
||||
|
||||
func configure(with message: MessageType) {
|
||||
|
||||
switch message.data {
|
||||
case .text(let text):
|
||||
messageLabel.text = text
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,10 +56,13 @@ open class MessageInputBar: UIView {
|
||||
|
||||
override public init(frame: CGRect) {
|
||||
super.init(frame: frame)
|
||||
|
||||
setupSubviews()
|
||||
setupConstraints()
|
||||
registerSelector()
|
||||
|
||||
backgroundColor = .inputBarGray
|
||||
|
||||
}
|
||||
|
||||
convenience public init() {
|
||||
@@ -101,6 +104,4 @@ open class MessageInputBar: UIView {
|
||||
func sendButtonPressed() {
|
||||
delegate?.sendButtonPressed(sender: sendButton, textView: inputTextView)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -42,5 +42,4 @@ open class MessagesCollectionView: UICollectionView {
|
||||
required public init?(coder aDecoder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -109,7 +109,6 @@ open class MessagesCollectionViewFlowLayout: UICollectionViewFlowLayout {
|
||||
attributes.avatarSize = avatarSize
|
||||
attributes.avatarBottomSpacing = avatarBottomSpacing
|
||||
attributes.avatarContainerSpacing = avatarContainerSpacing
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -128,7 +127,7 @@ extension MessagesCollectionViewFlowLayout {
|
||||
guard let collectionView = collectionView as? MessagesCollectionView, let dataSource = collectionView.messagesDataSource else { return .zero }
|
||||
|
||||
return dataSource.isFromCurrentSender(message: message) ? outgoingAvatarSize : incomingAvatarSize
|
||||
|
||||
|
||||
}
|
||||
|
||||
func minimumCellHeightFor(message: MessageType) -> CGFloat {
|
||||
@@ -143,7 +142,7 @@ extension MessagesCollectionViewFlowLayout {
|
||||
case .outgoing:
|
||||
return outgoingAvatarSize.height + avatarBottomSpacing
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
func containerHeightForMessage(message: MessageType) -> CGFloat {
|
||||
@@ -159,7 +158,7 @@ extension MessagesCollectionViewFlowLayout {
|
||||
let insets = messageContainerInsets.top + messageContainerInsets.bottom
|
||||
return estimatedHeight.rounded(.up) + insets //+ 1
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
func containerWidthForMessage(message: MessageType) -> CGFloat {
|
||||
@@ -178,14 +177,14 @@ extension MessagesCollectionViewFlowLayout {
|
||||
let finalWidth = estimatedWidth > availableWidth ? availableWidth : estimatedWidth
|
||||
return finalWidth + insets
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
func estimatedCellHeightForMessage(message: MessageType) -> CGFloat {
|
||||
|
||||
let messageContainerHeight = containerHeightForMessage(message: message)
|
||||
return messageContainerHeight
|
||||
|
||||
|
||||
}
|
||||
|
||||
func containerSizeFor(message: MessageType) -> CGSize {
|
||||
@@ -210,7 +209,6 @@ extension MessagesCollectionViewFlowLayout {
|
||||
return CGSize(width: itemWidth, height: actualHeight)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -57,6 +57,7 @@ final class MessagesCollectionViewLayoutAttributes: UICollectionViewLayoutAttrib
|
||||
}
|
||||
|
||||
override func isEqual(_ object: Any?) -> Bool {
|
||||
|
||||
// MARK: - LEAVE this as is
|
||||
if let _ = object as? MessagesCollectionViewLayoutAttributes {
|
||||
return super.isEqual(object)
|
||||
@@ -64,5 +65,4 @@ final class MessagesCollectionViewLayoutAttributes: UICollectionViewLayoutAttrib
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -40,7 +40,6 @@ public extension MessagesDataSource {
|
||||
func isFromCurrentSender(message: MessageType) -> Bool {
|
||||
return message.sender == currentSender()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -97,19 +97,24 @@ open class MessagesViewController: UIViewController {
|
||||
}
|
||||
|
||||
func handleKeyboardWillShow(_ notification: Notification) {
|
||||
|
||||
guard let keyboardSizeValue = notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue else { return }
|
||||
|
||||
let keyboardRect = keyboardSizeValue.cgRectValue
|
||||
let messageInputBarHeight = inputAccessoryView?.bounds.size.height ?? 0
|
||||
let keyboardHeight = keyboardRect.height - messageInputBarHeight
|
||||
collectionViewBottomConstraint.constant -= keyboardHeight
|
||||
|
||||
}
|
||||
|
||||
func handleKeyboardWillHide(_ notification: Notification) {
|
||||
guard let keyboardSizeValue = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue else { return }
|
||||
|
||||
let keyboardRect = keyboardSizeValue.cgRectValue
|
||||
let messageInputBarHeight = inputAccessoryView?.bounds.size.height ?? 0
|
||||
let keyboardHeight = keyboardRect.height - messageInputBarHeight
|
||||
collectionViewBottomConstraint.constant += keyboardHeight
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -132,15 +137,18 @@ extension MessagesViewController: UICollectionViewDataSource {
|
||||
|
||||
public func numberOfSections(in collectionView: UICollectionView) -> Int {
|
||||
guard let collectionView = collectionView as? MessagesCollectionView else { return 0 }
|
||||
|
||||
// Each message is its own section
|
||||
return collectionView.messagesDataSource?.numberOfMessages(in: collectionView) ?? 0
|
||||
}
|
||||
|
||||
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
||||
guard let collectionView = collectionView as? MessagesCollectionView else { return 0 }
|
||||
|
||||
let messageCount = collectionView.messagesDataSource?.numberOfMessages(in: collectionView) ?? 0
|
||||
// There will only ever be 1 message per section
|
||||
return messageCount > 0 ? 1 : 0
|
||||
|
||||
}
|
||||
|
||||
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
||||
@@ -161,11 +169,10 @@ extension MessagesViewController: UICollectionViewDataSource {
|
||||
cell.configure(with: message)
|
||||
|
||||
}
|
||||
|
||||
|
||||
return cell
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -27,15 +27,18 @@ import Foundation
|
||||
extension String {
|
||||
|
||||
func height(considering width: CGFloat, and font: UIFont) -> CGFloat {
|
||||
|
||||
let constraintBox = CGSize(width: width, height: .greatestFiniteMagnitude)
|
||||
let boundRect = self.boundingRect(with: constraintBox, options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)
|
||||
return boundRect.height
|
||||
|
||||
}
|
||||
|
||||
func width(considering height: CGFloat, and font: UIFont) -> CGFloat {
|
||||
|
||||
let constraintBox = CGSize(width: .greatestFiniteMagnitude, height: height)
|
||||
let boundRect = self.boundingRect(with: constraintBox, options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)
|
||||
return boundRect.width
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user