Skip to content

Commit

Permalink
Make MessageCollectionViewCell generic over its content
Browse files Browse the repository at this point in the history
  • Loading branch information
SD10 committed Sep 15, 2017
1 parent e2e0894 commit 2328326
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 31 deletions.
16 changes: 8 additions & 8 deletions Sources/MessageCellDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,24 @@ import Foundation

public protocol MessageCellDelegate: class, MessageLabelDelegate {

func didTapMessage(in cell: MessageCollectionViewCell)
func didTapMessage<T: UIView>(in cell: MessageCollectionViewCell<T>)

func didTapAvatar(in cell: MessageCollectionViewCell)
func didTapAvatar<T: UIView>(in cell: MessageCollectionViewCell<T>)

func didTapBottomLabel(in cell: MessageCollectionViewCell)
func didTapBottomLabel<T: UIView>(in cell: MessageCollectionViewCell<T>)

func didTapTopLabel(in cell: MessageCollectionViewCell)
func didTapTopLabel<T: UIView>(in cell: MessageCollectionViewCell<T>)

}

public extension MessageCellDelegate {

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

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

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

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

}
33 changes: 11 additions & 22 deletions Sources/MessageCollectionViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import UIKit

open class MessageCollectionViewCell: UICollectionViewCell {
open class MessageCollectionViewCell<ContentView: UIView>: UICollectionViewCell {

// MARK: - Properties

Expand All @@ -38,19 +38,17 @@ open class MessageCollectionViewCell: UICollectionViewCell {
return topLabel
}()

open var messageLabel: MessageLabel = MessageLabel()
open var messageContentView = ContentView()

open var cellBottomLabel: MessageLabel = {
let bottomLabel = MessageLabel()
bottomLabel.enabledDetectors = []
return bottomLabel
}()

open weak var delegate: MessageCellDelegate? {
didSet {
messageLabel.delegate = delegate
}
}
open weak var delegate: MessageCellDelegate?

var messageTapGesture: UITapGestureRecognizer?

// MARK: - Initializer

Expand All @@ -71,7 +69,7 @@ open class MessageCollectionViewCell: UICollectionViewCell {

contentView.addSubview(cellTopLabel)
contentView.addSubview(messageContainerView)
messageContainerView.addSubview(messageLabel)
messageContainerView.addSubview(messageContentView)
contentView.addSubview(avatarView)
contentView.addSubview(cellBottomLabel)

Expand All @@ -85,8 +83,8 @@ open class MessageCollectionViewCell: UICollectionViewCell {
avatarView.frame = attributes.avatarFrame

messageContainerView.frame = attributes.messageContainerFrame
messageLabel.frame = CGRect(origin: .zero, size: attributes.messageContainerFrame.size)
messageLabel.textInsets = attributes.messageLabelInsets
messageContentView.frame = CGRect(origin: .zero, size: attributes.messageContainerFrame.size)
//messageContentView.textInsets = attributes.messageLabelInsets

cellTopLabel.frame = attributes.cellTopLabelFrame
cellTopLabel.textInsets = attributes.cellTopLabelInsets
Expand All @@ -97,34 +95,25 @@ open class MessageCollectionViewCell: UICollectionViewCell {
}

override open func prepareForReuse() {
messageLabel.text = nil
messageLabel.attributedText = nil
cellTopLabel.text = nil
cellTopLabel.attributedText = nil
cellBottomLabel.text = nil
cellBottomLabel.attributedText = nil
}

public func configure(with message: MessageType) {

switch message.data {
case .text(let text):
messageLabel.text = text
case .attributedText(let text):
messageLabel.attributedText = text
}

// Provide in subclass
}

private func setupGestureRecognizers() {
func setupGestureRecognizers() {

let avatarTapGesture = UITapGestureRecognizer(target: self, action: #selector(didTapAvatar))
avatarView.addGestureRecognizer(avatarTapGesture)
avatarView.isUserInteractionEnabled = true

let messageTapGesture = UITapGestureRecognizer(target: self, action: #selector(didTapMessage))
messageContainerView.addGestureRecognizer(messageTapGesture)
messageTapGesture.delegate = messageLabel
self.messageTapGesture = messageTapGesture

let topLabelTapGesture = UITapGestureRecognizer(target: self, action: #selector(didTapTopLabel))
cellTopLabel.addGestureRecognizer(topLabelTapGesture)
Expand Down
2 changes: 1 addition & 1 deletion Sources/MessagesViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ extension MessagesViewController: UICollectionViewDataSource {
let messageStyle = displayDelegate.messageStyle(for: message, at: indexPath, in: messagesCollectionView)
let textColor = displayDelegate.textColor(for: message, at: indexPath, in: messagesCollectionView)

cell.messageLabel.textColor = textColor
//cell.messageLabel.textColor = textColor
cell.messageContainerView.messageColor = messageColor
cell.messageContainerView.style = messageStyle

Expand Down

0 comments on commit 2328326

Please sign in to comment.