Skip to content

Instantly share code, notes, and snippets.

@sbauch
Created January 9, 2015 19:14
Show Gist options
  • Save sbauch/fa231be17b125bb72fcb to your computer and use it in GitHub Desktop.
Save sbauch/fa231be17b125bb72fcb to your computer and use it in GitHub Desktop.
class MessagingController < JSQMessagesViewController
alias :'super_collectionView:cellForItemAtIndexPath' :'collectionView:cellForItemAtIndexPath'
attr_accessor :messages, :incomingBubbleImageData, :outgoingBubbleImageData, :garyAvatar
JSQ_MESSAGES_COLLECTION_VIEW_CELL_LABEL_HEIGHT = 20.0
def viewDidLoad
super
self.collectionView.collectionViewLayout.incomingAvatarViewSize = CGSizeMake(40,40)
self.collectionView.backgroundColor = '#244555'.uicolor
self.senderId = '2'
setupTestModel
self.garyAvatar = JSQMessagesAvatarImageFactory.avatarImageWithImage('gary.png'.uiimage, diameter: 90)
# Remove camera button since media messages are not yet implemented
# self.inputToolbar.contentView.leftBarButtonItem = nil
# Or, you can set a custom `leftBarButtonItem` and a custom `rightBarButtonItem`
# Create bubble images.
bubble_factory = JSQMessagesBubbleImageFactory.alloc.init
self.outgoingBubbleImageData = bubble_factory.outgoingMessagesBubbleImageWithColor('#a0bec7'.uicolor)
self.incomingBubbleImageData = bubble_factory.incomingMessagesBubbleImageWithColor('#ef3d2d'.uicolor)
# self.navigationItem.rightBarButtonItem = UIBarButtonItem.alloc.initWithImage(UIImage.imageNamed("typing"),
# style:UIBarButtonItemStyleBordered,
# target:self,
# action:'receiveMessagePressed')
end
def viewWillAppear(animated)
super
# if self.delegateModal
# self.navigationItem.leftBarButtonItem = UIBarButtonItem.alloc.initWithBarButtonSystemItem(UIBarButtonSystemItemStop, target:self, action:'closePressed')
# end
end
def viewDidAppear(animated)
super
# Enable/disable springy bubbles, default is true.
# For best results, toggle from `viewDidAppear:`
self.collectionView.collectionViewLayout.springinessEnabled = true
end
# Actions
def closePressed(sender)
self.delegateModal.didDismissJSQDemoViewController(self)
end
# JSQMessagesViewController method overrides
def didPressAccessoryButton(sender)
NSLog("Camera pressed!")
end
# JSQMessages CollectionView DataSource
def collectionView(collectionView, messageDataForItemAtIndexPath:indexPath)
return self.messages[indexPath.item]
end
def collectionView(collectionView, messageBubbleImageDataForItemAtIndexPath:indexPath)
# You may return nil here if you do not want bubbles.
# In this case, you should set the background color of your collection view cell's textView.
# Reuse created bubble images, but create new imageView to add to each cell
# Otherwise, each cell would be referencing the same imageView and bubbles would disappear from cells
message = self.messages[indexPath.item]
if (message.senderId == self.senderId)
return self.outgoingBubbleImageData
end
return self.incomingBubbleImageData
end
def collectionView(collectionView, avatarImageDataForItemAtIndexPath:indexPath)
# Return `nil` here if you do not want avatars.
# If you do return `nil`, be sure to do the following in `viewDidLoad`
# self.collectionView.collectionViewLayout.outgoingAvatarViewSize = CGSizeZero
# It is possible to have only outgoing avatars or only incoming avatars, too.
# Reuse created avatar images, but create new imageView to add to each cell
# Otherwise, each cell would be referencing the same imageView and avatars would disappear from cells
# Note: these images will be sized according to these values:
# self.collectionView.collectionViewLayout.incomingAvatarViewSize
# self.collectionView.collectionViewLayout.outgoingAvatarViewSize
# Override the defaults in `viewDidLoad`
return self.garyAvatar
end
def collectionView(collectionView, attributedTextForCellTopLabelAtIndexPath:indexPath)
# This logic should be consistent with what you return from `heightForCellTopLabelAtIndexPath:`
# The other label text delegate methods should follow a similar pattern.
# Show a timestamp for every 3rd message
if (indexPath.item % 3 == 0)
message = self.messages[indexPath.item]
return JSQMessagesTimestampFormatter.sharedFormatter.attributedTimestampForDate(message.date)
end
return nil
end
def collectionView(collectionView, attributedTextForMessageBubbleTopLabelAtIndexPath:indexPath)
message = self.messages[indexPath.item]
# iOS7-style sender name labels
if message.senderId == self.senderId
return nil
end
if (indexPath.item - 1 >= 0)
previousMessage = self.messages[indexPath.item - 1]
if (previousMessage.senderId == message.senderId)
return nil
end
end
# Don't specify attributes to use the defaults.
return NSAttributedString.alloc.initWithString(message.senderDisplayName)
end
def collectionView(collectionView, attributedTextForCellBottomLabelAtIndexPath:indexPath)
return nil
end
def collectionView(collectionView, numberOfItemsInSection:section)
if self.messages
self.messages.count
else
0
end
end
def collectionView(collectionView, cellForItemAtIndexPath:indexPath)
cell = super_collectionView(collectionView, cellForItemAtIndexPath:indexPath)
# Configure almost *anything* on the cell
# Text colors, label text, label colors, etc. DO NOT set `cell.textView.font` !
# Instead, you need to set `self.collectionView.collectionViewLayout.messageBubbleFont` to the font you want in `viewDidLoad`
# DO NOT manipulate cell layout information!
# Instead, override the properties you want on `self.collectionView.collectionViewLayout` from `viewDidLoad`
message = self.messages[indexPath.item]
cell.textView.textColor = UIColor.whiteColor
cell.backgroundColor = '#244555'.uicolor
cell.textView.linkTextAttributes = { NSForegroundColorAttributeName: cell.textView.textColor,
NSUnderlineStyleAttributeName: NSUnderlineStyleSingle | NSUnderlinePatternSolid
}
return cell
end
# JSQMessages collection view flow layout delegate
def collectionView(collectionView, layout:collectionViewLayout, heightForCellTopLabelAtIndexPath:indexPath)
# /** * Each label in a cell has a `height` delegate method that corresponds to its text dataSource method */
# /** * This logic should be consistent with what you return from `attributedTextForCellTopLabelAtIndexPath:`
# * The other label height delegate methods should follow similarly * * Show a timestamp for every 3rd message
# */
if (indexPath.item % 3 == 0)
return JSQ_MESSAGES_COLLECTION_VIEW_CELL_LABEL_HEIGHT
end
return 0
end
def collectionView(collectionView, layout:collectionViewLayout, heightForMessageBubbleTopLabelAtIndexPath:indexPath)
# iOS7-style sender name labels
currentMessage = self.messages[indexPath.item]
if currentMessage.senderId == self.senderId
return 0
end
if (indexPath.item - 1 >= 0)
previousMessage = self.messages[indexPath.item - 1]
if previousMessage.senderId == currentMessage.senderId
return 0
end
end
return JSQ_MESSAGES_COLLECTION_VIEW_CELL_LABEL_HEIGHT
end
def collectionView(collectionView, layout:collectionViewLayout, heightForCellBottomLabelAtIndexPath:indexPath)
return 0
end
def collectionView(collectionView, header:headerView, didTapLoadEarlierMessagesButton:sender)
NSLog("Load earlier messages!")
end
#
def didPressSendButton(button, withMessageText:text, senderId:senderId, senderDisplayName:senderDisplayName, date:date)
NSLog('pressed')
# # * Sending a message. Your implementation of this method should do *at least* the following:
# # *
# # * 1. Play sound (optional)
# # * 2. Add new id<JSQMessageData> object to your data source
# # * 3. Call `finishSendingMessage`
#
# # JSQSystemSoundPlayer.jsq_playMessageSentSound
#
message = JSQMessage.alloc.initWithSenderId(senderId, senderDisplayName:senderDisplayName, date:date,text:text)
self.messages << message
finishSendingMessageAnimated(true)
end
# - (void)didPressAccessoryButton:(UIButton *)sender
# {
# UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Media messages"
# delegate:self
# cancelButtonTitle:@"Cancel"
# destructiveButtonTitle:nil
# otherButtonTitles:@"Send photo", @"Send location", @"Send video", nil];
#
# [sheet showFromToolbar:self.inputToolbar];
# }
#
def setupTestModel
# Create avatar images once.
# Be sure to create your avatars one time and reuse them for good performance.
# If you are not using avatars, ignore this.
# outgoingDiameter = self.collectionView.collectionViewLayout.outgoingAvatarViewSize.width
# jsqImage = nil
#
# incomingDiameter = self.collectionView.collectionViewLayout.incomingAvatarViewSize.width
#
# # cookImage = JSQMessagesAvatarFactory.avatarWithImage(UIImage.imageNamed("demo_avatar_cook"), diameter:incomingDiameter)
# # jobsImage = JSQMessagesAvatarFactory.avatarWithImage(UIImage.imageNamed("demo_avatar_jobs"), diameter:incomingDiameter)
# # wozImage = JSQMessagesAvatarFactory.avatarWithImage(UIImage.imageNamed("demo_avatar_woz"), diameter:incomingDiameter)
# Fetch messages from Parse via Message model and convert into JSQMessage objects
Message.incomingMessages do |messages|
self.messages = messages
collectionView.reloadData
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment