Skip to content

Commit

Permalink
Generic way
Browse files Browse the repository at this point in the history
  • Loading branch information
SD10 committed Sep 15, 2017
1 parent 0acc641 commit 468f07b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 24 deletions.
8 changes: 8 additions & 0 deletions Sources/MessageCollectionViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ open class MessageCollectionViewCell<ContentView: UIView>: UICollectionViewCell
cellBottomLabel.attributedText = nil
}

func setAvatar(_ avatar: Avatar) {

}

func setCellLabels(bottomText: NSAttributedString, topText: NSAttributedString) {

}

public func configure(with message: MessageType) {
// Provide in subclass
}
Expand Down
62 changes: 38 additions & 24 deletions Sources/MessagesViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -172,42 +172,56 @@ extension MessagesViewController: UICollectionViewDataSource {

}

public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MessageCell", for: indexPath) as? MessageCollectionViewCell ?? MessageCollectionViewCell()

guard let messagesCollectionView = collectionView as? MessagesCollectionView else { return cell }

func configureMessageCell<T: UIView>(cell: MessageCollectionViewCell<T>) {
if let cellDelegate = messagesCollectionView.messageCellDelegate {
if cell.delegate == nil { cell.delegate = cellDelegate }
}
}

func configureTextCell(cell: TextMessageCell) {
configureMessageCell(cell: cell)
}

public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

guard let messagesDataSource = messagesCollectionView.messagesDataSource else { return cell }
guard let messagesCollectionView = collectionView as? MessagesCollectionView else { return UICollectionViewCell() }
guard let messagesDataSource = messagesCollectionView.messagesDataSource else { fatalError("Please set messagesDataSource") }

let message = messagesDataSource.messageForItem(at: indexPath, in: messagesCollectionView)
let avatar = messagesDataSource.avatar(for: message, at: indexPath, in: messagesCollectionView)
let topLabelText = messagesDataSource.cellTopLabelAttributedText(for: message, at: indexPath)
let bottomLabelText = messagesDataSource.cellBottomLabelAttributedText(for: message, at: indexPath)

if let displayDelegate = messagesCollectionView.messagesDisplayDelegate {
func configure<T: UIView>(_ cell: MessageCollectionViewCell<T>) {
if let cellDelegate = messagesCollectionView.messageCellDelegate {
if cell.delegate == nil { cell.delegate = cellDelegate }
}

let avatar = messagesDataSource.avatar(for: message, at: indexPath, in: messagesCollectionView)
let topLabelText = messagesDataSource.cellTopLabelAttributedText(for: message, at: indexPath)
let bottomLabelText = messagesDataSource.cellBottomLabelAttributedText(for: message, at: indexPath)

let messageColor = displayDelegate.backgroundColor(for: message, at: indexPath, in: messagesCollectionView)
let messageStyle = displayDelegate.messageStyle(for: message, at: indexPath, in: messagesCollectionView)
let textColor = displayDelegate.textColor(for: message, at: indexPath, in: messagesCollectionView)
if let displayDelegate = messagesCollectionView.messagesDisplayDelegate {

//cell.messageLabel.textColor = textColor
cell.messageContainerView.messageColor = messageColor
cell.messageContainerView.style = messageStyle
let messageColor = displayDelegate.backgroundColor(for: message, at: indexPath, in: messagesCollectionView)
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.messageContainerView.messageColor = messageColor
cell.messageContainerView.style = messageStyle

// Must be set after configuring displayDelegate properties
cell.avatarView.set(avatar: avatar)
cell.cellTopLabel.attributedText = topLabelText
cell.cellBottomLabel.attributedText = bottomLabelText
cell.configure(with: message)
}

// Must be set after configuring displayDelegate properties
cell.avatarView.set(avatar: avatar)
cell.cellTopLabel.attributedText = topLabelText
cell.cellBottomLabel.attributedText = bottomLabelText
}

return cell
switch message.data {
case .text, .attributedText:
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MessageCell", for: indexPath) as? TextMessageCell else { return UICollectionViewCell() }
configure(cell)
return cell
}

}

Expand Down

0 comments on commit 468f07b

Please sign in to comment.