Created
April 19, 2014 21:23
-
-
Save seanmccann/11097922 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// EVPCConversationViewController.h | |
// EVPcard | |
// | |
// Created by Sebastien Arbogast on 27/02/14. | |
// Copyright (c) 2014 Epseelon. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> | |
#import <JSMessagesViewController/JSMessagesViewController.h> | |
#import <Parse/Parse.h> | |
@interface EVPCConversationViewController : JSMessagesViewController<JSMessagesViewDelegate, JSMessagesViewDataSource> | |
@property (nonatomic, strong) PFObject *conversation; | |
@property (nonatomic, strong) PFUser *recipient; | |
- (IBAction)cancelButtonTapped:(id)sender; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// EVPCConversationViewController.m | |
// EVPcard | |
// | |
// Created by Sebastien Arbogast on 27/02/14. | |
// Copyright (c) 2014 Epseelon. All rights reserved. | |
// | |
#import "EVPCConversationViewController.h" | |
#import <JSMessagesViewController/JSMessage.h> | |
#import <Parse/Parse.h> | |
@interface EVPCConversationViewController () | |
@property(nonatomic, strong) NSMutableArray *messages; | |
@end | |
@implementation EVPCConversationViewController | |
- (void)viewDidLoad { | |
self.delegate = self; | |
self.dataSource = self; | |
[super viewDidLoad]; | |
[[JSBubbleView appearance] setFont:[UIFont systemFontOfSize:16.0f]]; | |
self.title = self.recipient.username; | |
self.messageInputView.textView.placeHolder = NSLocalizedString(@"Your message", @""); | |
self.sender = [PFUser currentUser].username; | |
} | |
- (void)viewWillAppear:(BOOL)animated { | |
[super viewWillAppear:animated]; | |
if (self.conversation) { | |
PFQuery *messageQuery = [PFQuery queryWithClassName:@"EVPCMessage"]; | |
[messageQuery whereKey:@"conversation" equalTo:self.conversation]; | |
[messageQuery orderByAscending:@"date"]; | |
[messageQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { | |
if (error == nil) { | |
self.messages = [[NSMutableArray alloc] initWithCapacity:objects.count]; | |
for (PFObject *msg in objects) { | |
JSMessage *message = [[JSMessage alloc] initWithText:msg[@"text"] sender:((PFUser *) msg[@"sender"]).username date:msg[@"date"]]; | |
[self.messages addObject:message]; | |
} | |
[self.tableView reloadData]; | |
} else { | |
NSLog(@"Error: %@", [error localizedDescription]); | |
} | |
}]; | |
} else { | |
self.messages = [[NSMutableArray alloc] init]; | |
} | |
[self scrollToBottomAnimated:NO]; | |
[self.messageInputView.textView becomeFirstResponder]; | |
} | |
- (void)didReceiveMemoryWarning { | |
[super didReceiveMemoryWarning]; | |
// Dispose of any resources that can be recreated. | |
} | |
#pragma mark - Table view data source | |
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { | |
return [self.messages count]; | |
} | |
#pragma mark - JSMessagesViewDataSource implementation | |
- (id <JSMessageData>)messageForRowAtIndexPath:(NSIndexPath *)indexPath { | |
JSMessage *message = self.messages[(NSUInteger) indexPath.row]; | |
return message; | |
} | |
- (UIImageView *)avatarImageViewForRowAtIndexPath:(NSIndexPath *)indexPath sender:(NSString *)sender { | |
return nil; | |
} | |
#pragma mark - JSMessagesViewDelegate implementation | |
- (void)didSendText:(NSString *)text fromSender:(NSString *)sender onDate:(NSDate *)date { | |
JSMessage *newMessage = [[JSMessage alloc] initWithText:text sender:[PFUser currentUser].username date:date]; | |
[self.messages addObject:newMessage]; | |
[self finishSend]; | |
/*self.messageInputView.sendButton.enabled = NO; | |
if (self.conversation != nil) { | |
[self sendMessageWithText:text date:date]; | |
} else { | |
PFObject *conversation = [PFObject objectWithClassName:@"EVPCConversation"]; | |
conversation[@"sender"] = [PFUser currentUser]; | |
conversation[@"recipient"] = self.recipient; | |
conversation[@"startDate"] = [NSDate date]; | |
[conversation saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { | |
if (succeeded) { | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
self.conversation = conversation; | |
[self sendMessageWithText:text date:date]; | |
}); | |
} else { | |
self.messageInputView.sendButton.enabled = YES; | |
NSLog(@"Error: %@", [error localizedDescription]); | |
} | |
}]; | |
}*/ | |
} | |
- (void)sendMessageWithText:(NSString *)text date:(NSDate *)date { | |
PFObject *message = [PFObject objectWithClassName:@"EVPCMessage"]; | |
message[@"conversation"] = self.conversation; | |
message[@"sender"] = [PFUser currentUser]; | |
message[@"text"] = text; | |
message[@"date"] = date; | |
[message saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { | |
if (succeeded) { | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
JSMessage *newMessage = [[JSMessage alloc] initWithText:text sender:[PFUser currentUser].username date:date]; | |
[self.messages addObject:newMessage]; | |
[self finishSend]; | |
}); | |
} else { | |
NSLog(@"Error: %@", [error localizedDescription]); | |
} | |
self.messageInputView.sendButton.enabled = YES; | |
}]; | |
} | |
- (JSBubbleMessageType)messageTypeForRowAtIndexPath:(NSIndexPath *)indexPath { | |
JSMessage *message = self.messages[(NSUInteger) indexPath.row]; | |
if ([message.sender isEqualToString:[PFUser currentUser].username]) { | |
return JSBubbleMessageTypeOutgoing; | |
} else { | |
return JSBubbleMessageTypeIncoming; | |
} | |
} | |
- (UIImageView *)bubbleImageViewWithType:(JSBubbleMessageType)type forRowAtIndexPath:(NSIndexPath *)indexPath { | |
return nil; | |
} | |
- (JSMessageInputViewStyle)inputViewStyle { | |
return JSMessageInputViewStyleFlat; | |
} | |
- (BOOL)shouldDisplayTimestampForRowAtIndexPath:(NSIndexPath *)indexPath { | |
return NO; | |
} | |
- (BOOL)shouldPreventScrollToBottomWhileUserScrolling { | |
return NO; | |
} | |
- (BOOL)allowsPanToDismissKeyboard { | |
return YES; | |
} | |
/*- (UIButton *)sendButtonForInputView { | |
return nil; | |
}*/ | |
/*- (NSString *)customCellIdentifierForRowAtIndexPath:(NSIndexPath *)indexPath { | |
return nil; | |
}*/ | |
- (IBAction)cancelButtonTapped:(id)sender { | |
[self.navigationController dismissViewControllerAnimated:YES completion:nil]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment