From db2e92929688bf3ec6be0c19ceea7bd88e909435 Mon Sep 17 00:00:00 2001 From: Steven Deutsch Date: Thu, 27 Jul 2017 02:13:57 -0500 Subject: [PATCH] Update Podspec and clean up code --- .../Sources/ConversationViewController.swift | 10 +++++----- MessageKit.podspec | 2 +- Sources/Avatar.swift | 2 +- Sources/MessageCollectionViewCell.swift | 19 +++++++++++++------ Sources/MessageInputBar.swift | 5 +++-- Sources/MessagesCollectionView.swift | 1 - .../MessagesCollectionViewFlowLayout.swift | 12 +++++------- ...ssagesCollectionViewLayoutAttributes.swift | 2 +- Sources/MessagesDataSource.swift | 1 - Sources/MessagesViewController.swift | 11 +++++++++-- Sources/String+Extensions.swift | 5 ++++- 11 files changed, 42 insertions(+), 28 deletions(-) diff --git a/Example/Sources/ConversationViewController.swift b/Example/Sources/ConversationViewController.swift index a5778f10..c8eb2113 100644 --- a/Example/Sources/ConversationViewController.swift +++ b/Example/Sources/ConversationViewController.swift @@ -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() } diff --git a/MessageKit.podspec b/MessageKit.podspec index 8f42c3ba..bf36da5c 100644 --- a/MessageKit.podspec +++ b/MessageKit.podspec @@ -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.' diff --git a/Sources/Avatar.swift b/Sources/Avatar.swift index 5fdee88d..fd393ad8 100644 --- a/Sources/Avatar.swift +++ b/Sources/Avatar.swift @@ -46,6 +46,6 @@ public struct Avatar { case false: return image ?? placeholderImage } - + } } diff --git a/Sources/MessageCollectionViewCell.swift b/Sources/MessageCollectionViewCell.swift index d9108e6c..223fc22e 100644 --- a/Sources/MessageCollectionViewCell.swift +++ b/Sources/MessageCollectionViewCell.swift @@ -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 } - } + } } diff --git a/Sources/MessageInputBar.swift b/Sources/MessageInputBar.swift index 746bbce4..670d2bf7 100644 --- a/Sources/MessageInputBar.swift +++ b/Sources/MessageInputBar.swift @@ -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) } - - } diff --git a/Sources/MessagesCollectionView.swift b/Sources/MessagesCollectionView.swift index 91c1043d..505cc842 100644 --- a/Sources/MessagesCollectionView.swift +++ b/Sources/MessagesCollectionView.swift @@ -42,5 +42,4 @@ open class MessagesCollectionView: UICollectionView { required public init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } - } diff --git a/Sources/MessagesCollectionViewFlowLayout.swift b/Sources/MessagesCollectionViewFlowLayout.swift index 8e1b19d4..f0b2fc12 100644 --- a/Sources/MessagesCollectionViewFlowLayout.swift +++ b/Sources/MessagesCollectionViewFlowLayout.swift @@ -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) } - } diff --git a/Sources/MessagesCollectionViewLayoutAttributes.swift b/Sources/MessagesCollectionViewLayoutAttributes.swift index 89b3bfe8..4c84a25b 100644 --- a/Sources/MessagesCollectionViewLayoutAttributes.swift +++ b/Sources/MessagesCollectionViewLayoutAttributes.swift @@ -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 } } - } diff --git a/Sources/MessagesDataSource.swift b/Sources/MessagesDataSource.swift index 9c6348d1..b306d23b 100644 --- a/Sources/MessagesDataSource.swift +++ b/Sources/MessagesDataSource.swift @@ -40,7 +40,6 @@ public extension MessagesDataSource { func isFromCurrentSender(message: MessageType) -> Bool { return message.sender == currentSender() } - } diff --git a/Sources/MessagesViewController.swift b/Sources/MessagesViewController.swift index 7b890b10..6c610b9b 100644 --- a/Sources/MessagesViewController.swift +++ b/Sources/MessagesViewController.swift @@ -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 } - } diff --git a/Sources/String+Extensions.swift b/Sources/String+Extensions.swift index a75a7653..dfdf557a 100644 --- a/Sources/String+Extensions.swift +++ b/Sources/String+Extensions.swift @@ -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 + } - }