Skip to content

Commit

Permalink
messages, displayedMessagesRange for ChatViewController
Browse files Browse the repository at this point in the history
  • Loading branch information
nixzhu committed Jul 5, 2016
1 parent 6ee31e8 commit 1a8dad8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
6 changes: 4 additions & 2 deletions Yep/ViewControllers/Chat/CellNodes/ChatBaseCellNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import AsyncDisplayKit

class ChatBaseCellNode: ASCellNode {

static let avatarSize = CGSize(width: 40, height: 40)

var user: User? {
didSet {
if let user = user {
Expand Down Expand Up @@ -48,12 +50,12 @@ class ChatBaseCellNode: ASCellNode {

override func calculateSizeThatFits(constrainedSize: CGSize) -> CGSize {

return CGSize(width: constrainedSize.width, height: 50)
return CGSize(width: constrainedSize.width, height: ChatBaseCellNode.avatarSize.height)
}

override func layout() {
super.layout()

avatarImageNode.frame = CGRect(x: 15, y: 0, width: 40, height: 40)
avatarImageNode.frame = CGRect(origin: CGPoint(x: 15, y: 0), size: ChatBaseCellNode.avatarSize)
}
}
6 changes: 3 additions & 3 deletions Yep/ViewControllers/Chat/CellNodes/ChatLeftTextCellNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ class ChatLeftTextCellNode: ChatBaseCellNode {
textNode.backgroundColor = UIColor.greenColor()
}

func configure(withMessage message: Message, layoutCache: ChatTextCellLayoutCache) {
func configure(withMessage message: Message) {

self.user = message.fromFriend

do {
let text = message.textContent
let attributes = [
NSForegroundColorAttributeName: UIColor.blackColor(),
NSFontAttributeName: UIFont.systemFontOfSize(17)
NSFontAttributeName: UIFont.chatTextFont(),
]
textNode.attributedText = NSAttributedString(string: text, attributes: attributes)
}
Expand All @@ -41,7 +41,7 @@ class ChatLeftTextCellNode: ChatBaseCellNode {
let textMaxWidth = constrainedSize.width - (15 + 40 + 5 + 15)
textNode.measure(CGSize(width: textMaxWidth, height: CGFloat.max))

let height = max(textNode.calculatedSize.height, avatarImageNode.bounds.height)
let height = max(textNode.calculatedSize.height, ChatBaseCellNode.avatarSize.height)

return CGSize(width: constrainedSize.width, height: height)
}
Expand Down
27 changes: 25 additions & 2 deletions Yep/ViewControllers/Chat/ChatViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ import AsyncDisplayKit
class ChatViewController: BaseViewController {

var conversation: Conversation!
var realm: Realm!

lazy var messages: Results<Message> = {
return messagesOfConversation(self.conversation, inRealm: self.realm)
}()

let messagesBunchCount = 20
var displayedMessagesRange = NSRange()

lazy var tableNode: ASTableNode = {
let node = ASTableNode()
Expand Down Expand Up @@ -58,6 +66,16 @@ class ChatViewController: BaseViewController {
view.addSubview(tableNode.view)
//view.addSubview(collectionNode.view)
}

realm = conversation.realm!

do {
if messages.count >= messagesBunchCount {
displayedMessagesRange = NSRange(location: messages.count - messagesBunchCount, length: messagesBunchCount)
} else {
displayedMessagesRange = NSRange(location: 0, length: messages.count)
}
}
}
}

Expand All @@ -70,13 +88,18 @@ extension ChatViewController: ASTableDataSource, ASTableDelegate {

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return 20
return displayedMessagesRange.length
}

func tableView(tableView: ASTableView, nodeForRowAtIndexPath indexPath: NSIndexPath) -> ASCellNode {

let node = ChatLeftTextCellNode()
node.backgroundColor = UIColor.yepTintColor()

guard let message = messages[safe: (displayedMessagesRange.location + indexPath.item)] else {
fatalError()
}
node.configure(withMessage: message)

return node
}
}
Expand Down

0 comments on commit 1a8dad8

Please sign in to comment.