-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathChatThreadController.m
More file actions
407 lines (309 loc) · 12.3 KB
/
ChatThreadController.m
File metadata and controls
407 lines (309 loc) · 12.3 KB
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//
// ChatThreadController.m
// SimpleChat
//
// Created by ARGELA on 1/13/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "ChatThreadController.h"
#import "XMPPMessageCoreDataObject.h"
#import "chatMessageCell.h"
#import "MediaHandler.h"
#import "MediaViewController.h"
@interface ChatThreadController()<UITableViewDelegate,UITableViewDataSource,NSFetchedResultsControllerDelegate,UIActionSheetDelegate,MediaHandlerDelegate,ChatCellDelegate>
@property (nonatomic,strong) NSFetchedResultsController *fetchedResultsController;
@property (nonatomic,strong) MediaHandler *mediaHandler;
@property (nonatomic,assign) XMPPMessageCoreDataObject* selectedMessageToView;
@end
@implementation ChatThreadController
@synthesize chatTable;
@synthesize keyboardToolbar;
@synthesize chatTextField;
@synthesize chatWith = _chatWith;
@synthesize myJid = _myJid;
@synthesize fetchedResultsController = _fetchedResultsController;
@synthesize context = _context;
@synthesize selfID = _selfID;
@synthesize mediaHandler = _mediaHandler;
@synthesize selectedMessageToView = _selectedMessageToView;
-(void) setMyJid:(XMPPJID *)myJid
{
_myJid = myJid;
if (_mediaHandler) {
[_mediaHandler setSelfJid:_myJid];
}
}
-(void) setChatWith:(NSString *)chatWith
{
_chatWith = chatWith;
if (_mediaHandler) {
[_mediaHandler setToChatJid:_chatWith];
}
}
-(void) configure
{
if (!self.fetchedResultsController) {
if (self.context) {
NSEntityDescription *entity = [NSEntityDescription entityForName:@"XMPPMessageCoreDataObject" inManagedObjectContext:self.context];
if (entity) {
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"whoOwns.jidStr = %@ AND messageReceipant=%@",self.chatWith,self.selfID];
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"sendDate" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:descriptor];
[request setEntity:entity];
[request setPredicate:predicate];
[request setSortDescriptors:sortDescriptors];
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.context sectionNameKeyPath:@"whoOwns.jidStr" cacheName:nil];
[self.fetchedResultsController setDelegate:self];
NSError *fetchError;
if ([self.fetchedResultsController performFetch:&fetchError])
{
NSLog(@"Sorgu Hatasi: %@",[fetchError description]);
}
}
}
}
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
-(void) controllerDidChangeContent:(NSFetchedResultsController *)controller
{
//NSLog(@"Tabloo Degisti");
[self.chatTable reloadData];
NSIndexPath *lastIndex = [NSIndexPath indexPathForRow:[self.chatTable numberOfRowsInSection:0]-1 inSection:0];
[self.chatTable scrollToRowAtIndexPath:lastIndex atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
#pragma mark - View lifecycle
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
//[self.chatTable setDelegate:self];
//[self.chatTable setDataSource:self];
_mediaHandler = [[MediaHandler alloc] init];
[_mediaHandler setDelegate:self];
if (!_mediaHandler.selfJid) {
[_mediaHandler setSelfJid:_myJid];
}
if (!_mediaHandler.toChatJid) {
[_mediaHandler setToChatJid:_chatWith];
}
[self configure];
}
- (void)viewDidUnload
{
[self setChatTable:nil];
[self setKeyboardToolbar:nil];
[self setChatTextField:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
-(void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
-(void) viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark -
#pragma mark Notifications
- (void)keyboardWillShow:(NSNotification *)notification {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
/*
CGRect frame = self.keyboardToolbar.frame;
frame.origin.y = self.view.frame.size.height - 260.0;
self.keyboardToolbar.frame = frame;
CGRect chatFrame = self.chatTable.frame;
chatFrame.size.height = self.chatTable.frame.size.height - 260.0;
self.chatTable.frame = chatFrame;
*/
self.keyboardToolbar.frame = CGRectMake(0, 156, 320, 44);
self.chatTable.frame = CGRectMake(0, 0, 320, 156);
//[self.chatTable setContentSize:CGSizeMake(self.chatTable.contentSize.width, self.chatTable.contentSize.height-260.0)];
[UIView commitAnimations];
}
- (void)keyboardWillHide:(NSNotification *)notification {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
/*
CGRect frame = self.keyboardToolbar.frame;
frame.origin.y = self.view.frame.size.height;
self.keyboardToolbar.frame = frame;
CGRect chatFrame = self.chatTable.frame;
chatFrame.size.height = self.view.frame.size.height;
self.chatTable.frame = chatFrame;
*/
self.keyboardToolbar.frame = CGRectMake(0, 372, 320, 44);
self.chatTable.frame = CGRectMake(0, 0, 320, 372);
[UIView commitAnimations];
}
- (IBAction)messageDone:(id)sender
{
NSString *textMessage = [self.chatTextField text];
//NSString *person = [self.chatWith description];
//NSLog(@"Chat Message: TO: %@ BODY: %@",person,textMessage);
NSArray *values = [NSArray arrayWithObjects:textMessage,self.chatWith,@"chat", nil];
NSArray *keys = [NSArray arrayWithObjects:@"body",@"to",@"type", nil];
NSDictionary *messageContent = [NSDictionary dictionaryWithObjects:values forKeys:keys];
[[NSNotificationCenter defaultCenter] postNotificationName:@"messageSent" object:nil userInfo:messageContent];
[self.chatTextField setText:@""];
}
#pragma mark - Table View Delegate
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
//NSLog(@"Sections Count: %d",[[self.fetchedResultsController sections] count]);
return 1;
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSArray *sections = [[self fetchedResultsController] sections];
if ([sections count]) {
id <NSFetchedResultsSectionInfo> sectionInfo = [sections objectAtIndex:0];
return sectionInfo.numberOfObjects;
}
return 0;
}
-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"messageCell";
chatMessageCell *cell = (chatMessageCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSLog(@"Yeni Cell Olusturuluyorrr");
NSArray* topLevelObj = [[NSBundle mainBundle] loadNibNamed:@"messageCell" owner:nil options:nil];
for (id currentObject in topLevelObj) {
if ([currentObject isKindOfClass:[UITableViewCell class]]) {
cell = (chatMessageCell *) currentObject;
break;
}
}
}
XMPPMessageCoreDataObject *messageObj = [self.fetchedResultsController objectAtIndexPath:indexPath];
[cell setMessage:messageObj];
[cell setMessageIndex:indexPath.row];
[cell setButtonDelegate:self];
NSLog(@"[ChatThreadCont] Index: %d Message Body: %@",indexPath.row,messageObj.body);
return cell;
}
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [chatMessageCell sizeForMessage:[self.fetchedResultsController objectAtIndexPath:indexPath]];
}
//////////////////////////////////////////
///ACTIONS///////////////////////////////
////////////////////////////////////////
- (IBAction)selectAction:(id)sender
{
UIActionSheet *popupQuery = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Take Photo or Video",@"Choose Existing",@"Audio Note",@"Share Contact", @"Share Location", nil];
popupQuery.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
[popupQuery showInView:self.view];
}
-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
//NSLog(@"Action SheetButton Index %d",buttonIndex);
switch (buttonIndex) {
case 0:
[_mediaHandler takePhoto];
break;
case 1:
[_mediaHandler takeFromGallery];
break;
case 2:
[_mediaHandler recordSound];
break;
case 3:
[_mediaHandler shareContact];
break;
case 4:
[_mediaHandler getLocation];
break;
default:
break;
}
}
-(void) presentMediaPickerController:(UIViewController *)mediaPicker
{
[self presentModalViewController:mediaPicker animated:YES];
}
-(void) dismissMediaPicker
{
[self dismissModalViewControllerAnimated:YES];
}
-(void) presentMediaResultController:(UIViewController *)mediaResult
{
[self.navigationController pushViewController:mediaResult animated:YES];
}
-(void) dismissMediaResult
{
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
-(void) presentActivityIndicator:(UIActivityIndicatorView *)activityIndicator
{
[self.view addSubview:activityIndicator];
}
-(void) dismissActivityIndicator:(NSInteger)tag
{
UIActivityIndicatorView *av = (UIActivityIndicatorView*) [self.view viewWithTag:tag];
if (av) {
[av removeFromSuperview];
}
}
-(UIStoryboard*) mediaHandlerGetStoryBoard
{
return self.storyboard;
}
///////////////////////////////////////////
-(void) chatCellViewButtonPressed:(XMPPMessageCoreDataObject*)selectedMessage
{
_selectedMessageToView = selectedMessage;
if (_selectedMessageToView.type == @"coordinate") {
[self performSegueWithIdentifier:@"mapSegue" sender:self];
} else if (_selectedMessageToView.type == @"contact") {
NSString *messageBody = _selectedMessageToView.body;
NSArray *contactInfo = [messageBody componentsSeparatedByString:@","];
[_mediaHandler addContactToList:[contactInfo objectAtIndex:0] withLastName:[contactInfo objectAtIndex:1] andMobileNo:[contactInfo objectAtIndex:2] withExtraNumber:[contactInfo objectAtIndex:3]];
} else {
[self performSegueWithIdentifier:@"mediaViewSegue" sender:self];
}
}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"mediaViewSegue"]) {
[segue.destinationViewController setContext:self.context];
[segue.destinationViewController setFromUsername:_chatWith];
[segue.destinationViewController setMessage:_selectedMessageToView];
} else if ([segue.identifier isEqualToString:@"mapSegue"]) {
[segue.destinationViewController setMessage:_selectedMessageToView];
}
}
@end