-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostPicViewController.m
672 lines (508 loc) · 27 KB
/
PostPicViewController.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
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
//
// PostPicViewController.m
// Teamstory
//
// Created by Freddy Hidalgo-Monchez on 2014-11-24.
//
//
#import "ALAssetsLibrary+CustomPhotoAlbum.h"
#import "PostPicViewController.h"
#import "PAPUtility.h"
#import "SVProgressHUD.h"
#import "PAPTabBarController.h"
#import "PAPHomeViewController.h"
#import "PAPAccountViewController.h"
#import "Mixpanel.h"
#import <FlightRecorder/FlightRecorder.h>
#import "AtMention.h"
#import "AppDelegate.h"
@interface PostPicViewController ()
// ui variables
@property (nonatomic, strong) UIScrollView *cropScrollView;
@property (nonatomic, strong) UIImageView *cropImgView;
@property (nonatomic, strong) UITextView *descriptionTextView;
@property (nonatomic, strong) UIImage *originalImg;
@property (nonatomic, strong) UIImage *croppedImg;
@property (nonatomic, strong) UIBarButtonItem *rightNavButton;
@property (nonatomic, strong) NSString *placeholderText;
@property (nonatomic, strong) UIColor *placeholderTextColor;
//@property (nonatomic, strong) NSMutableArray *userArray;
//@property (nonatomic, strong) NSString *atmentionSearchString;
//@property (nonatomic, strong) UITableView *autocompleteTableView;
//@property (nonatomic, strong) NSArray *filteredArray;
//@property (nonatomic, strong) NSString *cellType;
//@property (nonatomic, strong) PFQuery *userQuery;
//@property (nonatomic, strong) NSMutableArray *atmentionUserArray;
//@property (nonatomic, strong) UIView *dimView;
//@property NSInteger text_location;
//@property NSInteger atmentionLength;
//@property NSRange atmentionRange;
// upload variables
@property (nonatomic, strong) PFFile *photoFile;
@property (nonatomic, strong) PFFile *thumbnailFile;
@property (nonatomic, assign) UIBackgroundTaskIdentifier fileUploadBackgroundTaskId;
@property (nonatomic, assign) UIBackgroundTaskIdentifier photoPostBackgroundTaskId;
@property (nonatomic, strong) ALAssetsLibrary *assetsLibrary;
@property (nonatomic, strong) NSString *source;
@end
@implementation PostPicViewController
- (id)initWithImage:(UIImage *)originalImg source:(NSString *)source
{
self = [super init];
if (self) {
if (!originalImg) {
return nil;
}
self.source = source;
self.originalImg = originalImg;
self.fileUploadBackgroundTaskId = UIBackgroundTaskInvalid;
self.photoPostBackgroundTaskId = UIBackgroundTaskInvalid;
self.placeholderText = @"Add a description to your moment...";
self.placeholderTextColor = [UIColor colorWithRed:174.0/255.0 green:174.0/255.0 blue:174.0/255.0 alpha:1];
}
return self;
}
- (void)viewWillAppear:(BOOL)animated{
// google analytics
[PAPUtility captureScreenGA:@"Post Pic Upload"];
// mixpanel analytics
[[Mixpanel sharedInstance] track:@"Viewed Screen" properties:@{@"Type" : @"Post Photo"}];
// flightrecorder event analytics
[[FlightRecorder sharedInstance] trackEventWithCategory:@"post_photo_screen" action:@"viewing_post_photo" label:@"" value:@""];
// flightrecorder analytics
[[FlightRecorder sharedInstance] trackPageView:@"Post Photo"];
// change proportions based on iphone height
float cropScrollHeight;
float descriptionTextViewHeight;
if ([UIScreen mainScreen].bounds.size.height > 480) {
cropScrollHeight = 320;
descriptionTextViewHeight = 180;
}else{
cropScrollHeight = 290;
descriptionTextViewHeight = 120;
}
// set view's background
[self.view setBackgroundColor:[UIColor whiteColor]];
// scrollview set up
self.cropScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, [UIScreen mainScreen].bounds.size.width, cropScrollHeight)];
[self.cropScrollView setDelegate:self];
[self.cropScrollView setBackgroundColor:[UIColor blackColor]];
[self.cropScrollView setShowsHorizontalScrollIndicator:NO];
[self.cropScrollView setShowsVerticalScrollIndicator:NO];
[self.cropScrollView setMaximumZoomScale:2.0];
// image view set up
self.cropImgView = [[UIImageView alloc] initWithImage:self.originalImg];
[self.cropImgView setBackgroundColor:[UIColor redColor]];
[self.cropImgView setFrame:CGRectMake(0.0, 0.0, self.originalImg.size.width, self.originalImg.size.height)];
[self.cropScrollView setContentSize:self.originalImg.size];
// calculate minimum zoom for selected image
CGRect scrollViewFrame = self.cropScrollView.frame;
CGFloat scaleWidth = scrollViewFrame.size.width / self.cropScrollView.contentSize.width;
CGFloat scaleHeight = scrollViewFrame.size.height / self.cropScrollView.contentSize.height;
CGFloat minScale = MAX(scaleWidth,scaleHeight);
// set imageview and add to scrollview
[self.cropScrollView setMinimumZoomScale:minScale];
[self.cropScrollView setZoomScale:minScale];
[self.cropScrollView addSubview:self.cropImgView];
// center image by manipulting scrollview's offset
CGFloat newContentOffsetX = (self.cropScrollView.contentSize.width/2) - (self.cropScrollView.bounds.size.width/2);
[self.cropScrollView setContentOffset:CGPointMake(newContentOffsetX, 0)];
[self.view addSubview:self.cropScrollView];
// create text view placed below crop area
self.descriptionTextView = [[UITextView alloc]initWithFrame:CGRectMake(10, self.cropScrollView.frame.origin.y + self.cropScrollView.frame.size.height + 8, [UIScreen mainScreen].bounds.size.width - 20, descriptionTextViewHeight)];
[self.descriptionTextView setDelegate:self];
[self.descriptionTextView setText:self.placeholderText];
[self.descriptionTextView setFont:[UIFont systemFontOfSize:15.0f]];
[self.descriptionTextView setTextColor:self.placeholderTextColor];
[self.view addSubview:self.descriptionTextView];
/*
self.dimView = [[UIView alloc] init];
self.dimView.hidden = YES;
self.dimView.backgroundColor = [UIColor colorWithWhite:0.5f alpha:0.8f];
[self.view addSubview:self.dimView];
self.autocompleteTableView = [[UITableView alloc] init];
self.autocompleteTableView.delegate = self;
self.autocompleteTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.autocompleteTableView.dataSource = self;
self.autocompleteTableView.scrollEnabled = YES;
self.autocompleteTableView.hidden = YES;
[self.view addSubview:self.autocompleteTableView];
*/
}
-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:NO];
self.firstPicViewController = NO;
}
- (void)viewDidLoad {
[super viewDidLoad];
// init nav bar
[[self navigationController] setNavigationBarHidden:NO animated:YES];
// set color of nav bar to custom grey
[self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:(79/255.0) green:(91/255.0) blue:(100/255.0) alpha:(0.0/255.0)];
self.navigationController.navigationBar.translucent = NO;
self.navigationItem.title = @"Update Moment";
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"button_cancel.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backButtonAction)];
[self.navigationItem.leftBarButtonItem setTintColor:[UIColor whiteColor]];
self.rightNavButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"button_done.png"] style:UIBarButtonItemStylePlain target:self action:@selector(cropPressed)];
self.rightNavButton.tintColor = [UIColor whiteColor];
self.navigationItem.rightBarButtonItem = self.rightNavButton;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
UITapGestureRecognizer *tapOutside = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tapOutside];
}
#pragma mark - Crop Methods
- (UIImage *)cropImage{
float zoomScale = 1.0 / [self.cropScrollView zoomScale];
CGRect rect;
rect.origin.x = [self.cropScrollView contentOffset].x * zoomScale;
rect.origin.y = [self.cropScrollView contentOffset].y * zoomScale;
rect.size.width = [self.cropScrollView bounds].size.width * zoomScale;
rect.size.height = [self.cropScrollView bounds].size.height * zoomScale;
CGImageRef cr = CGImageCreateWithImageInRect([[self.cropImgView image] CGImage], rect);
UIImage *cropped = [UIImage imageWithCGImage:cr];
CGImageRelease(cr);
// check if crop has been used
if(self.cropScrollView.contentOffset.y > 0){
// Mixpanel analytics
[[Mixpanel sharedInstance] track:@"Used photo crop" properties:@{}];
}
return cropped;
}
- (UIImage *)processImage{
// Cropping & resizing picture
UIImage *resizedCroppedPic = [PAPUtility resizeImage:[self cropImage] width:640 height:640];
return resizedCroppedPic;
}
- (void)cropPressed {
// increment activity point
[[ActivityPointSystem sharedActivityPointSystem] addPointToActivityCount:@"post"];
// show spinning indicator
[SVProgressHUD show];
// mixpanel analytics
[[Mixpanel sharedInstance] track:@"Engaged" properties:@{@"Type": @"Core", @"Action": @"Posted Moment"}];
// intercom analytics
[Intercom logEventWithName:@"posted-moment" metaData:@{@"":@""}];
// increment user photo count by one
[[Mixpanel sharedInstance].people increment:@"Photo Count" by:[NSNumber numberWithInt:1]];
//resize cropped image (work on background thread)
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
// crop and resize image from scroll view
self.croppedImg = [self processImage];
dispatch_async(dispatch_get_main_queue(), ^{
// save image to teamstory folder
[self saveImageToCustomFolder:self.croppedImg];
// upload pic to server
[self startUploadProcess:self.croppedImg];
});
});
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return self.cropImgView;
}
#pragma mark - Upload Methods
- (void)saveImageToCustomFolder:(UIImage *)anImage{
// Save image to custom photo album
void (^failure)(NSError *) = ^(NSError *error) {
if (error == nil) return;
NSLog(@"Photo failed to save in album: %@", error);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Couldn't save photo in album :( Check your internet connection or contact us at [email protected]" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Dismiss", nil];
[alert show];
};
self.assetsLibrary = [[ALAssetsLibrary alloc] init];
[self.assetsLibrary saveImage:anImage
toAlbum:@"Teamstory"
completion:nil
failure:failure];
}
- (void)startUploadProcess:(UIImage *)anImage {
UIImage *thumbnailImage = [PAPUtility resizeImage:anImage width:86.0f height:86.0f];
// JPEG to decrease file size and enable faster uploads & downloads
NSData *imageData = UIImageJPEGRepresentation(anImage, 1.0f);
NSData *thumbnailImageData = UIImagePNGRepresentation(thumbnailImage);
if (!imageData || !thumbnailImageData) {
return;
}
self.photoFile = [PFFile fileWithData:imageData];
self.thumbnailFile = [PFFile fileWithData:thumbnailImageData];
// Request a background execution task to allow us to finish uploading the photo even if the app is backgrounded
self.fileUploadBackgroundTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:self.fileUploadBackgroundTaskId];
}];
NSLog(@"Requested background expiration task with id %d for Teamstory photo upload", (int)self.fileUploadBackgroundTaskId);
[self.photoFile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (succeeded) {
NSLog(@"Photo uploaded successfully");
[self.thumbnailFile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (succeeded) {
NSLog(@"Thumbnail uploaded successfully");
[self uploadFinishedImage:anImage];
}
[[UIApplication sharedApplication] endBackgroundTask:self.fileUploadBackgroundTaskId];
}];
} else {
[[UIApplication sharedApplication] endBackgroundTask:self.fileUploadBackgroundTaskId];
}
}];
}
- (void)uploadFinishedImage:(UIImage *)anImage {
// prepare comment for saving
NSDictionary *userInfo = [NSDictionary dictionary];
NSString *trimmedComment = [self.descriptionTextView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if (trimmedComment.length != 0) {
userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
trimmedComment,kPAPEditPhotoViewControllerUserInfoCommentKey,
nil];
}
if (!self.photoFile || !self.thumbnailFile) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Couldn't post your photo" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Dismiss", nil];
[alert show];
return;
}
// both files have finished uploading
// create a photo object
PFObject *photo = [PFObject objectWithClassName:kPAPPhotoClassKey];
[photo setObject:[PFUser currentUser] forKey:kPAPPhotoUserKey];
[photo setObject:self.photoFile forKey:kPAPPhotoPictureKey];
[photo setObject:self.thumbnailFile forKey:kPAPPhotoThumbnailKey];
[photo setObject:@"picture" forKey:kPAPPhotoType];
[photo setObject:[NSNumber numberWithInt:0] forKey:@"discoverCount"];
// set comment to photo
if (userInfo) {
NSString *commentText = [userInfo objectForKey:kPAPEditPhotoViewControllerUserInfoCommentKey];
if (commentText && commentText.length != 0 && ![commentText isEqualToString:self.placeholderText]) {
[photo setObject:commentText forKey:@"caption"];
}
}
// photos are public, but may only be modified by the user who uploaded them
PFACL *photoACL = [PFACL ACLWithUser:[PFUser currentUser]];
[photoACL setPublicReadAccess:YES];
photo.ACL = photoACL;
// Request a background execution task to allow us to finish uploading the photo even if the app is backgrounded
self.photoPostBackgroundTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:self.photoPostBackgroundTaskId];
}];
// upload to server
[photo saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (succeeded) {
NSLog(@"Photo uploaded");
// create activity for posted
[PAPUtility posted:photo];
[[PAPCache sharedCache] setAttributesForPhoto:photo likers:[NSArray array] commenters:[NSArray array] likedByCurrentUser:NO];
[[NSNotificationCenter defaultCenter] postNotificationName:PAPTabBarControllerDidFinishEditingPhotoNotification object:photo];
} else {
NSLog(@"Photo failed to save: %@", error);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Couldn't post your photo :( Check your internet connection or contact us at [email protected]" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Dismiss", nil];
[alert show];
}
[[UIApplication sharedApplication] endBackgroundTask:self.photoPostBackgroundTaskId];
[SVProgressHUD dismiss];
[self exitPhoto];
}];
}
#pragma mark - Nav Methods
-(void)dismissKeyboard {
[self.view endEditing:YES];
// self.autocompleteTableView.hidden = YES;
// self.dimView.hidden = YES;
}
- (void)backButtonAction{
if (self.firstPicViewController) {
[self dismissViewControllerAnimated:YES completion:nil];
} else {
[[self navigationController] setNavigationBarHidden:YES animated:YES];
[self.navigationController popViewControllerAnimated:YES];
}
}
- (void)exitPhoto{
[self.view endEditing:YES];
[(AppDelegate*)[[UIApplication sharedApplication] delegate] settingRootViewAsTabBarController];
}
#pragma mark - UITextView Delegate
- (void)textViewDidBeginEditing:(UITextView *)textView{
if ([[textView text] isEqualToString:self.placeholderText]) {
[textView setText:@""];
[textView setTextColor:[UIColor blackColor]];
}
}
- (void)textViewDidEndEditing:(UITextView *)textView {
if ([textView.text length] == 0) {
[textView setText:self.placeholderText];
[textView setTextColor:self.placeholderTextColor];
}
}
#pragma mark - UIKeyboard
static inline UIViewAnimationOptions animationOptionsWithCurve(UIViewAnimationCurve curve) {
return (UIViewAnimationOptions)curve << 16;
}
- (void)keyboardWillShow:(NSNotification *)note {
CGRect keyboardFrameEnd = [[note.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
NSNumber *number = [[note userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey];
UIViewAnimationCurve animationCurve = [[[note userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
float keyboardDuration = [number doubleValue];
CGFloat offset = keyboardFrameEnd.size.height;
// Check system version for keyboard offset, ios8 added suggestion bar
// Align the bottom edge of the photo with the keyboardr
if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")){
offset += 20;
}
// ---------- Animation in sync with keyboard moving up
[UIView animateWithDuration:keyboardDuration delay:0 options:animationOptionsWithCurve(animationCurve) animations:^{
[self.view setBounds:CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y + offset, self.view.bounds.size.width,self.view.bounds.size.height)];
} completion:^(BOOL finished) {
}];
}
- (void)keyboardWillHide:(NSNotification *)note {
CGRect keyboardFrameEnd = [[note.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat offset = keyboardFrameEnd.size.height;
NSNumber *number = [[note userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey];
UIViewAnimationCurve animationCurve = [[[note userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
float keyboardDuration = [number doubleValue];
// Check system version for keyboard offset, ios8 added suggestion bar
// Align the bottom edge of the photo with the keyboardr
if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")){
offset += 20;
}
// ---------- Animation in sync with keyboard moving up
[UIView animateWithDuration:keyboardDuration delay:0 options:animationOptionsWithCurve(animationCurve) animations:^{
[self.view setBounds:CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y - offset, self.view.bounds.size.width,self.view.bounds.size.height)];
} completion:^(BOOL finished) {
}];
}
#pragma mark - Mention Method
/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellID = @"atmentionCell";
// Try to dequeue a cell and create one if necessary
PAPBaseTextCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[PAPBaseTextCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
cell.delegate = self;
}
[cell setUser:[self.filteredArray objectAtIndex:indexPath.row]];
[cell setContentText:@" "];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.filteredArray count];
}
- (void)shouldPresentAccountViewForUser:(PFUser *)user {
PAPAccountViewController *accountViewController = [[PAPAccountViewController alloc] initWithNibName:@"PhotoTimelineViewController" bundle:nil];
[accountViewController setUser:user];
[self.navigationController pushViewController:accountViewController animated:YES];
}
- (void)cell:(PAPBaseTextCell *)cellView didTapUserButton:(PFUser *)aUser cellType:(NSString *)acellType{
self.cellType = acellType;
self.text_location = 0;
if (self.atmentionRange.location != NSNotFound) {
[self textView:self.descriptionTextView shouldChangeTextInRange:self.atmentionRange replacementText:[aUser objectForKey:@"displayName"]];
}
self.autocompleteTableView.hidden = YES;
self.dimView.hidden = YES;
[self.atmentionUserArray addObject:aUser];
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
if ([self.cellType isEqualToString:@"atmentionCell"]) {
text = [text stringByAppendingString:@" "];
if (range.location != NSNotFound) {
If the user presses the Delete key, the length of the range is 1 and an empty string object replaces that single character. Goes out of bounds when user presses delete and selects display name at the start of message.
long replacementRange = range.length + 1;
// Check if new range is in bounds of current text, accounting for extra key when deleting
if(replacementRange < self.descriptionTextView.text.length){
self.descriptionTextView.text = [self.descriptionTextView.text stringByReplacingCharactersInRange:NSMakeRange(range.location, replacementRange) withString:text];
}else{
self.descriptionTextView.text = [self.descriptionTextView.text stringByReplacingCharactersInRange:NSMakeRange(range.location, range.length) withString:text];
}
}
self.cellType = nil;
return YES;
}
if ([text isEqualToString:@"@"]){
[SVProgressHUD show];
CGPoint cursorPosition = [textView caretRectForPosition:textView.selectedTextRange.start].origin;
float offsetEmptyTextViewToKb = self.descriptionTextView.frame.size.height - cursorPosition.y;
[self.view setBounds:CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y - offsetEmptyTextViewToKb, self.view.bounds.size.width,self.view.bounds.size.height)];
NSLog(@"offsetToKb %f", offsetEmptyTextViewToKb);
if ([self.userArray count] < 1) {
self.userQuery = [PFUser query];
self.userQuery.limit = MAXFLOAT;
[self.userQuery whereKeyExists:@"displayName"];
[self.userQuery orderByAscending:@"displayName"];
[self.userQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
[SVProgressHUD dismiss];
if (!error) {
self.userArray = [[NSMutableArray alloc] initWithArray:objects];
self.atmentionUserArray = [[NSMutableArray alloc] init];
self.filteredArray = objects;
self.autocompleteTableView.backgroundColor = [UIColor clearColor];
} else {
NSLog(@"%@", error);
}
}]; } else {
[SVProgressHUD dismiss];
}
}
if ([self.userArray count] > 0) {
NSMutableString *updatedText = [[NSMutableString alloc] initWithString:self.descriptionTextView.text];
if (range.location == 0 || range.location == self.text_location) {
self.autocompleteTableView.hidden = YES;
self.dimView.hidden = YES;
self.text_location = 0;
} else if (range.location > 0 && [[updatedText substringWithRange:NSMakeRange(range.location - 1, 1)] isEqualToString:@"@"]) {
self.text_location = range.location;
}
if ([text isEqualToString:@""] && self.text_location > 1) {
range.location -=1;
if (self.text_location > range.location) {
self.text_location -= 1;
}
}
if (self.text_location > 0) {
if (range.location == NSNotFound) {
NSLog(@"range location not found");
} else {
self.atmentionRange = NSMakeRange(self.text_location, range.location - self.text_location);
self.atmentionSearchString = [updatedText substringWithRange:self.atmentionRange];
self.atmentionSearchString = [self.atmentionSearchString stringByAppendingString:text];
}
self.filteredArray = [self.userArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"displayName contains[c] %@", self.atmentionSearchString]];
// Check system version for keyboard offset, ios8 added suggestion bar
// Align the mention table view
self.dimView.frame = CGRectMake(0.0f, 0.0f, 320.0f, 9999.0f);
if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")){
self.autocompleteTableView.frame = CGRectMake(0, 64, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height - self.navigationController.navigationBar.frame.size.height - self.descriptionTextView.frame.size.height - 273);
}else{
self.autocompleteTableView.frame = CGRectMake(0, 64, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height - self.navigationController.navigationBar.frame.size.height - self.descriptionTextView.frame.size.height - 273 + 37);
}
if ([self.filteredArray count] < 1) {
self.dimView.hidden = YES;
} else {
self.dimView.hidden = NO;
}
self.autocompleteTableView.hidden = NO;
[self.autocompleteTableView reloadData];
}
}
return YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
*/
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
@end