-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathUIMessageInputView.m
executable file
·1035 lines (936 loc) · 42.7 KB
/
UIMessageInputView.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
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// UIMessageInputView.m
// Coding_iOS
//
// Created by 王 原闯 on 14-9-11.
// Copyright (c) 2014年 Coding. All rights reserved.
//
#define kKeyboardView_Height 216.0
#define kMessageInputView_Height 50.0
#define kMessageInputView_HeightMax 120.0
#define kMessageInputView_PadingHeight 7.0
#define kMessageInputView_Width_Tool 35.0
#define kMessageInputView_MediaPadding 1.0
#import "UIMessageInputView.h"
#import "UIPlaceHolderTextView.h"
#import "UIMessageInputView_Add.h"
#import "UIMessageInputView_CCell.h"
#import "UIMessageInputView_Voice.h"
//at某人的功能
#import "UsersViewController.h"
#import "ProjectMemberListViewController.h"
#import "Users.h"
#import "Login.h"
#import "UICustomCollectionView.h"
#import "QBImagePickerController.h"
#import "Helper.h"
#import "Coding_FileManager.h"
#import <MBProgressHUD/MBProgressHUD.h>
#import "MJPhotoBrowser.h"
#import "FunctionTipsManager.h"
static NSMutableDictionary *_inputStrDict, *_inputMediaDict;
@interface UIMessageInputView () <AGEmojiKeyboardViewDelegate, AGEmojiKeyboardViewDataSource, QBImagePickerControllerDelegate, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (strong, nonatomic) AGEmojiKeyboardView *emojiKeyboardView;
@property (strong, nonatomic) UIMessageInputView_Add *addKeyboardView;
@property (strong, nonatomic) UIMessageInputView_Voice *voiceKeyboardView;
@property (strong, nonatomic) UIScrollView *contentView;
@property (strong, nonatomic) UIPlaceHolderTextView *inputTextView;
@property (strong, nonatomic) UIButton *arrowKeyboardButton;
@property (strong, nonatomic) UICustomCollectionView *mediaView;
@property (strong, nonatomic) NSMutableArray *mediaList, *uploadMediaList;
@property (strong, nonatomic) UIButton *addButton, *emotionButton, *photoButton, *voiceButton;
@property (assign, nonatomic) CGFloat viewHeightOld;
@property (assign, nonatomic) UIMessageInputViewState inputState;
@property (strong, nonatomic) MBProgressHUD *HUD;
@property (strong, nonatomic) NSString *uploadingPhotoName;
@end
@implementation UIMessageInputView
- (void)setFrame:(CGRect)frame{
CGFloat oldheightToBottom = kScreen_Height - CGRectGetMinY(self.frame);
CGFloat newheightToBottom = kScreen_Height - CGRectGetMinY(frame);
[super setFrame:frame];
if (fabs(oldheightToBottom - newheightToBottom) > 1.0) {
DebugLog(@"heightToBottom-----:%.2f", newheightToBottom);
if (oldheightToBottom > newheightToBottom) {//降下去的时候保存
[self saveInputStr];
[self saveInputMedia];
}
if (_delegate && [_delegate respondsToSelector:@selector(messageInputView:heightToBottomChenged:)]) {
[self.delegate messageInputView:self heightToBottomChenged:newheightToBottom];
}
}
}
- (void)p_setY:(CGFloat)y{
if (ABS(kScreen_Height - CGRectGetHeight(self.frame) - y) < 1.0) {
y -= kSafeArea_Bottom;
}
[self setY:y];
}
- (void)setInputState:(UIMessageInputViewState)inputState{
if (_inputState != inputState) {
_inputState = inputState;
switch (_inputState) {
case UIMessageInputViewStateSystem:
{
[self.addButton setImage:[UIImage imageNamed:@"keyboard_add"] forState:UIControlStateNormal];
[self.emotionButton setImage:[UIImage imageNamed:@"keyboard_emotion"] forState:UIControlStateNormal];
[self.voiceButton setImage:[UIImage imageNamed:@"keyboard_voice"] forState:UIControlStateNormal];
}
break;
case UIMessageInputViewStateEmotion:
{
[self.addButton setImage:[UIImage imageNamed:@"keyboard_add"] forState:UIControlStateNormal];
[self.emotionButton setImage:[UIImage imageNamed:@"keyboard_keyboard"] forState:UIControlStateNormal];
[self.voiceButton setImage:[UIImage imageNamed:@"keyboard_voice"] forState:UIControlStateNormal];
}
break;
case UIMessageInputViewStateAdd:
{
[self.addButton setImage:[UIImage imageNamed:@"keyboard_keyboard"] forState:UIControlStateNormal];
[self.emotionButton setImage:[UIImage imageNamed:@"keyboard_emotion"] forState:UIControlStateNormal];
[self.voiceButton setImage:[UIImage imageNamed:@"keyboard_voice"] forState:UIControlStateNormal];
}
break;
case UIMessageInputViewStateVoice:
{
[self.addButton setImage:[UIImage imageNamed:@"keyboard_add"] forState:UIControlStateNormal];
[self.emotionButton setImage:[UIImage imageNamed:@"keyboard_emotion"] forState:UIControlStateNormal];
[self.voiceButton setImage:[UIImage imageNamed:@"keyboard_keyboard"] forState:UIControlStateNormal];
}
break;
default:
break;
}
_contentView.hidden = _inputState == UIMessageInputViewStateVoice;
_arrowKeyboardButton.hidden = !_contentView.hidden;
[self updateContentViewBecauseOfMedia:NO];
_arrowKeyboardButton.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
}
}
- (void)setPlaceHolder:(NSString *)placeHolder{
_placeHolder = placeHolder;
if (_inputTextView && ![_inputTextView.placeholder isEqualToString:placeHolder]) {
_inputTextView.placeholder = placeHolder;
}
}
- (NSMutableArray *)mediaList{
if (!_mediaList) {
_mediaList =[[NSMutableArray alloc] init];
}
return _mediaList;
}
- (void)mediaListChenged{
[self saveInputMedia];
if (_mediaView) {
[self.mediaView reloadData];
[self updateContentViewBecauseOfMedia:YES];
}
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.backgroundColor = kColorNavBG;
[self addLineUp:YES andDown:NO andColor:kColorCCC];
_viewHeightOld = CGRectGetHeight(frame);
_inputState = UIMessageInputViewStateSystem;
_isAlwaysShow = NO;
_curProject = nil;
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(didPan:)];
[self addGestureRecognizer:panGesture];
//补个底色背景
UIView *bottomV = [UIView new];
bottomV.backgroundColor = self.backgroundColor;
[self addSubview:bottomV];
[bottomV mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(kSafeArea_Bottom + kKeyboardView_Height);
make.top.equalTo(self.mas_bottom);
make.left.right.equalTo(self);
}];
}
return self;
}
- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)didPan:(UIPanGestureRecognizer *)panGesture
{
if (panGesture.state == UIGestureRecognizerStateChanged) {
CGFloat verticalDiff = [panGesture translationInView:self].y;
if (verticalDiff > 60) {
[self isAndResignFirstResponder];
}
}
}
#pragma mark remember input
- (NSMutableDictionary *)shareInputStrDict{
if (!_inputStrDict) {
_inputStrDict = [[NSMutableDictionary alloc] init];
}
return _inputStrDict;
}
- (NSMutableDictionary *)shareInputMediaDict{
if (!_inputMediaDict) {
_inputMediaDict = [[NSMutableDictionary alloc] init];
}
return _inputMediaDict;
}
- (NSString *)inputKey{
NSString *inputKey = nil;
if (_contentType == UIMessageInputViewContentTypePriMsg) {
inputKey = [NSString stringWithFormat:@"privateMessage_%@", self.toUser.global_key];
}else{
if (_commentOfId) {
switch (_contentType) {
case UIMessageInputViewContentTypeTweet:
inputKey = [NSString stringWithFormat:@"tweet_%@_%@", _commentOfId.stringValue, _toUser.global_key.length > 0? _toUser.global_key:@""];
break;
case UIMessageInputViewContentTypeTopic:
inputKey = [NSString stringWithFormat:@"topic_%@_%@", _commentOfId.stringValue, _toUser.global_key.length > 0? _toUser.global_key:@""];
break;
case UIMessageInputViewContentTypeTask:
inputKey = [NSString stringWithFormat:@"task_%@_%@", _commentOfId.stringValue, _toUser.global_key.length > 0? _toUser.global_key:@""];
break;
default:
break;
}
}
}
return inputKey;
}
- (NSString *)inputStr{
NSString *inputKey = [self inputKey];
if (inputKey) {
return [[self shareInputStrDict] objectForKey:inputKey];
}
return nil;
}
- (void)deleteInputData{
NSString *inputKey = [self inputKey];
if (inputKey) {
[[self shareInputStrDict] removeObjectForKey:inputKey];
[[self shareInputMediaDict] removeObjectForKey:inputKey];
}
}
- (void)saveInputStr{
NSString *inputStr = _inputTextView.text;
NSString *inputKey = [self inputKey];
if (inputKey && inputKey.length > 0) {
if (inputStr && inputStr.length > 0) {
[[self shareInputStrDict] setObject:inputStr forKey:inputKey];
}else{
[[self shareInputStrDict] removeObjectForKey:inputKey];
}
}
}
- (void)saveInputMedia{
NSString *inputKey = [self inputKey];
if (inputKey && inputKey.length > 0) {
if (_mediaList.count > 0) {
[[self shareInputMediaDict] setObject:_mediaList forKey:inputKey];
}else{
[[self shareInputMediaDict] removeObjectForKey:inputKey];
}
}
}
- (NSMutableArray *)inputMedia{
NSString *inputKey = [self inputKey];
if (inputKey) {
return [[self shareInputMediaDict] objectForKey:inputKey];
}
return nil;
}
- (void)setToUser:(User *)toUser{
_toUser = toUser;
NSString *inputStr = [self inputStr];
if (_inputTextView) {
if (_contentType != UIMessageInputViewContentTypePriMsg) {
_inputTextView.placeholder = _toUser? [NSString stringWithFormat:@"回复 %@", _toUser.name]: _placeHolder;
}else{
self.placeHolder = _placeHolder;
}
_inputTextView.selectedRange = NSMakeRange(0, _inputTextView.text.length);
[_inputTextView insertText:inputStr? inputStr: @""];
_mediaList = [self inputMedia];
[self mediaListChenged];
}
}
#pragma mark Public M
- (void)prepareToShow{
if ([self superview] == kKeyWindow) {
return;
}
[self p_setY:kScreen_Height];
[kKeyWindow addSubview:self];
[kKeyWindow addSubview:_emojiKeyboardView];
[kKeyWindow addSubview:_addKeyboardView];
[kKeyWindow addSubview:_voiceKeyboardView];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
if (_isAlwaysShow && ![self isCustomFirstResponder]) {
[UIView animateWithDuration:0.25 animations:^{
[self p_setY:kScreen_Height - CGRectGetHeight(self.frame)];
}];
}
}
- (void)prepareToDismiss{
if ([self superview] == nil) {
return;
}
[self isAndResignFirstResponder];
[UIView animateWithDuration:0.25 delay:0.0 options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{
[self p_setY:kScreen_Height];
} completion:^(BOOL finished) {
[_emojiKeyboardView removeFromSuperview];
[_addKeyboardView removeFromSuperview];
[_voiceKeyboardView removeFromSuperview];
[self removeFromSuperview];
}];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (BOOL)notAndBecomeFirstResponder{
self.inputState = UIMessageInputViewStateSystem;
if ([_inputTextView isFirstResponder]) {
return NO;
}else{
[_inputTextView becomeFirstResponder];
return YES;
}
}
- (BOOL)isAndResignFirstResponder{
if (self.inputState == UIMessageInputViewStateAdd || self.inputState == UIMessageInputViewStateEmotion || self.inputState == UIMessageInputViewStateVoice) {
[UIView animateWithDuration:0.25 delay:0.0f options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{
[_emojiKeyboardView setY:kScreen_Height];
[_addKeyboardView setY:kScreen_Height];
[_voiceKeyboardView setY:kScreen_Height];
if (self.isAlwaysShow) {
[self p_setY:kScreen_Height- CGRectGetHeight(self.frame)];
}else{
[self p_setY:kScreen_Height];
}
} completion:^(BOOL finished) {
self.inputState = UIMessageInputViewStateSystem;
}];
return YES;
}else{
if ([_inputTextView isFirstResponder]) {
[_inputTextView resignFirstResponder];
return YES;
}else{
return NO;
}
}
}
- (BOOL)isCustomFirstResponder{
return ([_inputTextView isFirstResponder] || self.inputState == UIMessageInputViewStateAdd || self.inputState == UIMessageInputViewStateEmotion || self.inputState == UIMessageInputViewStateVoice);
}
- (CGFloat)heightWithSafeArea{
return CGRectGetHeight(self.frame) + kSafeArea_Bottom;
}
+ (instancetype)messageInputViewWithType:(UIMessageInputViewContentType)type{
return [self messageInputViewWithType:type placeHolder:nil];
}
+ (instancetype)messageInputViewWithType:(UIMessageInputViewContentType)type placeHolder:(NSString *)placeHolder{
UIMessageInputView *messageInputView = [[UIMessageInputView alloc] initWithFrame:CGRectMake(0, kScreen_Height, kScreen_Width, kMessageInputView_Height)];
[messageInputView customUIWithType:type];
if (placeHolder) {
messageInputView.placeHolder = placeHolder;
}else{
messageInputView.placeHolder = @"撰写评论";
}
return messageInputView;
}
- (void)customUIWithType:(UIMessageInputViewContentType)type{
_contentType = type;
CGFloat contentViewHeight = kMessageInputView_Height -2*kMessageInputView_PadingHeight;
NSInteger toolBtnNum;
BOOL hasEmotionBtn, hasAddBtn, hasPhotoBtn, hasVoiceBtn;
BOOL showBigEmotion;
switch (_contentType) {
case UIMessageInputViewContentTypeTweet:
{
toolBtnNum = 1;
hasEmotionBtn = YES;
hasAddBtn = NO;
hasPhotoBtn = NO;
showBigEmotion = NO;
hasVoiceBtn = NO;
}
break;
case UIMessageInputViewContentTypePriMsg:
{
toolBtnNum = 2;
hasEmotionBtn = YES;
hasAddBtn = YES;
hasPhotoBtn = NO;
showBigEmotion = YES;
hasVoiceBtn = [NSObject isPrivateCloud].boolValue? NO: YES;
}
break;
case UIMessageInputViewContentTypeTopic:
case UIMessageInputViewContentTypeTask:
{
toolBtnNum = 1;
hasEmotionBtn = NO;
hasAddBtn = NO;
hasPhotoBtn = YES;
showBigEmotion = NO;
hasVoiceBtn = NO;
}
break;
default:
toolBtnNum = 1;
hasEmotionBtn = NO;
hasAddBtn = NO;
hasPhotoBtn = NO;
showBigEmotion = NO;
hasVoiceBtn = NO;
break;
}
__weak typeof(self) weakSelf = self;
if (!_contentView) {
_contentView = [[UIScrollView alloc] init];
_contentView.backgroundColor = [UIColor whiteColor];
_contentView.layer.borderWidth = 0.5;
_contentView.layer.borderColor = [UIColor lightGrayColor].CGColor;
_contentView.layer.cornerRadius = contentViewHeight/2;
_contentView.layer.masksToBounds = YES;
_contentView.alwaysBounceVertical = YES;
[self addSubview:_contentView];
[_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
CGFloat left = hasVoiceBtn ? (7+kMessageInputView_Width_Tool+7) : kPaddingLeftWidth;
make.edges.equalTo(self).insets(UIEdgeInsetsMake(kMessageInputView_PadingHeight, left, kMessageInputView_PadingHeight, kPaddingLeftWidth + toolBtnNum *kMessageInputView_Width_Tool));
}];
}
if (!_inputTextView) {
_inputTextView = [[UIPlaceHolderTextView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width - kPaddingLeftWidth - toolBtnNum *kMessageInputView_Width_Tool - (hasVoiceBtn ? 7+kMessageInputView_Width_Tool+7 : kPaddingLeftWidth), contentViewHeight)];
_inputTextView.font = [UIFont systemFontOfSize:16];
_inputTextView.returnKeyType = UIReturnKeySend;
_inputTextView.scrollsToTop = NO;
_inputTextView.delegate = self;
//输入框缩进
UIEdgeInsets insets = _inputTextView.textContainerInset;
insets.left += 8.0;
insets.right += 8.0;
_inputTextView.textContainerInset = insets;
[self.contentView addSubview:_inputTextView];
}
if (hasEmotionBtn && !_emotionButton) {
_emotionButton = [[UIButton alloc] initWithFrame:CGRectMake(kScreen_Width - kPaddingLeftWidth/2 - toolBtnNum * kMessageInputView_Width_Tool, (kMessageInputView_Height - kMessageInputView_Width_Tool)/2, kMessageInputView_Width_Tool, kMessageInputView_Width_Tool)];
[_emotionButton setImage:[UIImage imageNamed:@"keyboard_emotion"] forState:UIControlStateNormal];
[_emotionButton addTarget:self action:@selector(emotionButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_emotionButton];
}
_emotionButton.hidden = !hasEmotionBtn;
if (hasAddBtn && !_addButton) {
_addButton = [[UIButton alloc] initWithFrame:CGRectMake(kScreen_Width - kPaddingLeftWidth/2 -kMessageInputView_Width_Tool, (kMessageInputView_Height - kMessageInputView_Width_Tool)/2, kMessageInputView_Width_Tool, kMessageInputView_Width_Tool)];
[_addButton setImage:[UIImage imageNamed:@"keyboard_add"] forState:UIControlStateNormal];
[_addButton addTarget:self action:@selector(addButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_addButton];
}
_addButton.hidden = !hasAddBtn;
if (hasPhotoBtn && !_photoButton) {
_photoButton = [[UIButton alloc] initWithFrame:CGRectMake(kScreen_Width - kPaddingLeftWidth/2 -kMessageInputView_Width_Tool, (kMessageInputView_Height - kMessageInputView_Width_Tool)/2, kMessageInputView_Width_Tool, kMessageInputView_Width_Tool)];
[_photoButton setImage:[UIImage imageNamed:@"keyboard_photo"] forState:UIControlStateNormal];
[_photoButton addTarget:self action:@selector(photoButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_photoButton];
}
_photoButton.hidden = !hasPhotoBtn;
if (hasEmotionBtn && !_emojiKeyboardView) {
_emojiKeyboardView = [[AGEmojiKeyboardView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, kKeyboardView_Height) dataSource:self showBigEmotion:showBigEmotion];
_emojiKeyboardView.delegate = self;
[_emojiKeyboardView setY:kScreen_Height];
}
if (hasAddBtn && !_addKeyboardView) {
_addKeyboardView = [[UIMessageInputView_Add alloc] initWithFrame:CGRectMake(0, kScreen_Height, kScreen_Width, kKeyboardView_Height)];
_addKeyboardView.addIndexBlock = ^(NSInteger index){
if ([weakSelf.delegate respondsToSelector:@selector(messageInputView:addIndexClicked:)]) {
[weakSelf.delegate messageInputView:weakSelf addIndexClicked:index];
}
};
}
if (hasPhotoBtn && !_mediaView) {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
_mediaView = [[UICustomCollectionView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(_inputTextView.frame), CGRectGetWidth(_inputTextView.frame), 1) collectionViewLayout:layout];
_mediaView.scrollEnabled = NO;
[_mediaView setBackgroundView:nil];
[_mediaView setBackgroundColor:[UIColor clearColor]];
[_mediaView registerClass:[UIMessageInputView_CCell class] forCellWithReuseIdentifier:kCCellIdentifier_UIMessageInputView_CCell];
_mediaView.dataSource = self;
_mediaView.delegate = self;
[self.contentView addSubview:_mediaView];
}
if (_inputTextView) {
[[RACObserve(self.inputTextView, contentSize) takeUntil:self.rac_willDeallocSignal] subscribeNext:^(NSValue *contentSize) {
[weakSelf updateContentViewBecauseOfMedia:NO];
}];
}
if (hasPhotoBtn) {
//监听-上传文件成功
[[[[NSNotificationCenter defaultCenter] rac_addObserverForName:kNotificationUploadCompled object:nil] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(NSNotification *aNotification) {
//{NSURLResponse: response, NSError: error, ProjectFile: data}
NSDictionary* userInfo = [aNotification userInfo];
[self completionUploadWithResult:[userInfo objectForKey:@"data"] error:[userInfo objectForKey:@"error"]];
}];
}
if (hasVoiceBtn && !_voiceButton) {
_voiceButton = [[UIButton alloc] initWithFrame:CGRectMake(7, (kMessageInputView_Height - kMessageInputView_Width_Tool)/2, kMessageInputView_Width_Tool, kMessageInputView_Width_Tool)];
[_voiceButton setImage:[UIImage imageNamed:@"keyboard_voice"] forState:UIControlStateNormal];
[_voiceButton addTarget:self action:@selector(voiceButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_voiceButton];
}
_voiceButton.hidden = !hasVoiceBtn;
if (hasVoiceBtn && !_arrowKeyboardButton) {
_arrowKeyboardButton = [UIButton buttonWithType:UIButtonTypeCustom];
_arrowKeyboardButton.frame = CGRectMake(0, 0, kMessageInputView_Width_Tool, kMessageInputView_Width_Tool);
[_arrowKeyboardButton setImage:[UIImage imageNamed:@"keyboard_arrow_down"] forState:UIControlStateNormal];
[_arrowKeyboardButton addTarget:self action:@selector(arrowButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_arrowKeyboardButton];
}
_arrowKeyboardButton.hidden = YES;
if (hasVoiceBtn && !_voiceKeyboardView) {
_voiceKeyboardView = [[UIMessageInputView_Voice alloc] initWithFrame:CGRectMake(0, kScreen_Height, kScreen_Width, kKeyboardView_Height)];
_voiceKeyboardView.recordSuccessfully = ^(NSString *file, NSTimeInterval duration){
if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(messageInputView:sendVoice:duration:)]) {
[weakSelf.delegate messageInputView:weakSelf sendVoice:file duration:duration];
}
};
}
}
- (void)updateContentViewBecauseOfMedia:(BOOL)becauseOfMedia{
CGSize textSize = _inputTextView.contentSize, mediaSize = CGSizeZero;
if (!becauseOfMedia) {
if (ABS(CGRectGetHeight(_inputTextView.frame) - textSize.height) > 0.5) {
[_inputTextView setHeight:textSize.height];
}
}
if (_mediaView) {
CGFloat collectionItemHeight;
if (_mediaList.count <= 0) {
collectionItemHeight = 0;
}else if (_mediaList.count == 1) {
collectionItemHeight = 0.6* CGRectGetWidth(_inputTextView.frame);
}else if (_mediaList.count == 2){
collectionItemHeight = (CGRectGetWidth(_inputTextView.frame) - kMessageInputView_MediaPadding)/3;
}else{
collectionItemHeight = (CGRectGetWidth(_inputTextView.frame) - 2* kMessageInputView_MediaPadding)/3;
}
CGFloat mediaHeight = ceilf(_mediaList.count/3.0)* (collectionItemHeight+ kMessageInputView_MediaPadding) - kMessageInputView_MediaPadding;
mediaSize = CGSizeMake(CGRectGetWidth(_mediaView.frame), mediaHeight);
CGRect mediaFrame = CGRectMake(0, CGRectGetMaxY(_inputTextView.frame), mediaSize.width, mediaSize.height);
if (!CGRectEqualToRect(mediaFrame, _mediaView.frame)) {
_mediaView.frame = mediaFrame;
}
}
if (_contentView.hidden) {
textSize.height = kMessageInputView_Height - 2*kMessageInputView_PadingHeight;
}
CGSize contentSize = CGSizeMake(textSize.width, textSize.height + mediaSize.height);
CGFloat selfHeight = MAX(kMessageInputView_Height, contentSize.height + 2*kMessageInputView_PadingHeight);
CGFloat maxSelfHeight = kScreen_Height/2;
if (kDevice_Is_iPhone5){
maxSelfHeight = 230;
}else if (kDevice_Is_iPhone6) {
maxSelfHeight = 290;
}else if (kDevice_Is_iPhone6Plus){
maxSelfHeight = kScreen_Height/2;
}else{
maxSelfHeight = 140;
}
selfHeight = MIN(maxSelfHeight, selfHeight);
CGFloat diffHeight = selfHeight - _viewHeightOld;
if (ABS(diffHeight) > 0.5) {
CGRect selfFrame = self.frame;
selfFrame.size.height += diffHeight;
selfFrame.origin.y -= diffHeight;
[self setFrame:selfFrame];
self.viewHeightOld = selfHeight;
}
[self.contentView setContentSize:contentSize];
CGFloat bottomY = becauseOfMedia? contentSize.height: textSize.height;
CGFloat offsetY = MAX(0, bottomY - (CGRectGetHeight(self.frame)- 2* kMessageInputView_PadingHeight));
[self.contentView setContentOffset:CGPointMake(0, offsetY) animated:YES];
}
#pragma mark Collection M
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return _mediaList.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UIMessageInputView_CCell *ccell = [collectionView dequeueReusableCellWithReuseIdentifier:kCCellIdentifier_UIMessageInputView_CCell forIndexPath:indexPath];
[ccell setCurMedia:[_mediaList objectAtIndex:indexPath.row] andTotalCount:_mediaList.count];
@weakify(self);
ccell.deleteBlock = ^(UIMessageInputView_Media *toDelete){
@strongify(self);
[self.mediaList removeObject:toDelete];
[self mediaListChenged];
};
return ccell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
CGSize ccellSize;
CGFloat contentWidth = CGRectGetWidth(_inputTextView.frame);
if (_mediaList.count <= 0) {
ccellSize = CGSizeZero;
}else if (_mediaList.count == 1) {
ccellSize = CGSizeMake(contentWidth, 0.6* contentWidth);
}else if (_mediaList.count == 2){
ccellSize = CGSizeMake((contentWidth - kMessageInputView_MediaPadding)/2, (contentWidth - kMessageInputView_MediaPadding)/3);
}else{
ccellSize = CGSizeMake((contentWidth - 2* kMessageInputView_MediaPadding)/3, (contentWidth - 2* kMessageInputView_MediaPadding)/3);
}
return ccellSize;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsZero;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
return kMessageInputView_MediaPadding;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
return kMessageInputView_MediaPadding;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
[self isAndResignFirstResponder];
// 显示大图
int count = (int)_mediaList.count;
NSMutableArray *photos = [NSMutableArray arrayWithCapacity:count];
for (int i = 0; i<count; i++) {
UIMessageInputView_Media *mediaItem = [_mediaList objectAtIndex:i];
MJPhoto *photo = [[MJPhoto alloc] init];
PHAsset *asset = [PHAsset assetWithLocalIdentifier:mediaItem.assetID];
if (asset) {
mediaItem.curAsset = asset;
photo.image = asset.loadImage;
}else{
mediaItem.curAsset = nil;
photo.url = [NSURL URLWithString:mediaItem.urlStr]; // 图片路径
}
[photos addObject:photo];
}
// 2.显示相册
MJPhotoBrowser *browser = [[MJPhotoBrowser alloc] init];
browser.currentPhotoIndex = indexPath.row; // 弹出相册时显示的第一张图片是?
browser.photos = photos; // 设置所有的图片
[browser show];
}
#pragma mark addButton M
- (void)addButtonClicked:(id)sender{
CGFloat endY = kScreen_Height;
if (self.inputState == UIMessageInputViewStateAdd) {
self.inputState = UIMessageInputViewStateSystem;
[_inputTextView becomeFirstResponder];
}else{
self.inputState = UIMessageInputViewStateAdd;
[_inputTextView resignFirstResponder];
endY = kScreen_Height - kKeyboardView_Height - kSafeArea_Bottom;
}
[UIView animateWithDuration:0.25 delay:0.0f options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{
[_addKeyboardView setY:endY];
[_emojiKeyboardView setY:kScreen_Height];
[_voiceKeyboardView setY:kScreen_Height];
if (ABS(kScreen_Height - endY) > 1.0) {
[self p_setY:endY- CGRectGetHeight(self.frame)];
}
} completion:^(BOOL finished) {
}];
}
- (void)emotionButtonClicked:(id)sender{
CGFloat endY = kScreen_Height;
if (self.inputState == UIMessageInputViewStateEmotion) {
self.inputState = UIMessageInputViewStateSystem;
[_inputTextView becomeFirstResponder];
}else{
self.inputState = UIMessageInputViewStateEmotion;
[_inputTextView resignFirstResponder];
endY = kScreen_Height - kKeyboardView_Height - kSafeArea_Bottom;
}
[UIView animateWithDuration:0.25 delay:0.0f options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{
[_emojiKeyboardView setY:endY];
[_addKeyboardView setY:kScreen_Height];
[_voiceKeyboardView setY:kScreen_Height];
if (ABS(kScreen_Height - endY) > 1.0) {
[self p_setY:endY- CGRectGetHeight(self.frame)];
}
} completion:^(BOOL finished) {
}];
}
- (void)photoButtonClicked:(id)sender{
// 相册
if (![Helper checkPhotoLibraryAuthorizationStatus]) {
return;
}
[self isAndResignFirstResponder];
QBImagePickerController *imagePickerController = [[QBImagePickerController alloc] init];
imagePickerController.mediaType = QBImagePickerMediaTypeImage;
imagePickerController.delegate = self;
imagePickerController.allowsMultipleSelection = YES;
imagePickerController.maximumNumberOfSelection = 6;
[[BaseViewController presentingVC] presentViewController:imagePickerController animated:YES completion:^{
}];
}
- (void)voiceButtonClicked:(id)sender {
CGFloat endY = kScreen_Height;
if (self.inputState == UIMessageInputViewStateVoice) {
self.inputState = UIMessageInputViewStateSystem;
[_inputTextView becomeFirstResponder];
} else {
self.inputState = UIMessageInputViewStateVoice;
[_inputTextView resignFirstResponder];
endY = kScreen_Height - kKeyboardView_Height - kSafeArea_Bottom;
}
[UIView animateWithDuration:0.25 delay:0.0f options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{
[_voiceKeyboardView setY:endY];
[_emojiKeyboardView setY:kScreen_Height];
[_addKeyboardView setY:kScreen_Height];
if (ABS(kScreen_Height - endY) > 1.0) {
[self p_setY:endY- CGRectGetHeight(self.frame)];
}
} completion:^(BOOL finished) {
}];
}
- (void)arrowButtonClicked:(id)sender {
[self isAndResignFirstResponder];
}
#pragma mark QBImagePickerControllerDelegate
- (void)qb_imagePickerController:(QBImagePickerController *)imagePickerController didFinishPickingAssets:(NSArray *)assets{
_uploadMediaList = [[NSMutableArray alloc] initWithCapacity:assets.count];
for (PHAsset *asset in assets) {
[_uploadMediaList addObject:[UIMessageInputView_Media mediaWithAsset:asset urlStr:nil]];
}
[self doUploadMediaList];
}
- (void)qb_imagePickerControllerDidCancel:(QBImagePickerController *)imagePickerController{
[[BaseViewController presentingVC] dismissViewControllerAnimated:YES completion:nil];
}
#pragma uploadMedia
- (void)doUploadMediaList{
__block UIMessageInputView_Media *media = nil;
__block NSInteger index = 0;
[_uploadMediaList enumerateObjectsUsingBlock:^(UIMessageInputView_Media *obj, NSUInteger idx, BOOL *stop) {
if (obj.state == UIMessageInputView_MediaStateInit) {
media = obj;
index = idx;
*stop = YES;
}
}];
if (media && media.curAsset) {
[self doUploadMedia:media withIndex:index];
}else{
[self hudTipWillShow:nil];
[[BaseViewController presentingVC] dismissViewControllerAnimated:YES completion:nil];
}
}
- (void)doUploadMedia:(UIMessageInputView_Media *)media withIndex:(NSInteger)index{
//保存到app内
NSString* originalFileName = media.curAsset.fileName;;
NSString *fileName = [NSString stringWithFormat:@"%@|||%@|||%@", self.curProject.id.stringValue, @"0", originalFileName];
if ([Coding_FileManager writeUploadDataWithName:fileName andAsset:media.curAsset]) {
[self hudTipWillShow:[NSString stringWithFormat:@"正在上传第 %ld 张图片...", (long)index +1]];
media.state = UIMessageInputView_MediaStateUploading;
self.uploadingPhotoName = originalFileName;
Coding_UploadTask *uploadTask =[[Coding_FileManager sharedManager] addUploadTaskWithFileName:fileName projectIsPublic:_curProject.is_public.boolValue];
@weakify(self)
[RACObserve(uploadTask, progress.fractionCompleted) subscribeNext:^(NSNumber *fractionCompleted) {
@strongify(self);
dispatch_async(dispatch_get_main_queue(), ^{
if (self.HUD) {
self.HUD.progress = MAX(0, fractionCompleted.floatValue-0.05);
DebugLog(@"uploadingPhotoName - %@ : %.2f", self.uploadingPhotoName, fractionCompleted.floatValue);
}
});
}];
}else{
media.state = UIMessageInputView_MediaStateUploadFailed;
[NSObject showHudTipStr:[NSString stringWithFormat:@"%@ 文件处理失败", originalFileName]];
}
}
- (void)completionUploadWithResult:(id)responseObject error:(NSError *)error{
//移除文件(共有项目不能自动移除)
NSString *diskFileName = [NSString stringWithFormat:@"%@|||%@|||%@", self.curProject.id.stringValue, @"0", self.uploadingPhotoName];
[Coding_FileManager deleteUploadDataWithName:diskFileName];
__block UIMessageInputView_Media *media = nil;
[_uploadMediaList enumerateObjectsUsingBlock:^(UIMessageInputView_Media *obj, NSUInteger idx, BOOL *stop) {
if (obj.state == UIMessageInputView_MediaStateUploading) {
media = obj;
*stop = YES;
}
}];
if (!media) {
return;
}else{
if (responseObject) {
NSString *fileName = nil, *fileUrlStr = @"";
if ([responseObject isKindOfClass:[NSString class]]) {
fileUrlStr = responseObject;
}else if ([responseObject isKindOfClass:[ProjectFile class]]){
ProjectFile *curFile = responseObject;
fileName = curFile.name;
fileUrlStr = curFile.owner_preview;
}
if (!fileName || [fileName isEqualToString:self.uploadingPhotoName]) {
media.urlStr = fileUrlStr;
media.state = UIMessageInputView_MediaStateUploadSucess;
[self.mediaList addObject:media];
[self mediaListChenged];
}
}
if (media.state != UIMessageInputView_MediaStateUploadSucess) {
media.state = UIMessageInputView_MediaStateUploadFailed;
}
[self doUploadMediaList];
}
}
- (void)hudTipWillShow:(NSString *)tipStr{
if (tipStr.length > 0) {
[self resignFirstResponder];
if (!_HUD) {
_HUD = [MBProgressHUD showHUDAddedTo:kKeyWindow animated:YES];
_HUD.mode = MBProgressHUDModeDeterminateHorizontalBar;
_HUD.removeFromSuperViewOnHide = YES;
}else{
[kKeyWindow addSubview:_HUD];
[_HUD show:NO];
}
_HUD.progress = 0;
_HUD.labelText = tipStr;
}else{
[_HUD hide:NO];
}
}
#pragma mark UITextViewDelegate M
- (void)sendTextStr{
[self deleteInputData];
NSMutableString *sendStr = [NSMutableString stringWithString:self.inputTextView.text];
if (_mediaList.count > 0) {
[_mediaList enumerateObjectsUsingBlock:^(UIMessageInputView_Media *obj, NSUInteger idx, BOOL *stop) {
[sendStr appendFormat:@"\n", obj.urlStr];
}];
}
if (sendStr && ![sendStr isEmpty] && _delegate && [_delegate respondsToSelector:@selector(messageInputView:sendText:)]) {
[self.delegate messageInputView:self sendText:sendStr];
}
[_mediaList removeAllObjects];
_inputTextView.selectedRange = NSMakeRange(0, _inputTextView.text.length);
[_inputTextView insertText:@""];
[self updateContentViewBecauseOfMedia:NO];
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
if ([text isEqualToString:@"\n"]) {
if (![self.inputTextView.text hasListenChar]) {
[self sendTextStr];
}
return NO;
}else if ([text isEqualToString:@"@"]){
__weak typeof(self) weakSelf = self;
if (self.curProject) {
//@项目成员
[ProjectMemberListViewController showATSomeoneWithBlock:^(User *curUser) {
[weakSelf atSomeUser:curUser inTextView:textView andRange:range];
} withProject:self.curProject];
}else{
//@好友
[UsersViewController showATSomeoneWithBlock:^(User *curUser) {
[weakSelf atSomeUser:curUser inTextView:textView andRange:range];
}];
}
return NO;
}
return YES;
}
- (void)atSomeUser:(User *)curUser inTextView:(UITextView *)textView andRange:(NSRange)range{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardChange:) name:UIKeyboardDidChangeFrameNotification object:nil];//英文键盘,重新弹出键盘时,没有WillChange的通知
NSString *appendingStr;
if (curUser) {
appendingStr = [NSString stringWithFormat:@"@%@ ", curUser.name];
}else{
appendingStr = @"@";
}
[textView setSelectedRange:range];
[textView insertText:appendingStr];
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
if (self.inputState != UIMessageInputViewStateSystem) {
self.inputState = UIMessageInputViewStateSystem;
[UIView animateWithDuration:0.25 delay:0.0f options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{
[_emojiKeyboardView setY:kScreen_Height];
[_addKeyboardView setY:kScreen_Height];
[_voiceKeyboardView setY:kScreen_Height];
} completion:^(BOOL finished) {
self.inputState = UIMessageInputViewStateSystem;
}];
}
return YES;
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
if (self.inputState == UIMessageInputViewStateSystem) {
[UIView animateWithDuration:0.25 delay:0.0f options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{
if (_isAlwaysShow) {
[self p_setY:kScreen_Height- CGRectGetHeight(self.frame)];
}else{
[self p_setY:kScreen_Height];
}
} completion:^(BOOL finished) {
}];
}
return YES;
}
#pragma mark - KeyBoard Notification Handlers
- (void)keyboardChange:(NSNotification*)aNotification{
if ([aNotification name] == UIKeyboardDidChangeFrameNotification) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidChangeFrameNotification object:nil];
}
if (self.inputState == UIMessageInputViewStateSystem && [self.inputTextView isFirstResponder]) {
NSDictionary* userInfo = [aNotification userInfo];
CGRect keyboardEndFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat keyboardY = keyboardEndFrame.origin.y;
CGFloat selfOriginY = keyboardY+1 > kScreen_Height? self.isAlwaysShow? kScreen_Height - CGRectGetHeight(self.frame): kScreen_Height : keyboardY - CGRectGetHeight(self.frame);
if (selfOriginY == self.frame.origin.y) {
return;
}
__weak typeof(self) weakSelf = self;
void (^endFrameBlock)() = ^(){
[weakSelf p_setY:selfOriginY];
};
if ([aNotification name] == UIKeyboardWillChangeFrameNotification) {
NSTimeInterval animationDuration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve animationCurve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
[UIView animateWithDuration:animationDuration delay:0.0f options:[UIView animationOptionsForCurve:animationCurve] animations:^{
endFrameBlock();
} completion:nil];
}else{
endFrameBlock();
}
}
}
#pragma mark AGEmojiKeyboardView
- (void)emojiKeyBoardView:(AGEmojiKeyboardView *)emojiKeyBoardView didUseEmoji:(NSString *)emoji {
NSString *emotion_monkey = [emoji emotionSpecailName];