Skip to content

Commit

Permalink
Refactor cell configuration method to cleanup cellForItem(at:IndexPath)
Browse files Browse the repository at this point in the history
  • Loading branch information
SD10 committed Sep 16, 2017
1 parent 100b8d1 commit 9a396d0
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 40 deletions.
4 changes: 4 additions & 0 deletions MessageKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
B015E8191F24623D007EDFB6 /* MessagesCollectionViewLayoutAttributes.swift in Sources */ = {isa = PBXBuildFile; fileRef = B015E8181F24623D007EDFB6 /* MessagesCollectionViewLayoutAttributes.swift */; };
B015E81F1F259D8E007EDFB6 /* MessageInputBarDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B015E81E1F259D8E007EDFB6 /* MessageInputBarDelegate.swift */; };
B01E2DD81F5BBDB800E4FA1C /* MessageStyle.swift in Sources */ = {isa = PBXBuildFile; fileRef = B01E2DD71F5BBDB800E4FA1C /* MessageStyle.swift */; };
B0291DA91F6DBB9F00BEDF03 /* TextMessageCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0291DA81F6DBB9F00BEDF03 /* TextMessageCell.swift */; };
B03FF9AF1F31BB1200754FE5 /* MessageCellDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B03FF9AE1F31BB1200754FE5 /* MessageCellDelegate.swift */; };
B05530B51F493CFA008BB420 /* DetectorType.swift in Sources */ = {isa = PBXBuildFile; fileRef = B05530B41F493CFA008BB420 /* DetectorType.swift */; };
B0655A2A1F23D77200542A83 /* Sender.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0655A291F23D77200542A83 /* Sender.swift */; };
Expand Down Expand Up @@ -128,6 +129,7 @@
B015E8181F24623D007EDFB6 /* MessagesCollectionViewLayoutAttributes.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MessagesCollectionViewLayoutAttributes.swift; sourceTree = "<group>"; };
B015E81E1F259D8E007EDFB6 /* MessageInputBarDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MessageInputBarDelegate.swift; sourceTree = "<group>"; };
B01E2DD71F5BBDB800E4FA1C /* MessageStyle.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MessageStyle.swift; sourceTree = "<group>"; };
B0291DA81F6DBB9F00BEDF03 /* TextMessageCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TextMessageCell.swift; sourceTree = "<group>"; };
B03FF9AE1F31BB1200754FE5 /* MessageCellDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MessageCellDelegate.swift; sourceTree = "<group>"; };
B05530B41F493CFA008BB420 /* DetectorType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DetectorType.swift; sourceTree = "<group>"; };
B0655A291F23D77200542A83 /* Sender.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Sender.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -299,6 +301,7 @@
B074EEA71F3971A600ABB8C8 /* MessageLabel.swift */,
E8586DB41F59A1C300C9BE9D /* MessageContainerView.swift */,
B0147C8F1F5ED0810035B36E /* MessageDateHeaderView.swift */,
B0291DA81F6DBB9F00BEDF03 /* TextMessageCell.swift */,
);
name = Views;
sourceTree = "<group>";
Expand Down Expand Up @@ -522,6 +525,7 @@
B05530B51F493CFA008BB420 /* DetectorType.swift in Sources */,
B01E2DD81F5BBDB800E4FA1C /* MessageStyle.swift in Sources */,
88916B471CF0DFE600469F91 /* MessageType.swift in Sources */,
B0291DA91F6DBB9F00BEDF03 /* TextMessageCell.swift in Sources */,
B0AA1F511F42E91A00BAE583 /* AvatarAlignment.swift in Sources */,
B03FF9AF1F31BB1200754FE5 /* MessageCellDelegate.swift in Sources */,
37C936981F38F6AC00853DF2 /* Avatar.swift in Sources */,
Expand Down
26 changes: 23 additions & 3 deletions Sources/MessageCollectionViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ open class MessageCollectionViewCell<ContentView: UIView>: UICollectionViewCell

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

cellTopLabel.frame = attributes.cellTopLabelFrame
cellTopLabel.textInsets = attributes.cellTopLabelInsets
Expand All @@ -101,8 +100,29 @@ open class MessageCollectionViewCell<ContentView: UIView>: UICollectionViewCell
cellBottomLabel.attributedText = nil
}

public func configure(with message: MessageType) {
// Provide in subclass
public func configure(with message: MessageType, at indexPath: IndexPath, and messagesCollectionView: MessagesCollectionView) {
if let displayDelegate = messagesCollectionView.messagesDisplayDelegate {

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

messageContainerView.messageColor = messageColor
messageContainerView.style = messageStyle
}

// Make sure we set all data source properties after configuring display delegate properties
// The MessageLabel class probably has a stateful issue
if let dataSource = messagesCollectionView.messagesDataSource {

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

avatarView.set(avatar: avatar)
cellTopLabel.attributedText = topLabelText
cellBottomLabel.attributedText = bottomLabelText
}

}

func setupGestureRecognizers() {
Expand Down
39 changes: 5 additions & 34 deletions Sources/MessagesViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ open class MessagesViewController: UIViewController {
messagesCollectionView.topAnchor.constraint(equalTo: view.topAnchor),
messagesCollectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
messagesCollectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor),

messagesCollectionView.bottomAnchor.constraint(equalTo: bottomLayoutGuide.topAnchor),
messagesCollectionView.bottomAnchor.constraint(equalTo: bottomLayoutGuide.topAnchor)
]
NSLayoutConstraint.activate(constraints)
}
Expand Down Expand Up @@ -189,40 +188,12 @@ extension MessagesViewController: UICollectionViewDataSource {

let message = messagesDataSource.messageForItem(at: indexPath, in: messagesCollectionView)

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)

if let displayDelegate = messagesCollectionView.messagesDisplayDelegate {

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



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
}

switch message.data {
case .text, .attributedText:
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TextMessageCell", for: indexPath) as? TextMessageCell else { return UICollectionViewCell() }
let textColor = messagesCollectionView.messagesDisplayDelegate?.textColor(for: message, at: indexPath, in: messagesCollectionView)
cell.messageContentView.textColor = textColor
cell.configure(with: message)
configure(cell)
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TextMessageCell", for: indexPath) as? TextMessageCell else {
fatalError("Unable to dequeue TextMessageCell")
}
cell.configure(with: message, at: indexPath, and: messagesCollectionView)
return cell
}

Expand Down
11 changes: 9 additions & 2 deletions TextMessageCell.swift → Sources/TextMessageCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,20 @@ open class TextMessageCell: MessageCollectionViewCell<MessageLabel> {
cellBottomLabel.attributedText = nil
}

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

if let displayDelegate = messagesCollectionView.messagesDisplayDelegate {
let textColor = displayDelegate.textColor(for: message, at: indexPath, in: messagesCollectionView)
messageContentView.textColor = textColor
}

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

}
2 changes: 1 addition & 1 deletion Sources/UIView+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ extension UIView {
leftAnchor.constraint(equalTo: superview.leftAnchor),
rightAnchor.constraint(equalTo: superview.rightAnchor),
topAnchor.constraint(equalTo: superview.topAnchor),
bottomAnchor.constraint(equalTo: superview.bottomAnchor),
bottomAnchor.constraint(equalTo: superview.bottomAnchor)
]
NSLayoutConstraint.activate(constraints)
}
Expand Down

0 comments on commit 9a396d0

Please sign in to comment.