This repository has been archived by the owner on Feb 19, 2020. It is now read-only.
/
BITFeedbackComposeViewController.m
705 lines (544 loc) · 28.6 KB
/
BITFeedbackComposeViewController.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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
/*
* Author: Andreas Linde <mail@andreaslinde.de>
*
* Copyright (c) 2012-2014 HockeyApp, Bit Stadium GmbH.
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#import "HockeySDK.h"
#if HOCKEYSDK_FEATURE_FEEDBACK
#import "HockeySDKPrivate.h"
#import "BITFeedbackManagerPrivate.h"
#import "BITFeedbackMessageAttachment.h"
#import "BITFeedbackComposeViewController.h"
#import "BITFeedbackUserDataViewController.h"
#import "BITHockeyBaseManagerPrivate.h"
#import "BITHockeyHelper.h"
#import "BITImageAnnotationViewController.h"
#import "BITHockeyAttachment.h"
@interface BITFeedbackComposeViewController () <BITFeedbackUserDataDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIActionSheetDelegate, BITImageAnnotationDelegate> {
}
@property (nonatomic, weak) BITFeedbackManager *manager;
@property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) UIView *contentViewContainer;
@property (nonatomic, strong) UIScrollView *attachmentScrollView;
@property (nonatomic, strong) NSMutableArray *attachmentScrollViewImageViews;
@property (nonatomic, strong) UIButton *addPhotoButton;
@property (nonatomic, strong) NSString *text;
@property (nonatomic, strong) NSMutableArray *attachments;
@property (nonatomic, strong) NSMutableArray *imageAttachments;
@property (nonatomic, strong) UIView *textAccessoryView;
@property (nonatomic) NSInteger selectedAttachmentIndex;
@property (nonatomic, strong) UITapGestureRecognizer *tapRecognizer;
/**
* Workaround for UIImagePickerController bug.
* The statusBar shows up when the UIImagePickerController opens.
* The status bar does not disappear again when the UIImagePickerController is dismissed.
* Therefore store the state when UIImagePickerController is shown and restore when viewWillAppear gets called.
*/
@property (nonatomic, strong) NSNumber *isStatusBarHiddenBeforeShowingPhotoPicker;
@end
@implementation BITFeedbackComposeViewController {
BOOL _blockUserDataScreen;
BOOL _actionSheetVisible;
}
#pragma mark - NSObject
- (instancetype)init {
self = [super init];
if (self) {
_blockUserDataScreen = NO;
_actionSheetVisible = NO;
_delegate = nil;
_manager = [BITHockeyManager sharedHockeyManager].feedbackManager;
_attachments = [NSMutableArray new];
_imageAttachments = [NSMutableArray new];
_attachmentScrollViewImageViews = [NSMutableArray new];
_tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewTapped:)];
[_attachmentScrollView addGestureRecognizer:self.tapRecognizer];
_text = nil;
}
return self;
}
#pragma mark - Public
- (void)prepareWithItems:(NSArray *)items {
for (id item in items) {
if ([item isKindOfClass:[NSString class]]) {
self.text = [(self.text ? self.text : @"") stringByAppendingFormat:@"%@%@", (self.text ? @" " : @""), item];
} else if ([item isKindOfClass:[NSURL class]]) {
self.text = [(self.text ? self.text : @"") stringByAppendingFormat:@"%@%@", (self.text ? @" " : @""), [(NSURL *)item absoluteString]];
} else if ([item isKindOfClass:[UIImage class]]) {
UIImage *image = item;
BITFeedbackMessageAttachment *attachment = [BITFeedbackMessageAttachment attachmentWithData:UIImageJPEGRepresentation(image, 0.7f) contentType:@"image/jpeg"];
attachment.originalFilename = [NSString stringWithFormat:@"Image_%li.jpg", (unsigned long)[self.attachments count]];
[self.attachments addObject:attachment];
[self.imageAttachments addObject:attachment];
} else if ([item isKindOfClass:[NSData class]]) {
BITFeedbackMessageAttachment *attachment = [BITFeedbackMessageAttachment attachmentWithData:item contentType:@"application/octet-stream"];
attachment.originalFilename = [NSString stringWithFormat:@"Attachment_%li.data", (unsigned long)[self.attachments count]];
[self.attachments addObject:attachment];
} else if ([item isKindOfClass:[BITHockeyAttachment class]]) {
BITHockeyAttachment *sourceAttachment = (BITHockeyAttachment *)item;
if (!sourceAttachment.hockeyAttachmentData) {
BITHockeyLog(@"BITHockeyAttachment instance doesn't contain any data.");
continue;
}
NSString *filename = [NSString stringWithFormat:@"Attachment_%li.data", (unsigned long)[self.attachments count]];
if (sourceAttachment.filename) {
filename = sourceAttachment.filename;
}
BITFeedbackMessageAttachment *attachment = [BITFeedbackMessageAttachment attachmentWithData:sourceAttachment.hockeyAttachmentData contentType:sourceAttachment.contentType];
attachment.originalFilename = filename;
[self.attachments addObject:attachment];
} else {
BITHockeyLog(@"Unknown item type %@", item);
}
}
}
#pragma mark - Keyboard
- (void)keyboardWasShown:(NSNotification*)aNotification {
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
BOOL isPortraitOrientation = NO;
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_8_0
isPortraitOrientation = UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]);
#else
isPortraitOrientation = UIInterfaceOrientationIsPortrait(self.interfaceOrientation);
#endif
CGRect frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
if (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPad) {
if (!bit_isPreiOS8Environment() || isPortraitOrientation) {
frame.size.height -= kbSize.height;
} else {
frame.size.height -= kbSize.width;
}
} else {
CGSize windowSize = [[UIScreen mainScreen] bounds].size;
CGFloat windowHeight = windowSize.height - 20;
CGFloat navBarHeight = self.navigationController.navigationBar.frame.size.height;
if (!bit_isPreiOS8Environment() || isPortraitOrientation) {
CGFloat modalGap = (windowHeight - self.view.bounds.size.height) / 2;
frame.size.height = windowHeight - navBarHeight - kbSize.height;
if (bit_isPreiOS8Environment()) {
frame.size.height -= modalGap;
}
} else {
windowHeight = windowSize.width - 20;
CGFloat modalGap = 0.0f;
if (windowHeight - kbSize.width < self.view.bounds.size.height) {
modalGap = 30;
} else {
modalGap = (windowHeight - self.view.bounds.size.height) / 2;
}
frame.size.height = windowSize.width - navBarHeight - modalGap - kbSize.width;
}
}
[self.contentViewContainer setFrame:frame];
[self performSelector:@selector(refreshAttachmentScrollview) withObject:nil afterDelay:0.0f];
}
- (void)keyboardWillBeHidden:(NSNotification*)aNotification {
CGRect frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[self.contentViewContainer setFrame:frame];
}
#pragma mark - View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
self.title = BITHockeyLocalizedString(@"HockeyFeedbackComposeTitle");
self.view.backgroundColor = [UIColor whiteColor];
// Do any additional setup after loading the view.
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:@selector(dismissAction:)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:BITHockeyLocalizedString(@"HockeyFeedbackComposeSend")
style:UIBarButtonItemStyleDone
target:self
action:@selector(sendAction:)];
// Container that contains both the textfield and eventually the photo scroll view on the right side
self.contentViewContainer = [[UIView alloc] initWithFrame:self.view.bounds];
self.contentViewContainer.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
[self.view addSubview:self.contentViewContainer];
// message input textfield
self.textView = [[UITextView alloc] initWithFrame:self.view.bounds];
self.textView.font = [UIFont systemFontOfSize:17];
self.textView.delegate = self;
self.textView.backgroundColor = [UIColor whiteColor];
self.textView.returnKeyType = UIReturnKeyDefault;
self.textView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
[self.contentViewContainer addSubview:self.textView];
// Add Photo Button + Container that's displayed above the keyboard.
self.textAccessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 44)];
self.textAccessoryView.backgroundColor = [UIColor colorWithRed:0.9f green:0.9f blue:0.9f alpha:1.0f];
self.addPhotoButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.addPhotoButton setTitle:BITHockeyLocalizedString(@"HockeyFeedbackComposeAttachmentAddImage") forState:UIControlStateNormal];
[self.addPhotoButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
[self.addPhotoButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled];
self.addPhotoButton.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 44);
[self.addPhotoButton addTarget:self action:@selector(addPhotoAction:) forControlEvents:UIControlEventTouchUpInside];
self.addPhotoButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin;
[self.textAccessoryView addSubview:self.addPhotoButton];
if (!self.hideImageAttachmentButton) {
self.textView.inputAccessoryView = self.textAccessoryView;
}
// This could be a subclass, yet
self.attachmentScrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
self.attachmentScrollView.scrollEnabled = YES;
self.attachmentScrollView.bounces = YES;
self.attachmentScrollView.autoresizesSubviews = NO;
self.attachmentScrollView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleRightMargin;
[self.contentViewContainer addSubview:self.attachmentScrollView];
}
- (void)viewWillAppear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
self.manager.currentFeedbackComposeViewController = self;
[super viewWillAppear:animated];
if (_text && self.textView.text.length == 0) {
self.textView.text = _text;
}
if (self.isStatusBarHiddenBeforeShowingPhotoPicker) {
// requires iOS 7
if ([self respondsToSelector:@selector(prefersStatusBarHidden)]) {
[self setNeedsStatusBarAppearanceUpdate];
} else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[[UIApplication sharedApplication] setStatusBarHidden:self.isStatusBarHiddenBeforeShowingPhotoPicker.boolValue];
#pragma clang diagnostic pop
}
}
self.isStatusBarHiddenBeforeShowingPhotoPicker = nil;
[self updateBarButtonState];
}
- (BOOL)prefersStatusBarHidden {
if (self.isStatusBarHiddenBeforeShowingPhotoPicker) {
return self.isStatusBarHiddenBeforeShowingPhotoPicker.boolValue;
}
return [super prefersStatusBarHidden];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if ([self.manager askManualUserDataAvailable] &&
([self.manager requireManualUserDataMissing] ||
![self.manager didAskUserData])
) {
if (!_blockUserDataScreen)
[self setUserDataAction];
} else {
// Invoke delayed to fix iOS 7 iPad landscape bug, where this view will be moved if not called delayed
[self.textView performSelector:@selector(becomeFirstResponder) withObject:nil afterDelay:0.0];
[self refreshAttachmentScrollview];
}
}
- (void)viewWillDisappear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
self.manager.currentFeedbackComposeViewController = nil;
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
}
- (void)refreshAttachmentScrollview {
CGFloat scrollViewWidth = 0;
if (self.imageAttachments.count){
scrollViewWidth = 100;
}
CGRect textViewFrame = self.textView.frame;
CGRect scrollViewFrame = self.attachmentScrollView.frame;
BOOL alreadySetup = CGRectGetWidth(scrollViewFrame) > 0;
if (alreadySetup && self.imageAttachments.count == 0) {
textViewFrame.size.width += 100;
self.textView.frame = textViewFrame;
scrollViewFrame.size.width = 0;
self.attachmentScrollView.frame = scrollViewFrame;
return;
}
if (!alreadySetup) {
CGSize tempTextViewSize = CGSizeMake(self.contentViewContainer.frame.size.width, self.contentViewContainer.frame.size.height);
textViewFrame.size = tempTextViewSize;
textViewFrame.size.width -= scrollViewWidth;
// height has to be identical to the textview!
scrollViewFrame = CGRectMake(CGRectGetMaxX(textViewFrame), self.view.frame.origin.y, scrollViewWidth, CGRectGetHeight(self.textView.bounds));
self.textView.frame = textViewFrame;
self.attachmentScrollView.frame = scrollViewFrame;
self.attachmentScrollView.contentInset = self.textView.contentInset;
}
if (self.imageAttachments.count > self.attachmentScrollViewImageViews.count){
NSInteger numberOfViewsToCreate = self.imageAttachments.count - self.attachmentScrollViewImageViews.count;
for (int i = 0; i <numberOfViewsToCreate; i++) {
UIButton *newImageButton = [UIButton buttonWithType:UIButtonTypeCustom];
[newImageButton addTarget:self action:@selector(imageButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.attachmentScrollViewImageViews addObject:newImageButton];
[self.attachmentScrollView addSubview:newImageButton];
}
}
int index = 0;
CGFloat currentYOffset = 0.0f;
NSEnumerator *reverseAttachments = self.imageAttachments.reverseObjectEnumerator;
for (BITFeedbackMessageAttachment *attachment in reverseAttachments.allObjects){
UIButton *imageButton = self.attachmentScrollViewImageViews[index];
UIImage *image = [attachment thumbnailWithSize:CGSizeMake(100, 100)];
// determine the factor by which we scale..
CGFloat scaleFactor = CGRectGetWidth(self.attachmentScrollView.frame) / image.size.width;
CGFloat height = image.size.height * scaleFactor;
imageButton.frame = CGRectInset(CGRectMake(0, currentYOffset, scaleFactor * image.size.width, height), 10, 10);
currentYOffset += height;
[imageButton setImage:image forState:UIControlStateNormal];
index++;
}
[self.attachmentScrollView setContentSize:CGSizeMake(CGRectGetWidth(self.attachmentScrollView.frame), currentYOffset)];
[self updateBarButtonState];
}
- (void)updateBarButtonState {
if (self.textView.text.length > 0 ) {
self.navigationItem.rightBarButtonItem.enabled = YES;
} else {
self.navigationItem.rightBarButtonItem.enabled = NO;
}
if (self.imageAttachments.count > 2){
[self.addPhotoButton setEnabled:NO];
} else {
[self.addPhotoButton setEnabled:YES];
}
}
- (void)removeAttachmentScrollView {
CGRect frame = self.attachmentScrollView.frame;
frame.size.width = 0;
self.attachmentScrollView.frame = frame;
frame = self.textView.frame;
frame.size.width += 100;
self.textView.frame = frame;
}
#pragma mark - UIViewController Rotation
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation {
return YES;
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
[self removeAttachmentScrollView];
[self refreshAttachmentScrollview];
}
#pragma mark - Private methods
- (void)setUserDataAction {
BITFeedbackUserDataViewController *userController = [[BITFeedbackUserDataViewController alloc] initWithStyle:UITableViewStyleGrouped];
userController.delegate = self;
UINavigationController *navController = [self.manager customNavigationControllerWithRootViewController:userController
presentationStyle:UIModalPresentationFormSheet];
[self presentViewController:navController animated:YES completion:nil];
}
#pragma mark - Actions
- (void)dismissAction:(id)sender {
for (BITFeedbackMessageAttachment *attachment in self.attachments){
[attachment deleteContents];
}
[self dismissWithResult:BITFeedbackComposeResultCancelled];
}
- (void)sendAction:(id)sender {
if ([self.textView isFirstResponder])
[self.textView resignFirstResponder];
NSString *text = self.textView.text;
[self.manager submitMessageWithText:text andAttachments:self.attachments];
[self dismissWithResult:BITFeedbackComposeResultSubmitted];
}
- (void)dismissWithResult:(BITFeedbackComposeResult) result {
if([self.delegate respondsToSelector:@selector(feedbackComposeViewController:didFinishWithResult:)]) {
[self.delegate feedbackComposeViewController:self didFinishWithResult:result];
} else if ([self.delegate respondsToSelector:@selector(feedbackComposeViewControllerDidFinish:)]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated"
[self.delegate feedbackComposeViewControllerDidFinish:self];
#pragma clang diagnostic pop
} else {
[self dismissViewControllerAnimated:YES completion:nil];
}
}
- (void)addPhotoAction:(id)sender {
if (_actionSheetVisible) return;
self.isStatusBarHiddenBeforeShowingPhotoPicker = @([[UIApplication sharedApplication] isStatusBarHidden]);
// add photo.
UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
pickerController.delegate = self;
pickerController.editing = NO;
[self presentViewController:pickerController animated:YES completion:nil];
}
- (void)scrollViewTapped:(id)unused {
UIMenuController *menuController = [UIMenuController sharedMenuController];
[menuController setTargetRect:CGRectMake([self.tapRecognizer locationInView:self.view].x, [self.tapRecognizer locationInView:self.view].x, 1, 1) inView:self.view];
[menuController setMenuVisible:YES animated:YES];
}
- (void)paste:(id)sender {
}
#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *pickedImage = info[UIImagePickerControllerOriginalImage];
if (pickedImage){
NSData *imageData = UIImageJPEGRepresentation(pickedImage, 0.7f);
BITFeedbackMessageAttachment *newAttachment = [BITFeedbackMessageAttachment attachmentWithData:imageData contentType:@"image/jpeg"];
NSURL *imagePath = [info objectForKey:@"UIImagePickerControllerReferenceURL"];
NSString *imageName = [imagePath lastPathComponent];
newAttachment.originalFilename = imageName;
[self.attachments addObject:newAttachment];
[self.imageAttachments addObject:newAttachment];
}
[picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)imageButtonAction:(UIButton *)sender {
// determine the index of the feedback
NSInteger index = [self.attachmentScrollViewImageViews indexOfObject:sender];
self.selectedAttachmentIndex = (self.attachmentScrollViewImageViews.count - index - 1);
/* We won't use this for now until we have a more robust solution for displaying UIAlertController
// requires iOS 8
id uialertcontrollerClass = NSClassFromString(@"UIAlertController");
if (uialertcontrollerClass) {
__weak typeof(self) weakSelf = self;
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:BITHockeyLocalizedString(@"HockeyFeedbackComposeAttachmentCancel")
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * action) {
typeof(self) strongSelf = weakSelf;
[strongSelf cancelAction];
_actionSheetVisible = NO;
}];
[alertController addAction:cancelAction];
UIAlertAction *editAction = [UIAlertAction actionWithTitle:BITHockeyLocalizedString(@"HockeyFeedbackComposeAttachmentEdit")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
typeof(self) strongSelf = weakSelf;
[strongSelf editAction];
_actionSheetVisible = NO;
}];
[alertController addAction:editAction];
UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:BITHockeyLocalizedString(@"HockeyFeedbackComposeAttachmentDelete")
style:UIAlertActionStyleDestructive
handler:^(UIAlertAction * action) {
typeof(self) strongSelf = weakSelf;
[strongSelf deleteAction];
_actionSheetVisible = NO;
}];
[alertController addAction:deleteAction];
[self presentViewController:alertController animated:YES completion:nil];
} else {
*/
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle: nil
delegate: self
cancelButtonTitle: BITHockeyLocalizedString(@"HockeyFeedbackComposeAttachmentCancel")
destructiveButtonTitle: BITHockeyLocalizedString(@"HockeyFeedbackComposeAttachmentDelete")
otherButtonTitles: BITHockeyLocalizedString(@"HockeyFeedbackComposeAttachmentEdit"), nil];
[actionSheet showFromRect: sender.frame inView: self.attachmentScrollView animated: YES];
#pragma clang diagnostic push
/*}*/
_actionSheetVisible = YES;
if ((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || ([[NSProcessInfo processInfo] respondsToSelector:@selector(isOperatingSystemAtLeastVersion:)] && [[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion){9,0,0}])) {
[self.textView resignFirstResponder];
}
}
#pragma mark - BITFeedbackUserDataDelegate
- (void)userDataUpdateCancelled {
_blockUserDataScreen = YES;
if ([self.manager requireManualUserDataMissing]) {
if ([self.navigationController respondsToSelector:@selector(dismissViewControllerAnimated:completion:)]) {
[self.navigationController dismissViewControllerAnimated:YES
completion:^(void) {
[self dismissViewControllerAnimated:YES completion:nil];
}];
} else {
[self dismissViewControllerAnimated:YES completion:nil];
[self performSelector:@selector(dismissAction:) withObject:nil afterDelay:0.4];
}
} else {
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
}
- (void)userDataUpdateFinished {
[self.manager saveMessages];
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView {
[self updateBarButtonState];
}
#pragma mark - UIActionSheet Delegate
- (void)deleteAction {
if (self.selectedAttachmentIndex != NSNotFound){
UIButton *imageButton = self.attachmentScrollViewImageViews[self.selectedAttachmentIndex];
BITFeedbackMessageAttachment *attachment = self.imageAttachments[self.selectedAttachmentIndex];
[attachment deleteContents]; // mandatory call to delete the files associated.
[self.imageAttachments removeObject:attachment];
[self.attachments removeObject:attachment];
[imageButton removeFromSuperview];
[self.attachmentScrollViewImageViews removeObject:imageButton];
}
self.selectedAttachmentIndex = NSNotFound;
[self refreshAttachmentScrollview];
if ((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || ([[NSProcessInfo processInfo] respondsToSelector:@selector(isOperatingSystemAtLeastVersion:)] && [[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion){9,0,0}])) {
[self.textView becomeFirstResponder];
}
}
- (void)editAction {
if (self.selectedAttachmentIndex != NSNotFound){
BITFeedbackMessageAttachment *attachment = self.imageAttachments[self.selectedAttachmentIndex];
BITImageAnnotationViewController *annotationEditor = [[BITImageAnnotationViewController alloc ] init];
annotationEditor.delegate = self;
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:annotationEditor];
annotationEditor.image = attachment.imageRepresentation;
[self presentViewController:navController animated:YES completion:nil];
}
}
- (void)cancelAction {
if ((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) || ([[NSProcessInfo processInfo] respondsToSelector:@selector(isOperatingSystemAtLeastVersion:)] && [[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion){9,0,0}])) {
[self.textView becomeFirstResponder];
}
}
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex == [actionSheet destructiveButtonIndex]) {
[self deleteAction];
} else if (buttonIndex != [actionSheet cancelButtonIndex]) {
[self editAction];
} else {
[self cancelAction];
}
_actionSheetVisible = NO;
}
#pragma mark - Image Annotation Delegate
- (void)annotationController:(BITImageAnnotationViewController *)annotationController didFinishWithImage:(UIImage *)image {
if (self.selectedAttachmentIndex != NSNotFound){
BITFeedbackMessageAttachment *attachment = self.imageAttachments[self.selectedAttachmentIndex];
[attachment replaceData:UIImageJPEGRepresentation(image, 0.7f)];
}
self.selectedAttachmentIndex = NSNotFound;
}
- (void)annotationControllerDidCancel:(BITImageAnnotationViewController *)annotationController {
self.selectedAttachmentIndex = NSNotFound;
}
@end
#endif /* HOCKEYSDK_FEATURE_FEEDBACK */