Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removing cell generic constraints #391

Merged
merged 5 commits into from
Dec 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ The changelog for `MessageKit`. Also see the [releases](https://github.com/Messa

### Changed

- **Breaking Change** Removed the generic constraint `<ContentView: UIView>` from `MessageCollectionViewCell`.
[#391](https://github.com/MessageKit/MessageKit/pull/391) by [@SD10](https://github.com/sd10).

- **Breaking Change** The `configure(message:indexPath:messagesCollectionView)` method of `LocationMessageCell`,
`MediaMessageCell`, `TextMessageCell`, and `MessageCollectionViewCell` has been replaced by methods that take the
delegate return values as arguments.
[#391](https://github.com/MessageKit/MessageKit/pull/391) by [@SD10](https://github.com/sd10).

- **Breaking Change** The `contentView` property has been renamed to `imageView` for `LocationMessageCell` and `MediaMessageCell`
and `messageLabel` for `TextMessageCell`.
[#391](https://github.com/MessageKit/MessageKit/pull/391) by [@SD10](https://github.com/sd10).

- **Breaking Change** Changed the name of `MessageInputBar`'s property `maxHeight` to `maxTextViewHeight` as the property is the max height the `InputTextView` can have, not the `MessageInputBar` itself.
[#380](https://github.com/MessageKit/MessageKit/pull/380) by [@nathantannar4](https://github.com/nathantannar4).

Expand Down
10 changes: 5 additions & 5 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
PODS:
- MessageKit (0.10.2)
- MessageKit (0.11.0)

DEPENDENCIES:
- MessageKit (from `../MessageKit.podspec`)
- MessageKit (from `../`)

EXTERNAL SOURCES:
MessageKit:
:path: ../MessageKit.podspec
:path: ../

SPEC CHECKSUMS:
MessageKit: a1e8d0f1d7785891e4b37f6252898f23cd8f8b6a
MessageKit: 96caa3ba2c32f9393b406256c5caa29ae9189d71

PODFILE CHECKSUM: 9ac65b8dedf0e1b63fea245b089b6645c4e66309
PODFILE CHECKSUM: cecdb7bc8129cf99f66de9f68eea3256fec30c3d

COCOAPODS: 1.3.1
12 changes: 8 additions & 4 deletions Example/Sources/ConversationViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,10 @@ extension ConversationViewController: MessagesDisplayDelegate {

extension ConversationViewController: MessagesLayoutDelegate {

func enabledDetectors(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> [DetectorType] {
return [.url, .address, .phoneNumber, .date]
}

func avatarPosition(for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> AvatarPosition {
return AvatarPosition(horizontal: .natural, vertical: .messageBottom)
}
Expand Down Expand Up @@ -372,19 +376,19 @@ extension ConversationViewController: MessagesLayoutDelegate {

extension ConversationViewController: MessageCellDelegate {

func didTapAvatar<T>(in cell: MessageCollectionViewCell<T>) {
func didTapAvatar(in cell: MessageCollectionViewCell) {
print("Avatar tapped")
}

func didTapMessage<T>(in cell: MessageCollectionViewCell<T>) {
func didTapMessage(in cell: MessageCollectionViewCell) {
print("Message tapped")
}

func didTapTopLabel<T>(in cell: MessageCollectionViewCell<T>) {
func didTapTopLabel(in cell: MessageCollectionViewCell) {
print("Top label tapped")
}

func didTapBottomLabel<T>(in cell: MessageCollectionViewCell<T>) {
func didTapBottomLabel(in cell: MessageCollectionViewCell) {
print("Bottom label tapped")
}

Expand Down
40 changes: 30 additions & 10 deletions Sources/Controllers/MessagesViewController.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

/*
MIT License

Expand Down Expand Up @@ -222,23 +223,47 @@ extension MessagesViewController: UICollectionViewDataSource {
fatalError("MessagesDataSource has not been set.")
}

guard let displayDelegate = messagesCollectionView.messagesDisplayDelegate else {
fatalError("MessagesDisplayDelegate has not been set.")
}

let message = messagesDataSource.messageForItem(at: indexPath, in: messagesCollectionView)
let avatar = messagesDataSource.avatar(for: message, at: indexPath, in: messagesCollectionView)
let bottomText = messagesDataSource.cellBottomLabelAttributedText(for: message, at: indexPath)
let topText = messagesDataSource.cellTopLabelAttributedText(for: message, at: indexPath)
let style = displayDelegate.messageStyle(for: message, at: indexPath, in: messagesCollectionView)
let backgroundColor = displayDelegate.backgroundColor(for: message, at: indexPath, in: messagesCollectionView)

let commonConfigure = { (cell: MessageCollectionViewCell) in
cell.messageContainerView.style = style
cell.messageContainerView.backgroundColor = backgroundColor
cell.configureAvatar(avatar)
cell.configureAccessoryLabels(topText, bottomText)
cell.delegate = messagesCollectionView.messageCellDelegate
}

switch message.data {
case .text, .attributedText, .emoji:
let cell = messagesCollectionView.dequeueReusableCell(TextMessageCell.self, for: indexPath)
cell.configure(with: message, at: indexPath, and: messagesCollectionView)
let detectors = displayDelegate.enabledDetectors(for: message, at: indexPath, in: messagesCollectionView)
let textColor = displayDelegate.textColor(for: message, at: indexPath, in: messagesCollectionView)
cell.configure(message, textColor, detectors)
commonConfigure(cell)
return cell
case .photo, .video:
let cell = messagesCollectionView.dequeueReusableCell(MediaMessageCell.self, for: indexPath)
cell.configure(with: message, at: indexPath, and: messagesCollectionView)
cell.configure(message)
commonConfigure(cell)
return cell
case .location:
case .location(let location):
let cell = messagesCollectionView.dequeueReusableCell(LocationMessageCell.self, for: indexPath)
cell.configure(with: message, at: indexPath, and: messagesCollectionView)
let options = displayDelegate.snapshotOptionsForLocation(message: message, at: indexPath, in: messagesCollectionView)
let annotationView = displayDelegate.annotationViewForLocation(message: message, at: indexPath, in: messagesCollectionView)
let animationBlock = displayDelegate.animationBlockForLocation(message: message, at: indexPath, in: messagesCollectionView)
cell.configure(location, options, annotationView, animationBlock)
commonConfigure(cell)
return cell
}

}

open func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
Expand All @@ -265,9 +290,7 @@ extension MessagesViewController: UICollectionViewDataSource {
default:
fatalError("Unrecognized element of kind: \(kind)")
}

}

}

// MARK: - Keyboard Handling
Expand All @@ -288,7 +311,6 @@ fileprivate extension MessagesViewController {

@objc
func handleTextViewDidBeginEditing(_ notification: Notification) {

if scrollsToBottomOnKeybordBeginsEditing {
guard let inputTextView = notification.object as? InputTextView, inputTextView === messageInputBar.inputTextView else { return }
messagesCollectionView.scrollToBottom(animated: true)
Expand All @@ -297,7 +319,6 @@ fileprivate extension MessagesViewController {

@objc
func handleKeyboardDidChangeState(_ notification: Notification) {

guard let keyboardEndFrame = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? CGRect else { return }

if (keyboardEndFrame.origin.y + keyboardEndFrame.size.height) > UIScreen.main.bounds.height {
Expand All @@ -312,7 +333,6 @@ fileprivate extension MessagesViewController {
messagesCollectionView.contentInset.bottom = bottomInset
messagesCollectionView.scrollIndicatorInsets.bottom = bottomInset
}

}

fileprivate var keyboardOffsetFrame: CGRect {
Expand Down
12 changes: 12 additions & 0 deletions Sources/Extensions/UIView+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ extension UIView {
NSLayoutConstraint.activate(constraints)
}

func centerInSuperview() {
guard let superview = self.superview else {
return
}
translatesAutoresizingMaskIntoConstraints = false
let constraints: [NSLayoutConstraint] = [
centerXAnchor.constraint(equalTo: superview.centerXAnchor),
centerYAnchor.constraint(equalTo: superview.centerYAnchor)
]
NSLayoutConstraint.activate(constraints)
}

@discardableResult
func addConstraints(_ top: NSLayoutYAxisAnchor? = nil, left: NSLayoutXAxisAnchor? = nil, bottom: NSLayoutYAxisAnchor? = nil, right: NSLayoutXAxisAnchor? = nil, topConstant: CGFloat = 0, leftConstant: CGFloat = 0, bottomConstant: CGFloat = 0, rightConstant: CGFloat = 0, widthConstant: CGFloat = 0, heightConstant: CGFloat = 0) -> [NSLayoutConstraint] {

Expand Down
16 changes: 8 additions & 8 deletions Sources/Protocols/MessageCellDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public protocol MessageCellDelegate: MessageLabelDelegate {
/// You can get a reference to the `MessageType` for the cell by using `UICollectionView`'s
/// `indexPath(for: cell)` method. Then using the returned `IndexPath` with the `MessagesDataSource`
/// method `messageForItem(at:indexPath:messagesCollectionView)`.
func didTapMessage<T>(in cell: MessageCollectionViewCell<T>)
func didTapMessage(in cell: MessageCollectionViewCell)

/// Triggered when a touch occurs in the `AvatarView`.
///
Expand All @@ -45,7 +45,7 @@ public protocol MessageCellDelegate: MessageLabelDelegate {
/// You can get a reference to the `MessageType` for the cell by using `UICollectionView`'s
/// `indexPath(for: cell)` method. Then using the returned `IndexPath` with the `MessagesDataSource`
/// method `messageForItem(at:indexPath:messagesCollectionView)`.
func didTapAvatar<T>(in cell: MessageCollectionViewCell<T>)
func didTapAvatar(in cell: MessageCollectionViewCell)

/// Triggered when a touch occurs in the cellBottomLabel.
///
Expand All @@ -55,7 +55,7 @@ public protocol MessageCellDelegate: MessageLabelDelegate {
/// You can get a reference to the `MessageType` for the cell by using `UICollectionView`'s
/// `indexPath(for: cell)` method. Then using the returned `IndexPath` with the `MessagesDataSource`
/// method `messageForItem(at:indexPath:messagesCollectionView)`.
func didTapBottomLabel<T>(in cell: MessageCollectionViewCell<T>)
func didTapBottomLabel(in cell: MessageCollectionViewCell)

/// Triggered when a touch occurs in the cellTopLabel.
///
Expand All @@ -65,18 +65,18 @@ public protocol MessageCellDelegate: MessageLabelDelegate {
/// You can get a reference to the `MessageType` for the cell by using `UICollectionView`'s
/// `indexPath(for: cell)` method. Then using the returned `IndexPath` with the `MessagesDataSource`
/// method `messageForItem(at:indexPath:messagesCollectionView)`.
func didTapTopLabel<T>(in cell: MessageCollectionViewCell<T>)
func didTapTopLabel(in cell: MessageCollectionViewCell)

}

public extension MessageCellDelegate {

func didTapMessage<T>(in cell: MessageCollectionViewCell<T>) {}
func didTapMessage(in cell: MessageCollectionViewCell) {}

func didTapAvatar<T>(in cell: MessageCollectionViewCell<T>) {}
func didTapAvatar(in cell: MessageCollectionViewCell) {}

func didTapBottomLabel<T>(in cell: MessageCollectionViewCell<T>) {}
func didTapBottomLabel(in cell: MessageCollectionViewCell) {}

func didTapTopLabel<T>(in cell: MessageCollectionViewCell<T>) {}
func didTapTopLabel(in cell: MessageCollectionViewCell) {}

}
41 changes: 16 additions & 25 deletions Sources/Views/Cells/LocationMessageCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,39 +25,31 @@
import UIKit
import MapKit

open class LocationMessageCell: MessageCollectionViewCell<UIImageView> {
open class LocationMessageCell: MessageCollectionViewCell {

open override class func reuseIdentifier() -> String { return "messagekit.cell.location" }

// MARK: - Properties

open var activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .gray)

open override func configure(with message: MessageType, at indexPath: IndexPath, and messagesCollectionView: MessagesCollectionView) {
super.configure(with: message, at: indexPath, and: messagesCollectionView)

switch message.data {
case .location(let location):
guard let displayDelegate = messagesCollectionView.messagesDisplayDelegate else { return }
let options = displayDelegate.snapshotOptionsForLocation(message: message, at: indexPath, in: messagesCollectionView)
let annotationView = displayDelegate.annotationViewForLocation(message: message, at: indexPath, in: messagesCollectionView)
let animationBlock = displayDelegate.animationBlockForLocation(message: message, at: indexPath, in: messagesCollectionView)
setMapSnaphotImage(for: location, annotationView: annotationView, options: options, animation: animationBlock)
default:
break
}
open var imageView = UIImageView()

open func configure(_ location: CLLocation, _ options: LocationMessageSnapshotOptions, _ annotationView: MKAnnotationView?, _ animationBlock: ((UIImageView) -> Void)?) {

setMapSnaphotImage(for: location, annotationView: annotationView, options: options, animation: animationBlock)
}

override func setupSubviews() {
open override func setupSubviews() {
super.setupSubviews()
messageContentView.addSubview(activityIndicator)
messageContainerView.addSubview(imageView)
messageContainerView.addSubview(activityIndicator)
setupConstraints()
}

private func setupConstraints() {
activityIndicator.translatesAutoresizingMaskIntoConstraints = false
let centerX = activityIndicator.centerXAnchor.constraint(equalTo: messageContentView.centerXAnchor)
let centerY = activityIndicator.centerYAnchor.constraint(equalTo: messageContentView.centerYAnchor)
NSLayoutConstraint.activate([centerX, centerY])
open func setupConstraints() {
imageView.fillSuperview()
activityIndicator.centerInSuperview()
}

open func setMapSnaphotImage(for location: CLLocation, annotationView: MKAnnotationView?, options: LocationMessageSnapshotOptions, animation: ((UIImageView) -> Void)?) {
Expand All @@ -66,7 +58,6 @@ open class LocationMessageCell: MessageCollectionViewCell<UIImageView> {

let snapshotOptions = MKMapSnapshotOptions()
snapshotOptions.region = MKCoordinateRegion(center: location.coordinate, span: options.span)
snapshotOptions.size = messageContainerView.frame.size
snapshotOptions.showsBuildings = options.showsBuildings
snapshotOptions.showsPointsOfInterest = options.showsPointsOfInterest

Expand All @@ -81,7 +72,7 @@ open class LocationMessageCell: MessageCollectionViewCell<UIImageView> {
}

guard let annotationView = annotationView else {
self.messageContentView.image = snapshot.image
self.imageView.image = snapshot.image
return
}

Expand All @@ -100,8 +91,8 @@ open class LocationMessageCell: MessageCollectionViewCell<UIImageView> {
let composedImage = UIGraphicsGetImageFromCurrentImageContext()

UIGraphicsEndImageContext()
self.messageContentView.image = composedImage
animation?(self.messageContentView)
self.imageView.image = composedImage
animation?(self.imageView)
}
}
}
30 changes: 13 additions & 17 deletions Sources/Views/Cells/MediaMessageCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@

import UIKit

open class MediaMessageCell: MessageCollectionViewCell<UIImageView> {
open class MediaMessageCell: MessageCollectionViewCell {

open override class func reuseIdentifier() -> String { return "messagekit.cell.mediamessage" }

// MARK: - Properties
Expand All @@ -35,37 +36,32 @@ open class MediaMessageCell: MessageCollectionViewCell<UIImageView> {
return playButtonView
}()

// MARK: - Methods

private func setupConstraints() {
playButtonView.translatesAutoresizingMaskIntoConstraints = false
open var imageView = UIImageView()

let centerX = playButtonView.centerXAnchor.constraint(equalTo: messageContainerView.centerXAnchor)
let centerY = playButtonView.centerYAnchor.constraint(equalTo: messageContainerView.centerYAnchor)
let width = playButtonView.widthAnchor.constraint(equalToConstant: playButtonView.bounds.width)
let height = playButtonView.heightAnchor.constraint(equalToConstant: playButtonView.bounds.height)
// MARK: - Methods

NSLayoutConstraint.activate([centerX, centerY, width, height])
open func setupConstraints() {
imageView.fillSuperview()
playButtonView.centerInSuperview()
}

override func setupSubviews() {
open override func setupSubviews() {
super.setupSubviews()
messageContentView.addSubview(playButtonView)
messageContainerView.addSubview(imageView)
messageContainerView.addSubview(playButtonView)
setupConstraints()
}

open override func configure(with message: MessageType, at indexPath: IndexPath, and messagesCollectionView: MessagesCollectionView) {
super.configure(with: message, at: indexPath, and: messagesCollectionView)
open func configure(_ message: MessageType) {
switch message.data {
case .photo(let image):
messageContentView.image = image
imageView.image = image
playButtonView.isHidden = true
case .video(_, let image):
messageContentView.image = image
imageView.image = image
playButtonView.isHidden = false
default:
break
}
}

}
Loading