-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChatViewController.m
363 lines (306 loc) · 15.7 KB
/
ChatViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
//
// ChatViewController.m
// flixnchill
//
// Created by Prasanth Guruprasad on 12/2/15.
// Copyright © 2015 codepath. All rights reserved.
//
#import "ChatViewController.h"
#import "UIImageView+AFNetworking.h"
#import "MessageCell.h"
#import "User.h"
#import "MovieComparisonView.h"
#import <Parse/Parse.h>
#import <PubNub/PubNub.h>
const NSString *PUBNUB_PUB_KEY = @"pub-c-fcaa8727-17b2-40b6-a0cd-f153f2ac72df";
const NSString *PUBNUB_SUB_KEY = @"sub-c-ead124cc-99d6-11e5-9a49-02ee2ddab7fe";
@interface ChatViewController () <UITableViewDelegate, UITableViewDataSource, PNObjectEventListener, UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UILabel *chatWithLabel;
@property (weak, nonatomic) IBOutlet UIButton *sendButton;
@property (weak, nonatomic) IBOutlet UITextField *chatTextField;
@property (weak, nonatomic) IBOutlet UITableView *messagesTableView;
@property (strong, nonatomic) NSString *channelName;
@property (weak, nonatomic) IBOutlet UIView *header;
@property (strong, nonatomic) NSMutableArray *messages;
@property (nonatomic) PubNub *client;
@end
@implementation ChatViewController
- (void) viewDidLoad {
[self setupTableView];
[self setupViews];
[self setupPubNub];
[super viewDidLoad];
self.messages = [NSMutableArray array];
// Do any additional setup after loading the view.
}
- (void) viewWillAppear:(BOOL)animated {
[self setupNavBar];
}
- (void) didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction) onBackTapped:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
}
- (void) setupNavBar {
self.navigationController.navigationBar.hidden = YES;
}
- (IBAction)sendButtonTapped:(id)sender {
NSString *messageText = self.chatTextField.text;
[self sendMessage: messageText];
self.chatTextField.text = @"";
}
-(void) setupViews {
UIColor *netflixRed = [UIColor colorWithRed:(185/255.0) green:(9/255.0) blue:(11/255.0) alpha:1] ;
self.header.backgroundColor = netflixRed;
[self registerForKeyboardNotifications];
self.chatWithLabel.text = self.chatWith.name;
self.chatTextField.delegate = self;
// HACK : TO Push the Content Up beyond the border
// UIEdgeInsets contentInsets = UIEdgeInsetsMake(-20.0, 0.0, 0.0, 0.0);
// self.scrollView.contentInset = contentInsets;
// self.scrollView.scrollIndicatorInsets = contentInsets;
}
- (BOOL) textFieldShouldReturn:(UITextField *)textField {
[self.chatTextField resignFirstResponder];
return NO;
}
- (void) registerForKeyboardNotifications {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void) keyboardWasShown: (NSNotification *)aNotification {
NSDictionary *info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(-kbSize.height, 0.0, kbSize.height, 0.0);
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
}
- (void) keyboardWillBeHidden: (NSNotification *)aNotification {
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
}
#pragma mark TableView methods
-(void) setupTableView {
UITapGestureRecognizer *tapTableGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapOnTableView)];
[self.messagesTableView addGestureRecognizer:tapTableGR];
self.messagesTableView.dataSource = self;
self.messagesTableView.delegate = self;
UINib *messageCell = [UINib nibWithNibName:@"MessageCell" bundle:nil];
[self.messagesTableView registerNib:messageCell forCellReuseIdentifier:@"MessageCell"];
self.messagesTableView.estimatedRowHeight = 100.0f;
self.messagesTableView.separatorColor = [UIColor clearColor];
UINib *movieComparisonCell = [UINib nibWithNibName:@"MovieComparisonView" bundle:nil];
[self.messagesTableView registerNib:movieComparisonCell forCellReuseIdentifier:@"MovieComparisonView"];
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) {
return 1;
} else {
return self.messages.count;
}
}
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0 && indexPath.row == 0) {
MovieComparisonView *comparisonView = [tableView dequeueReusableCellWithIdentifier:@"MovieComparisonView"];
comparisonView.matchLikedLabel.text = [NSString stringWithFormat:@" %@ prefers: ", self.chatWith.name];
//set up movie comparison
PFQuery *query = [PFQuery queryWithClassName:@"MatchMovieInfo"];
[query whereKey:@"currentUser" equalTo:[User currentUser].email];
[query whereKey:@"matchedUser" equalTo:self.chatWith.email];
[query orderByDescending:@"createdAt"];
[query getFirstObjectInBackgroundWithBlock:^(PFObject * _Nullable object, NSError * _Nullable error) {
if (!error) {
NSLog(@"%@", object.objectId);
NSString *movieOneWithChoice = object[@"movieOne"];
NSString *movieOneChoice = [movieOneWithChoice substringToIndex:4];
NSString *movieOneId = [movieOneWithChoice substringFromIndex:4];
NSString *movieTwoWithChoice = object[@"movieTwo"];
NSString *movieTwoChoice = [movieTwoWithChoice substringToIndex:4];
NSString *movieTwoId = [movieTwoWithChoice substringFromIndex:4];
NSString *movieThreeWithChoice = object[@"movieThree"];
NSString *movieThreeChoice = [movieThreeWithChoice substringToIndex:4];
NSString *movieThreeId = [movieThreeWithChoice substringFromIndex:4];
if ([movieOneChoice isEqualToString:@"like"]) {
comparisonView.youMovieChoiceOne.image = [UIImage imageNamed: @"nav_like_button"];
} else if ([movieOneChoice isEqualToString:@"nope"]) {
comparisonView.youMovieChoiceOne.image = [UIImage imageNamed: @"nav_nope_button"];
}
comparisonView.youMovieChoiceOne.alpha = 0.7f;
if ([movieTwoChoice isEqualToString:@"like"]) {
comparisonView.youMovieChoiceTwo.image = [UIImage imageNamed: @"nav_like_button"];
} else if ([movieTwoChoice isEqualToString:@"nope"]) {
comparisonView.youMovieChoiceTwo.image = [UIImage imageNamed: @"nav_nope_button"];
}
comparisonView.youMovieChoiceTwo.alpha = 0.7f;
if ([movieThreeChoice isEqualToString:@"like"]) {
comparisonView.youMovieChoiceThree.image = [UIImage imageNamed: @"nav_like_button"];
} else if ([movieThreeChoice isEqualToString:@"nope"]) {
comparisonView.youMovieChoiceThree.image = [UIImage imageNamed: @"nav_nope_button"];
}
comparisonView.youMovieChoiceThree.alpha = 0.7f;
int i = 0;
for (NSDictionary *movie in self.movies) {
if ([movie[@"id"] isEqualToString:movieOneId]) {
NSString *thumbnailString = movie[@"posters"][@"thumbnail"];
NSURL *url = [NSURL URLWithString:thumbnailString];
[comparisonView.youMovieOne setImageWithURL:url];
}
if ([movie[@"id"] isEqualToString:movieTwoId]) {
NSString *thumbnailString = movie[@"posters"][@"thumbnail"];
NSURL *url = [NSURL URLWithString:thumbnailString];
[comparisonView.youMovieTwo setImageWithURL:url];
}
if ([movie[@"id"] isEqualToString:movieThreeId]) {
NSString *thumbnailString = movie[@"posters"][@"thumbnail"];
NSURL *url = [NSURL URLWithString:thumbnailString];
[comparisonView.youMovieThree setImageWithURL:url];
}
if (self.randOne == i) {
NSString *thumbnailString = movie[@"posters"][@"thumbnail"];
NSURL *url = [NSURL URLWithString:thumbnailString];
[comparisonView.matchMovieOne setImageWithURL:url];
if (self.randTwo < 10) {
comparisonView.matchMovieChoiceOne.image = [UIImage imageNamed: @"nav_like_button"];
} else {
comparisonView.matchMovieChoiceOne.image = [UIImage imageNamed: @"nav_nope_button"];
}
comparisonView.matchMovieChoiceOne.alpha = 0.7f;
}
if (self.randTwo == i) {
NSString *thumbnailString = movie[@"posters"][@"thumbnail"];
NSURL *url = [NSURL URLWithString:thumbnailString];
[comparisonView.matchMovieTwo setImageWithURL:url];
if (self.randThree < 10) {
comparisonView.matchMovieChoiceTwo.image = [UIImage imageNamed: @"nav_like_button"];
} else {
comparisonView.matchMovieChoiceTwo.image = [UIImage imageNamed: @"nav_nope_button"];
}
comparisonView.matchMovieChoiceTwo.alpha = 0.7f;
}
if (self.randThree == i) {
NSString *thumbnailString = movie[@"posters"][@"thumbnail"];
NSURL *url = [NSURL URLWithString:thumbnailString];
[comparisonView.matchMovieThree setImageWithURL:url];
if (self.randOne < 10) {
comparisonView.matchMovieChoiceThree.image = [UIImage imageNamed: @"nav_like_button"];
} else {
comparisonView.matchMovieChoiceThree.image = [UIImage imageNamed: @"nav_nope_button"];
}
comparisonView.matchMovieChoiceThree.alpha = 0.7f;
}
i = i+1;
}
} else {
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
return comparisonView;
} else {
MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MessageCell"];
NSDictionary *message = self.messages[indexPath.row];
if ([message[@"author"] isEqualToString:[User currentUser].name]) {
cell.otherPersonMessageLabel.text = @"";
cell.myMessageLabel.text = [NSString stringWithFormat:@" %@ ", message[@"text"]];
} else {
cell.myMessageLabel.text = @"";
cell.otherPersonMessageLabel.text = [NSString stringWithFormat:@" %@ ", message[@"text"]];
}
return cell;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 0;
}
- (void) scrollTableToBottomAnimated: (BOOL) animated {
int rowNumber = [self.messagesTableView numberOfRowsInSection:1];
if (rowNumber > 0) [self.messagesTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:rowNumber-1 inSection:1] atScrollPosition:UITableViewScrollPositionBottom animated:animated];
}
- (void) scrollTableToBottom {
[self scrollTableToBottomAnimated: NO];
}
- (void) didTapOnTableView {
[self.chatTextField resignFirstResponder];
}
#pragma mark END TableView methods
#pragma mark PubNub methods
- (void) setupPubNub {
self.channelName = [self generateChannelName];
PNConfiguration *config = [PNConfiguration configurationWithPublishKey:PUBNUB_PUB_KEY subscribeKey:PUBNUB_SUB_KEY];
self.client = [PubNub clientWithConfiguration:config];
[self.client addListener:self];
[self.client subscribeToChannels:@[self.channelName] withPresence:YES];
[self loadHistoryForChannel: self.channelName];
}
- (void) loadHistoryForChannel: (NSString *)channel {
[self.client historyForChannel:channel start:nil end:nil limit:100 withCompletion:^(PNHistoryResult *result, PNErrorStatus *status) {
if (!status.isError) {
[self.messages addObjectsFromArray:result.data.messages];
[self.messagesTableView reloadData];
[self scrollTableToBottomAnimated: NO];
}
}];
}
- (void) client:(PubNub *)client didReceiveMessage:(PNMessageResult *)message {
if ([message.data.message[@"author"] isEqualToString:[User currentUser].name]) {
return; // return if the currently received message is from self, as it would have already been added
}
[self.messages addObject:message.data.message];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:(self.messages.count - 1) inSection:1];
[self.messagesTableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
[self scrollTableToBottom];
NSLog(@"Received message: %@ on channel %@ at %@", message.data.message,
message.data.subscribedChannel, message.data.timetoken);
}
- (void) sendMessage: (NSString *)messageText {
if ([[messageText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] isEqualToString:@""]) {
// empty message
return;
}
User * me = [User currentUser];
NSLog(@"Attempting Message Send : %@", messageText);
NSMutableDictionary *messageDictionary = [NSMutableDictionary dictionary];
messageDictionary[@"text"] = messageText;
messageDictionary[@"author"] = me.name;
[self.client publish: messageDictionary toChannel: self.channelName storeInHistory:YES
withCompletion:^(PNPublishStatus *status) {
if (!status.isError) {
NSLog(@"Successfully sent message");
}
else {
}
}
];
[self.messages addObject:messageDictionary];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:(self.messages.count - 1) inSection:1];
[self.messagesTableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
[self scrollTableToBottom];
}
// Gives back a channel name for user1 &user2 to converse (user1Email:user2Email), s.t. user1Email < user2Email
- (NSString *) generateChannelName {
NSString *myEmail = [User currentUser].email;
NSString *otherEmail = self.chatWith.email;
if ( [myEmail caseInsensitiveCompare:otherEmail] == NSOrderedAscending ) {
return [NSString stringWithFormat:@"%@:%@", myEmail, otherEmail];
} else {
return [NSString stringWithFormat:@"%@:%@", otherEmail, myEmail];
}
}
#pragma mark END PubNub methods
@end