Using JSQMessagesViewController with Parse.com

I came across what looks like a very complex and customizable library that would be very useful for any application that wants to create a messaging system. JSQMessagesViewController on Github . While I was trying to implement the library myself, I ran into some problems. Alone, I do not quite understand the terminology of โ€œprotocolsโ€ and โ€œclassesโ€ (I am a little versed in classes). The first problem was that I could not use PFObjects in the CollectionView, because the JSQMessage instance should go through most of the custom methods. Now I know how to take in PFObject to get properties from this, for example

self.eachMessage = [self.messages objectAtIndex:indexPath.row]; //each message
self.eachMessage[@"message"] //message from the PFObject
self.eachMessage[@"sender"] //sender from the PFObject

The JSQMessage class has custom properties that will represent PFObject properties, for example

JSQMessage *message = [[JSQMessage alloc] init]; //initialize it
message.senderId //could be the objectId of the user
message.senderDisplayName //the user username
message.text //text of the message
message.date //time sent of the message

The thing is, in the JSQMessage custom class ... all of these properties are read-only. Iโ€™m sure I can go in and change it so that I can assign them exactly what I want, but here Iโ€™m missing something. I will attach everything in my .h and .m files. When I send a message , the only thing that comes is text, and I believe that this is because it rises when it comes from textView on the input tab.

.h file

#import <UIKit/UIKit.h>
#import <JSQMessagesViewController/JSQMessages.h>
#import <Parse/Parse.h>
#import <JSQMessagesViewController/JSQMessagesBubbleImageFactory.h>


@interface ConvoViewController : JSQMessagesViewController 

@property (strong, nonatomic) NSMutableArray *messages;
@property (strong, nonatomic) PFUser *sender;
@property (strong, nonatomic) PFUser *receiver;
@property (strong, nonatomic) JSQMessage *eachMessage;
@property (strong, nonatomic) PFObject *aMessage;
@property (strong, nonatomic) JSQMessagesBubbleImageFactory *bubbleImage;

@end

.m file

- (void)viewDidLoad {
    [super viewDidLoad];
    //Color of the keyboard (Dark to match everything else)
    self.inputToolbar.contentView.textView.keyboardAppearance = UIKeyboardAppearanceDark;

    //Color the inputview background
    self.inputToolbar.backgroundColor = [UIColor colorWithWhite:0 alpha:0.9];

    //Delete the avatars appearing next to the messages
    self.collectionView.collectionViewLayout.incomingAvatarViewSize = CGSizeZero;
    self.collectionView.collectionViewLayout.outgoingAvatarViewSize = CGSizeZero;

    //Set the senderID
    self.senderId = self.sender.objectId;


}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:YES];

    //Query for part of the messages
    PFQuery *messages1 = [PFQuery queryWithClassName:@"Messages"];
    [messages1 whereKey:@"sender" equalTo:self.sender];
    [messages1 whereKey:@"receiver" equalTo:self.receiver];

    //Query for other part of messages
    PFQuery *messages2 = [PFQuery queryWithClassName:@"Messages"];
    [messages2 whereKey:@"sender" equalTo:self.receiver];
    [messages2 whereKey:@"receiver" equalTo:self.sender];

    //Combine those queries
    PFQuery *allMessages = [PFQuery orQueryWithSubqueries:@[messages1, messages2]];
    [allMessages findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        self.messages = [objects mutableCopy];
        [self.collectionView reloadData];
    }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Send Button
- (void)didPressSendButton:(UIButton *)button withMessageText:(NSString *)text senderId:(NSString *)senderId senderDisplayName:(NSString *)senderDisplayName date:(NSDate *)date {
    [JSQSystemSoundPlayer jsq_playMessageSentSound];

    JSQMessage *message = [[JSQMessage alloc] initWithSenderId:self.sender.objectId
                                             senderDisplayName:self.sender.username
                                                          date:[NSDate date]
                                                          text:text];

    [self.messages addObject:message];
    NSLog(@"%@", text);

    [self finishSendingMessageAnimated:YES];


}

#pragma mark - JSQMessages Data Source methods

- (id<JSQMessageData>)collectionView:(JSQMessagesCollectionView *)collectionView messageDataForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //Return the actual message at each indexpath.row
    return [self.messages objectAtIndex:indexPath.row];
}

- (id<JSQMessageBubbleImageDataSource>)collectionView:(JSQMessagesCollectionView *)collectionView messageBubbleImageDataForItemAtIndexPath:(NSIndexPath *)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 textView.
     *
     *  Otherwise, return your previously created bubble image data objects.
     */

    JSQMessage *message = [self.messages objectAtIndex:indexPath.item];
    if ([message.senderId isEqualToString:self.senderId]) {
        return [self.bubbleImage incomingMessagesBubbleImageWithColor:[UIColor orangeColor]];
    }

    return [self.bubbleImage outgoingMessagesBubbleImageWithColor:[UIColor grayColor]];
}



#pragma mark - Collection View

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    //Number of messages
    return self.messages.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    //Number of sections
    return 1;
}


- (UICollectionViewCell *)collectionView:(JSQMessagesCollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    //Creating or initial cell for the number of index paths (number of messages)
    JSQMessagesCollectionViewCell *cell = (JSQMessagesCollectionViewCell *)[super collectionView:collectionView cellForItemAtIndexPath:indexPath];

    //Put our messages dictionaries into PFObject so we can put them into individual cells
    self.eachMessage = [self.messages objectAtIndex:indexPath.row];


    //Put the message object into the textView text property
    cell.textView.text = self.eachMessage.text;

    //Setting the text color of the message bubble based upon the sender
    if ([self.eachMessage.senderId isEqualToString:self.senderId]) {
        cell.textView.textColor = [UIColor blackColor];
    } else {
        cell.textView.textColor = [UIColor whiteColor];
    }
    //Set the top label to the person who sent the message
    cell.cellTopLabel.text = [NSString stringWithFormat:@"@%@", self.eachMessage.senderId];

    //Format the bottom label to a readable date
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MM/dd/yy h:mm a"];
    cell.cellBottomLabel.text = [dateFormatter stringFromDate:self.eachMessage.date];

    //If there is a link of some sorts in the message
    cell.textView.linkTextAttributes = @{ NSForegroundColorAttributeName : cell.textView.textColor,
                                          NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle | NSUnderlinePatternSolid) };

    //What we return into the collectionview
    return cell;

}
+4
source share
1 answer

. , , .: -)

DemoData DemoMessagesViewController:

self.demoData = [[DemoModelData alloc] init];

self.demoData.users = @{self.senderId:self.senderDisplayName,
                        self.targetUserId:self.targetUserName};

UIImage *tempImg = [HTTPServerKaDost fetchImageFromUrlString:userProfile.profilePicUrl]; //replace with your code to load image

JSQMessagesAvatarImage *srcImage = [JSQMessagesAvatarImageFactory
                                    avatarImageWithImage:tempImg
                                    diameter:kJSQMessagesCollectionViewAvatarSizeDefault];

tempImg = [HTTPServerKaDost fetchImageFromUrlString:self.targetUserImageFullUrl];

JSQMessagesAvatarImage *destImage = [JSQMessagesAvatarImageFactory
                                     avatarImageWithImage:tempImg
                                     diameter:kJSQMessagesCollectionViewAvatarSizeDefault];

self.demoData.avatars = @{self.senderId:srcImage,
                          self.targetUserId:destImage};

[self.demoData loadMessages];

loadMessages ( DemoModelData ), JSQMessage:

JSQMessage *msg = [[JSQMessage alloc] initWithSenderId:[msgDict valueForKey:@"sender"]
                                     senderDisplayName:[msgDict valueForKey:@"fname"]
                                                  date:dateT
                                                  text:[msgDict valueForKey:@"data"]];

[self.messages addObject:msg];

, , DemoMessagesViewController:

[self finishReceivingMessageAnimated:YES];
[self scrollToBottomAnimated:YES];
0

All Articles