123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935 |
- //
- // ChatInputBar.m
- // WFChat UIKit
- //
- // Created by WF Chat on 2017/10/28.
- // Copyright © 2017年 WildFireChat. All rights reserved.
- //
- #import <AVFoundation/AVFoundation.h>
- #import <CoreText/CoreText.h>
- #import "WFCUChatInputBar.h"
- #import "WFCUFaceBoard.h"
- #import "WFCUVoiceRecordView.h"
- #import "WFCUPluginBoardView.h"
- #import "WFCUUtilities.h"
- #import "WFCULocationViewController.h"
- #import "WFCULocationPoint.h"
- #import "WFCUSelectFileViewController.h"
- #import "KZVideoViewController.h"
- #import "UIView+Toast.h"
- #import <WFChatClient/WFCChatClient.h>
- #import "WFCUMentionUserTableViewController.h"
- #define CHAT_INPUT_BAR_PADDING 8
- #define CHAT_INPUT_BAR_ICON_SIZE (CHAT_INPUT_BAR_HEIGHT - CHAT_INPUT_BAR_PADDING - CHAT_INPUT_BAR_PADDING)
- @implementation WFCUMetionInfo
- - (instancetype)initWithType:(int)type target:(NSString *)target range:(NSRange)range {
- self = [super init];
- if (self) {
- self.mentionType = type;
- self.target = target;
- self.range = range;
- }
- return self;
- }
- @end
- //@implementation TextInfo
- //
- //@end
- @interface WFCUChatInputBar () <UITextViewDelegate, WFCUFaceBoardDelegate, UIImagePickerControllerDelegate, AVAudioRecorderDelegate, AVAudioPlayerDelegate, WFCUPluginBoardViewDelegate, UIImagePickerControllerDelegate, LocationViewControllerDelegate, UIActionSheetDelegate, KZVideoViewControllerDelegate, WFCUMentionUserDelegate>
- @property (nonatomic, assign)BOOL textInput;
- @property (nonatomic, assign)BOOL voiceInput;
- @property (nonatomic, assign)BOOL emojInput;
- @property (nonatomic, assign)BOOL pluginInput;
- @property (nonatomic, strong)UIButton *voiceSwitchBtn;
- @property (nonatomic, strong)UIButton *emojSwitchBtn;
- @property (nonatomic, strong)UIButton *pluginSwitchBtn;
- @property (nonatomic, strong)UITextView *textInputView;
- @property (nonatomic, strong)UIButton *voiceInputBtn;
- @property (nonatomic, strong)UIView *emojInputView;
- @property (nonatomic, strong)UIView *pluginInputView;
- @property(nonatomic, weak)id<WFCUChatInputBarDelegate> delegate;
- @property (nonatomic, strong)WFCUVoiceRecordView *recordView;
- @property(nonatomic) AVAudioRecorder *recorder;
- @property(nonatomic) NSTimer *recordingTimer;
- @property(nonatomic) NSTimer *updateMeterTimer;
- @property(nonatomic, assign) int seconds;
- @property(nonatomic) BOOL recordCanceled;
- @property(nonatomic, weak)UIView *parentView;
- @property (nonatomic, strong)NSMutableArray<WFCUMetionInfo *> *mentionInfos;
- @property (nonatomic, strong)WFCCConversation *conversation;
- @property (nonatomic, assign)double lastTypingTime;
- @end
- @implementation WFCUChatInputBar
- - (instancetype)initWithParentView:(UIView *)parentView conversation:(WFCCConversation *)conversation delegate:(id<WFCUChatInputBarDelegate>)delegate {
- self = [super initWithFrame:CGRectMake(0, parentView.bounds.size.height - CHAT_INPUT_BAR_HEIGHT, parentView.bounds.size.width, CHAT_INPUT_BAR_HEIGHT)];
- if (self) {
- [parentView addSubview:self];
- [self initSubViews];
- self.delegate = delegate;
- self.parentView = parentView;
- self.mentionInfos = [[NSMutableArray alloc] init];
- self.conversation = conversation;
- self.lastTypingTime = 0;
- }
- return self;
- }
- - (void)initSubViews {
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(keyboardWillShow:)
- name:UIKeyboardWillShowNotification
- object:nil];
-
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(keyboardWillHide:)
- name:UIKeyboardWillHideNotification
- object:nil];
-
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
-
- CGRect parentRect = self.bounds;
- self.voiceSwitchBtn = [[UIButton alloc] initWithFrame:CGRectMake(CHAT_INPUT_BAR_PADDING, CHAT_INPUT_BAR_PADDING, CHAT_INPUT_BAR_ICON_SIZE, CHAT_INPUT_BAR_ICON_SIZE)];
- [self.voiceSwitchBtn setImage:[UIImage imageNamed:@"chat_input_bar_voice"] forState:UIControlStateNormal];
- [self.voiceSwitchBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
- [self.voiceSwitchBtn addTarget:self action:@selector(onSwitchBtn:) forControlEvents:UIControlEventTouchDown];
- [self addSubview:self.voiceSwitchBtn];
-
- self.pluginSwitchBtn = [[UIButton alloc] initWithFrame:CGRectMake(parentRect.size.width - CHAT_INPUT_BAR_HEIGHT + CHAT_INPUT_BAR_PADDING, CHAT_INPUT_BAR_PADDING, CHAT_INPUT_BAR_ICON_SIZE, CHAT_INPUT_BAR_ICON_SIZE)];
- [self.pluginSwitchBtn setImage:[UIImage imageNamed:@"chat_input_bar_plugin"] forState:UIControlStateNormal];
- [self.pluginSwitchBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
- [self.pluginSwitchBtn addTarget:self action:@selector(onSwitchBtn:) forControlEvents:UIControlEventTouchDown];
- [self addSubview:self.pluginSwitchBtn];
-
- self.emojSwitchBtn = [[UIButton alloc] initWithFrame:CGRectMake(parentRect.size.width - CHAT_INPUT_BAR_HEIGHT - CHAT_INPUT_BAR_ICON_SIZE, CHAT_INPUT_BAR_PADDING, CHAT_INPUT_BAR_ICON_SIZE, CHAT_INPUT_BAR_ICON_SIZE)];
- [self.emojSwitchBtn setImage:[UIImage imageNamed:@"chat_input_bar_emoj"] forState:UIControlStateNormal];
- [self.emojSwitchBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
- [self.emojSwitchBtn addTarget:self action:@selector(onSwitchBtn:) forControlEvents:UIControlEventTouchDown];
- [self addSubview:self.emojSwitchBtn];
-
- self.textInputView = [[UITextView alloc] initWithFrame:CGRectMake(CHAT_INPUT_BAR_HEIGHT, CHAT_INPUT_BAR_PADDING, parentRect.size.width - CHAT_INPUT_BAR_HEIGHT - CHAT_INPUT_BAR_HEIGHT - CHAT_INPUT_BAR_HEIGHT + CHAT_INPUT_BAR_PADDING, CHAT_INPUT_BAR_ICON_SIZE)];
- self.textInputView.delegate = self;
- self.textInputView.layoutManager.allowsNonContiguousLayout = NO;
- [self.textInputView setExclusiveTouch:YES];
- [self.textInputView setTextColor:[UIColor blackColor]];
- [self.textInputView setFont:[UIFont systemFontOfSize:16]];
- [self.textInputView setReturnKeyType:UIReturnKeySend];
- self.textInputView.backgroundColor = [UIColor whiteColor];
- self.textInputView.enablesReturnKeyAutomatically = YES;
- self.textInputView.layer.cornerRadius = 4;
- self.textInputView.layer.masksToBounds = YES;
- self.textInputView.layer.borderWidth = 0.5f;
- self.textInputView.layer.borderColor = HEXCOLOR(0xdbdbdd).CGColor;
- [self addSubview:self.textInputView];
-
- self.voiceInputBtn = [[UIButton alloc] initWithFrame:CGRectMake(CHAT_INPUT_BAR_HEIGHT, CHAT_INPUT_BAR_PADDING, parentRect.size.width - CHAT_INPUT_BAR_HEIGHT - CHAT_INPUT_BAR_HEIGHT - CHAT_INPUT_BAR_HEIGHT + CHAT_INPUT_BAR_PADDING, CHAT_INPUT_BAR_ICON_SIZE)];
- [self.voiceInputBtn setTitle:@"按下 说话" forState:UIControlStateNormal];
- [self.voiceInputBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
- self.voiceInputBtn.layer.cornerRadius = 4;
- self.voiceInputBtn.layer.masksToBounds = YES;
- self.voiceInputBtn.layer.borderWidth = 0.5f;
- self.voiceInputBtn.layer.borderColor = HEXCOLOR(0xdbdbdd).CGColor;
- [self addSubview:self.voiceInputBtn];
-
- self.layer.borderWidth = 0.5f;
- self.layer.borderColor = HEXCOLOR(0xdbdbdd).CGColor;
-
- self.inputBarStatus = ChatInputBarDefaultStatus;
-
-
- [self.voiceInputBtn addTarget:self action:@selector(onTouchDown:) forControlEvents:UIControlEventTouchDown];
- [self.voiceInputBtn addTarget:self action:@selector(onTouchDragExit:) forControlEvents:UIControlEventTouchDragExit];
- [self.voiceInputBtn addTarget:self action:@selector(onTouchDragEnter:) forControlEvents:UIControlEventTouchDragEnter];
- [self.voiceInputBtn addTarget:self action:@selector(onTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
- [self.voiceInputBtn addTarget:self action:@selector(onTouchUpOutside:) forControlEvents:UIControlEventTouchUpOutside];
- [self.voiceInputBtn addTarget:self action:@selector(onTouchUpOutside:) forControlEvents:UIControlEventTouchCancel];
- self.voiceInputBtn.hidden = YES;
- self.textInputView.returnKeyType = UIReturnKeySend;
- self.textInputView.delegate = self;
- }
- - (void)onTouchDown:(id)sender {
- if ([self canRecord]) {
- _recordView = [[WFCUVoiceRecordView alloc] initWithFrame:CGRectMake(self.parentView.bounds.size.width/2 - 70, self.parentView.bounds.size.height/2 - 70, 140, 140)];
- _recordView.center = self.parentView.center;
- [self.parentView addSubview:_recordView];
- [self.parentView bringSubviewToFront:_recordView];
-
- [self recordStart];
- }
- }
- - (void)recordStart {
- if (self.recorder.recording) {
- return;
- }
-
- //[self stopPlayer];
-
- [[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
- if (granted) {
- AVAudioSession *session = [AVAudioSession sharedInstance];
- [session setCategory:AVAudioSessionCategoryRecord error:nil];
- BOOL r = [session setActive:YES error:nil];
- if (!r) {
- NSLog(@"activate audio session fail");
- return;
- }
- NSLog(@"start record...");
-
- NSArray *pathComponents = [NSArray arrayWithObjects:
- [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
- @"voice.wav",
- nil];
- NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];
-
- // Define the recorder setting
- NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];
-
- [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
- [recordSetting setValue:[NSNumber numberWithFloat:8000] forKey:AVSampleRateKey];
- [recordSetting setValue:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
-
- self.recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:NULL];
- self.recorder.delegate = self;
- self.recorder.meteringEnabled = YES;
- if (![self.recorder prepareToRecord]) {
- NSLog(@"prepare record fail");
- return;
- }
- if (![self.recorder record]) {
- NSLog(@"start record fail");
- return;
- }
-
-
- self.recordCanceled = NO;
- self.seconds = 0;
- self.recordingTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
-
- self.updateMeterTimer = [NSTimer scheduledTimerWithTimeInterval:0.05
- target:self
- selector:@selector(updateMeter:)
- userInfo:nil
- repeats:YES];
- } else {
- [[[UIAlertView alloc] initWithTitle:@"警告" message:@"无法录音,请到设置-隐私-麦克风,允许程序访问" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
- }
- }];
- }
- - (void)recordCancel {
- NSLog(@"touch cancel");
-
- if (self.recorder.recording) {
- NSLog(@"cancel record...");
- self.recordCanceled = YES;
- [self stopRecord];
- }
- }
- - (void)onTouchDragExit:(id)sender {
- [self.recordView recordButtonDragOutside];
- }
- - (void)onTouchDragEnter:(id)sender {
- [self.recordView recordButtonDragInside];
- }
- - (void)onTouchUpInside:(id)sender {
- [self.recordView removeFromSuperview];
- [self recordEnd];
- }
- - (void)onTouchUpOutside:(id)sender {
- [self.recordView removeFromSuperview];
- [self recordCancel];
- }
- - (BOOL)canRecord {
- __block BOOL bCanRecord = YES;
-
- if ([[AVAudioSession sharedInstance]
- respondsToSelector:@selector(requestRecordPermission:)]) {
- [[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
- bCanRecord = granted;
- dispatch_async(dispatch_get_main_queue(), ^{
- bCanRecord = granted;
- if (granted) {
- bCanRecord = YES;
- } else {
-
- }
- });
- }];
- }
-
- return bCanRecord;
- }
- - (void)timerFired:(NSTimer*)timer {
- self.seconds = self.seconds + 1;
- int minute = self.seconds/60;
- int s = self.seconds%60;
- NSString *str = [NSString stringWithFormat:@"%02d:%02d", minute, s];
- NSLog(@"timer:%@", str);
- int countdown = 60 - self.seconds;
- if (countdown <= 10) {
- [self.recordView setCountdown:countdown];
- }
- if (countdown <= 0) {
- [self.recordView removeFromSuperview];
- [self recordEnd];
- } else {
- [self notifyTyping:1];
- }
- }
- - (void)updateMeter:(NSTimer*)timer {
- double voiceMeter = 0;
- if ([self.recorder isRecording]) {
- [self.recorder updateMeters];
- //获取音量的平均值 [recorder averagePowerForChannel:0];
- //音量的最大值 [recorder peakPowerForChannel:0];
- double lowPassResults = pow(10, (0.05 * [self.recorder peakPowerForChannel:0]));
- voiceMeter = lowPassResults;
- }
- [self.recordView setVoiceImage:voiceMeter];
- }
- -(void)recordEnd {
- if (self.recorder.recording) {
- NSLog(@"stop record...");
- self.recordCanceled = NO;
- [self stopRecord];
- }
- }
- -(void)stopRecord {
- [self.recorder stop];
- [self.recordingTimer invalidate];
- self.recordingTimer = nil;
- [self.updateMeterTimer invalidate];
- self.updateMeterTimer = nil;
-
- AVAudioSession *audioSession = [AVAudioSession sharedInstance];
- BOOL r = [audioSession setActive:NO error:nil];
- if (!r) {
- NSLog(@"deactivate audio session fail");
- }
- }
- - (void)resetInputBarStatue {
- if (self.inputBarStatus != ChatInputBarRecordStatus) {
- self.inputBarStatus = ChatInputBarDefaultStatus;
- }
- }
- - (void)onSwitchBtn:(id)sender {
- if (sender == self.voiceSwitchBtn) {
- if (self.voiceInput && self.inputBarStatus != ChatInputBarDefaultStatus) {
- self.inputBarStatus = ChatInputBarKeyboardStatus;
- } else {
- self.inputBarStatus = ChatInputBarRecordStatus;
- }
- } else if(sender == self.emojSwitchBtn) {
- if (self.emojInput && self.inputBarStatus != ChatInputBarDefaultStatus) {
- self.inputBarStatus = ChatInputBarKeyboardStatus;
- } else {
- self.inputBarStatus = ChatInputBarEmojiStatus;
- }
- } else if (sender == self.pluginSwitchBtn) {
- if (self.pluginInput && self.inputBarStatus != ChatInputBarDefaultStatus) {
- self.inputBarStatus = ChatInputBarKeyboardStatus;
- } else {
- self.inputBarStatus = ChatInputBarPluginStatus;
- }
- }
- }
- - (void)setInputBarStatus:(ChatInputBarStatus)inputBarStatus {
- _inputBarStatus = inputBarStatus;
- switch (inputBarStatus) {
- case ChatInputBarKeyboardStatus:
- self.voiceInput = NO;
- self.emojInput = NO;
- self.pluginInput = NO;
- self.textInput = YES;
- break;
- case ChatInputBarPluginStatus:
- self.voiceInput = NO;
- self.emojInput = NO;
- self.pluginInput = YES;
- self.textInput = NO;
- break;
- case ChatInputBarEmojiStatus:
- self.voiceInput = NO;
- self.emojInput = YES;
- self.pluginInput = NO;
- self.textInput = NO;
- break;
- case ChatInputBarRecordStatus:
- self.voiceInput = YES;
- self.emojInput = NO;
- self.pluginInput = NO;
- self.textInput = NO;
- break;
- case ChatInputBarPublicStatus:
- self.voiceInput = NO;
- self.emojInput = NO;
- self.pluginInput = NO;
- self.textInput = NO;
- break;
- case ChatInputBarDefaultStatus:
- self.voiceInput = NO;
- self.emojInput = NO;
- self.pluginInput = NO;
- self.textInput = YES;
- [self.textInputView resignFirstResponder];
- break;
- default:
- break;
- }
- }
- - (void)setVoiceInput:(BOOL)voiceInput {
- _voiceInput = voiceInput;
- if (voiceInput) {
- [self.textInputView setHidden:YES];
- [self.voiceInputBtn setHidden:NO];
- if (self.textInputView.isFirstResponder) {
- [self.textInputView resignFirstResponder];
- }
- [self.voiceSwitchBtn setImage:[UIImage imageNamed:@"chat_input_bar_keyboard"] forState:UIControlStateNormal];
- } else {
- [self.textInputView setHidden:NO];
- [self.voiceInputBtn setHidden:YES];
- [self.voiceSwitchBtn setImage:[UIImage imageNamed:@"chat_input_bar_voice"] forState:UIControlStateNormal];
- }
- }
- - (void)setEmojInput:(BOOL)emojInput {
- _emojInput = emojInput;
- if (emojInput) {
- [self.textInputView setHidden:NO];
- [self.voiceInputBtn setHidden:YES];
- self.textInputView.inputView = self.emojInputView;
- if (!self.textInputView.isFirstResponder) {
- [self.textInputView becomeFirstResponder];
- }
- [self.textInputView reloadInputViews];
- [self.emojSwitchBtn setImage:[UIImage imageNamed:@"chat_input_bar_keyboard"] forState:UIControlStateNormal];
- } else {
- [self.emojSwitchBtn setImage:[UIImage imageNamed:@"chat_input_bar_emoj"] forState:UIControlStateNormal];
- }
- }
- - (void)setPluginInput:(BOOL)pluginInput {
- _pluginInput = pluginInput;
- if (pluginInput) {
- [self.textInputView setHidden:NO];
- [self.voiceInputBtn setHidden:YES];
- self.textInputView.inputView = self.pluginInputView;
- if (!self.textInputView.isFirstResponder) {
- [self.textInputView becomeFirstResponder];
- }
- [self.textInputView reloadInputViews];
- }
- }
- - (void)setTextInput:(BOOL)textInput {
- _textInput = textInput;
- if (textInput) {
- [self.textInputView setHidden:NO];
- [self.voiceInputBtn setHidden:YES];
- self.textInputView.inputView = nil;
- if (!self.textInputView.isFirstResponder && _inputBarStatus == ChatInputBarKeyboardStatus) {
- [self.textInputView becomeFirstResponder];
- }
- if (_inputBarStatus == ChatInputBarKeyboardStatus) {
- [self.textInputView reloadInputViews];
- }
- }
-
- }
- - (void)notifyTyping:(WFCCTypingType)type {
- double now = [[NSDate date] timeIntervalSince1970];
- if (self.lastTypingTime + TYPING_INTERVAL < now) {
- if ([self.delegate respondsToSelector:@selector(onTyping:)]) {
- [self.delegate onTyping:type];
- }
- self.lastTypingTime = now;
- }
- }
- - (void)setDraft:(NSString *)draft {
- NSError *__error = nil;
- NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:[draft dataUsingEncoding:NSUTF8StringEncoding]
- options:kNilOptions
- error:&__error];
-
- BOOL hasMentionInfo = NO;
- NSString *text = nil;
- NSMutableArray<WFCUMetionInfo *> *mentionInfos = [[NSMutableArray alloc] init];
- if (!__error) {
- if (dictionary[@"text"] != nil && [dictionary[@"mentions"] isKindOfClass:[NSArray class]]) {
- hasMentionInfo = YES;
- text = dictionary[@"text"];
- NSArray *mentions = dictionary[@"mentions"];
- [mentions enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
- NSDictionary *dic = (NSDictionary *)obj;
- WFCUMetionInfo *mentionInfo = [[WFCUMetionInfo alloc] init];
- mentionInfo.target = dic[@"target"];
- mentionInfo.mentionType = [dic[@"type"] intValue];
- mentionInfo.range = NSMakeRange([dic[@"loc"] integerValue], [dic[@"len"] integerValue]);
- [mentionInfos addObject:mentionInfo];
- }];
- }
- }
-
- if (hasMentionInfo) {
- draft = text;
- }
-
- [self textView:self.textInputView shouldChangeTextInRange:NSMakeRange(0, 0) replacementText:draft];
- self.textInputView.text = draft;
- self.mentionInfos = mentionInfos;
- }
- - (NSString *)draft {
- if (self.mentionInfos.count) {
- NSMutableDictionary *dataDict = [NSMutableDictionary dictionary];
- [dataDict setObject:self.textInputView.text forKey:@"text"];
- NSMutableArray *mentions = [[NSMutableArray alloc] init];
- [self.mentionInfos enumerateObjectsUsingBlock:^(WFCUMetionInfo * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
- NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
- [dic setObject:obj.target forKey:@"target"];
- [dic setObject:@(obj.mentionType) forKey:@"type"];
- [dic setObject:@(obj.range.location) forKey:@"loc"];
- [dic setObject:@(obj.range.length) forKey:@"len"];
- [mentions addObject:dic];
- }];
- [dataDict setObject:mentions forKey:@"mentions"];
-
- NSData *data = [NSJSONSerialization dataWithJSONObject:dataDict
- options:kNilOptions
- error:nil];
- return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
- } else {
- return self.textInputView.text;
- }
- }
- - (UIView *)emojInputView {
- if (!_emojInputView) {
- _emojInputView = [[WFCUFaceBoard alloc] init];
- ((WFCUFaceBoard*)_emojInputView).delegate = self;
- }
- return _emojInputView;
- }
- - (UIView *)pluginInputView {
- if (!_pluginInputView) {
- _pluginInputView = [[WFCUPluginBoardView alloc] initWithDelegate:self withVoip:(self.conversation.type == Single_Type || self.conversation.type == Group_Type)];
- }
- return _pluginInputView;
- }
- - (void)keyboardWillShow:(NSNotification *)notification {
- if (![self.textInputView isFirstResponder]) {
- return;
- }
- NSDictionary *userInfo = [notification userInfo];
- NSValue *value = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
- CGRect keyboardRect = [value CGRectValue];
- int height = keyboardRect.size.height - kTabbarSafeBottomMargin;
-
- CGFloat duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
- CGRect frame = CGRectMake(0, self.superview.bounds.size.height - self.bounds.size.height - height, self.superview.bounds.size.width, self.bounds.size.height);
- [self.delegate willChangeFrame:frame withDuration:duration keyboardShowing:YES];
- [UIView animateWithDuration:duration animations:^{
- self.frame = frame;
- }];
- }
- - (void)keyboardWillHide:(NSNotification *)notification {
- NSDictionary *userInfo = [notification userInfo];
-
- CGFloat duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
- CGRect frame = CGRectMake(0, self.superview.bounds.size.height - self.bounds.size.height, self.superview.bounds.size.width, self.bounds.size.height);
- [self.delegate willChangeFrame:frame withDuration:duration keyboardShowing:NO];
- [UIView animateWithDuration:duration animations:^{
- self.frame = frame;
- }];
- }
- -(void)keyboardDidHide:(NSNotification *)notification{
- if ((self.emojInput || self.pluginInput || self.textInput) && self.inputBarStatus != ChatInputBarDefaultStatus) {
- [self.textInputView becomeFirstResponder];
- }
- }
- - (BOOL)appendMention:(NSString *)userId name:(NSString *)userName {
- if (self.conversation.type == Group_Type) {
- NSString *mentionText = [NSString stringWithFormat:@"@%@ ", userName];
- [self didMentionType:1 user:userId range:NSMakeRange(self.textInputView.selectedRange.location, mentionText.length) text:mentionText];
- return YES;
- } else {
- return NO;
- }
- }
- - (void)paste:(id)sender {
- [self.textInputView paste:sender];
- }
- #pragma mark - AVAudioRecorderDelegate
- - (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag {
- NSLog(@"record finish:%d", flag);
- if (!flag) {
- return;
- }
- if (self.recordCanceled) {
- return;
- }
- if (self.seconds < 1) {
- NSLog(@"record time too short");
- [[[UIAlertView alloc] initWithTitle:@"警告" message:@"录音时间太短了" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
-
- return;
- }
- [self.delegate recordDidEnd:[recorder.url path] duration:self.seconds error:nil];
- [[NSFileManager defaultManager] removeItemAtURL:recorder.url error:nil];
- }
- #pragma mark - FaceBoardDelegate
- - (void)didTouchEmoj:(NSString *)emojString {
- NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc]initWithString:emojString];
- UIFont *font = [UIFont fontWithName:@"Heiti SC-Bold" size:16];
- [attStr addAttribute:(__bridge NSString*)kCTFontAttributeName value:(id)CFBridgingRelease(CTFontCreateWithName((CFStringRef)font.fontName,
- 16,
- NULL)) range:NSMakeRange(0, emojString.length)];
-
- NSInteger cursorPosition;
- if (self.textInputView.selectedTextRange) {
- cursorPosition = self.textInputView.selectedRange.location ;
- } else {
- cursorPosition = 0;
- }
- //获取光标位置
- if(cursorPosition> self.textInputView.textStorage.length)
- cursorPosition = self.textInputView.textStorage.length;
- [self.textInputView.textStorage
- insertAttributedString:attStr atIndex:cursorPosition];
-
- NSRange range;
- range.location = self.textInputView.selectedRange.location + emojString.length;
- range.length = 1;
-
- self.textInputView.selectedRange = range;
- }
- - (void)didTouchBackEmoj {
- [self.textInputView deleteBackward];
- }
- - (void)didTouchSendEmoj {
- [self sendAndCleanTextView];
- }
- - (void)sendAndCleanTextView {
- [self.delegate didTouchSend:self.textInputView.text withMentionInfos:self.mentionInfos];
- self.textInputView.text = nil;
- [self.mentionInfos removeAllObjects];
- [self changeTextViewHeight:32 needUpdateText:NO updateRange:NSMakeRange(0, 0)];
- }
- - (void)didSelectedSticker:(NSString *)stickerPath {
- if ([self.delegate respondsToSelector:@selector(didSelectSticker:)]) {
- [self.delegate didSelectSticker:stickerPath];
- }
- }
- #pragma mark - UITextViewDelegate
- - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
- if ([text isEqualToString:@"\n"]){ //判断输入的字是否是回车,即按下return
- [self sendAndCleanTextView];
- return NO;
- }
-
- BOOL needUpdateText = NO;
- if(self.conversation.type == Group_Type) {
- if ([text isEqualToString:@"@"]) {
- WFCUMentionUserTableViewController *mvc = [[WFCUMentionUserTableViewController alloc] init];
- mvc.groupId = self.conversation.target;
- mvc.delegate = self;
- mvc.range = range;
-
- UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:mvc];
- [[self.delegate requireNavi] presentViewController:nav animated:YES completion:nil];
- return NO;
- }
-
- if (text.length == 0) {
- WFCUMetionInfo *deletedMention;
- for (WFCUMetionInfo *mentionInfo in self.mentionInfos) {
- if ((mentionInfo.range.location >= range.location && mentionInfo.range.location < range.location + range.length) ||
- (range.location >= mentionInfo.range.location && range.location < mentionInfo.range.location + mentionInfo.range.length)) {
- deletedMention = mentionInfo;
- }
- }
-
- if (deletedMention) {
- range = deletedMention.range;
- [self.mentionInfos removeObject:deletedMention];
- needUpdateText = YES;
- }
- } else {
- for (WFCUMetionInfo *mentionInfo in self.mentionInfos) {
- if (range.location <= mentionInfo.range.location) {
- mentionInfo.range = NSMakeRange(mentionInfo.range.location - range.length + text.length, mentionInfo.range.length);
- }
- }
- }
-
- }
-
- NSString *oldStr = textView.text;
- NSString *newStr = [oldStr stringByReplacingCharactersInRange:range withString:text];
- CGFloat textAreaWidth = textView.frame.size.width - 2 * textView.textContainer.lineFragmentPadding;
- CGSize size = [WFCUUtilities getTextDrawingSize:newStr font:[UIFont systemFontOfSize:16] constrainedSize:CGSizeMake(textAreaWidth, 1000)];
-
- [self changeTextViewHeight:size.height needUpdateText:needUpdateText updateRange:range];
-
- return YES;
- }
- - (void)changeTextViewHeight:(CGFloat)height needUpdateText:(BOOL)needUpdateText updateRange:(NSRange)range {
- CGRect tvFrame = self.textInputView.frame;
- CGRect baseFrame = self.frame;
- CGRect voiceFrame = self.voiceSwitchBtn.frame;
- CGRect emojFrame = self.emojSwitchBtn.frame;
- CGRect extendFrame = self.pluginSwitchBtn.frame;
-
- CGFloat diff = 0;
- if (height <= 32.f) {
- tvFrame.size.height = 32.f;
- diff = (48.f - baseFrame.size.height);
- baseFrame.size.height = 48.f;
- } else if (height > 32.f && height < 50.f) {
- tvFrame.size.height = 50.f;
- diff = (66.f - baseFrame.size.height);
- baseFrame.size.height = 66.f;
- } else {
- tvFrame.size.height = 65.f;
- diff = (81.f - baseFrame.size.height);
- baseFrame.size.height = 81.f;
- }
-
- baseFrame.origin.y -= diff;
- voiceFrame.origin.y += diff;
- emojFrame.origin.y += diff;
- extendFrame.origin.y += diff;
-
-
- float duration = 0.5f;
- [self.delegate willChangeFrame:baseFrame withDuration:duration keyboardShowing:YES];
- __weak typeof(self)ws = self;
- [UIView animateWithDuration:duration animations:^{
- ws.textInputView.frame = tvFrame;
- self.frame = baseFrame;
- self.voiceSwitchBtn.frame = voiceFrame;
- self.emojSwitchBtn.frame = emojFrame;
- self.pluginSwitchBtn.frame = extendFrame;
- if(needUpdateText) {
- [ws.textInputView.textStorage replaceCharactersInRange:range withString:@" "];
- }
- }];
- }
- - (void)textViewDidChangeSelection:(UITextView *)textView {
- if (self.textInputView == textView && self.conversation.type == Group_Type) {
- NSRange range = textView.selectedRange;
- for (WFCUMetionInfo *mention in self.mentionInfos) {
- if (range.location > mention.range.location && range.location < mention.range.location + mention.range.length) {
- if (range.length == 0) {
- if(range.location == mention.range.location + mention.range.length - 1) {
- range.location = mention.range.location;
- } else {
- range = NSMakeRange(mention.range.location + mention.range.length, 0);
- }
- } else {
- long length = range.length - (mention.range.location + mention.range.length) + range.location;
- if (length < 0) {
- length = 0;
- }
- range = NSMakeRange(mention.range.location + mention.range.length, length);
- }
-
- textView.selectedRange = range;
- break;
- }
- }
- }
- }
- - (void)textViewDidChange:(UITextView *)textView {
- if (textView.text.length > 0) {
- [self notifyTyping:0];
- }
- }
- #pragma mark - PluginBoardViewDelegate
- - (void)onItemClicked:(NSUInteger)itemTag {
- UINavigationController *navi = [self.delegate requireNavi];
-
- self.inputBarStatus = ChatInputBarDefaultStatus;
- if (itemTag == 1) {
- UIImagePickerController *picker = [[UIImagePickerController alloc] init];
- picker.delegate = self;
- #if TARGET_IPHONE_SIMULATOR
- picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
- #else
- if (itemTag == 1) {
- picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
- } else if(itemTag == 2){
- picker.sourceType = UIImagePickerControllerSourceTypeCamera;
- }
- #endif
- [navi presentViewController:picker animated:YES completion:nil];
- [self checkAndAlertCameraAccessRight];
- } else if(itemTag == 2) {
- #if TARGET_IPHONE_SIMULATOR
- [self makeToast:@"模拟器不支持相机" duration:1 position:CSToastPositionCenter];
- UIImagePickerController *picker = [[UIImagePickerController alloc] init];
- picker.delegate = self;
- picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
- [navi presentViewController:picker animated:YES completion:nil];
- [self checkAndAlertCameraAccessRight];
- #else
- KZVideoViewController *videoVC = [[KZVideoViewController alloc] init];
- videoVC.delegate = self;
- [videoVC startAnimationWithType:KZVideoViewShowTypeSingle selectExist:NO];
- double now = [[NSDate date] timeIntervalSince1970];
- [self notifyTyping:2];
- #endif
- } else if(itemTag == 3){
- WFCULocationViewController *vc = [[WFCULocationViewController alloc] initWithDelegate:self];
-
- UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
- [navi presentViewController:nav animated:YES completion:nil];
- [self notifyTyping:3];
- return;
- } else if(itemTag == 4) {
- UIActionSheet *actionSheet =
- [[UIActionSheet alloc] initWithTitle:nil
- delegate:self
- cancelButtonTitle:@"取消"
- destructiveButtonTitle:@"视频"
- otherButtonTitles:@"音频", nil];
- [actionSheet showInView:self.parentView];
- } else if(itemTag == 5) {
- WFCUSelectFileViewController *sfvc = [[WFCUSelectFileViewController alloc] init];
- UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:sfvc];
- __weak typeof(self) ws = self;
- sfvc.selectResult = ^(NSArray *selectFiles) {
- [ws.delegate didSelectFiles:selectFiles];
- };
- [[self.delegate requireNavi] presentViewController:navi animated:YES completion:nil];
- [self notifyTyping:4];
- }
- }
- - (void)checkAndAlertCameraAccessRight {
- AVAuthorizationStatus authStatus =
- [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
- if (authStatus == AVAuthorizationStatusDenied ||
- authStatus == AVAuthorizationStatusRestricted) {
- UIAlertView *alertView = [[UIAlertView alloc]
- initWithTitle:@"拍照权限"
- message:@"需要拍照权限,请在设置里打开"
- delegate:nil
- cancelButtonTitle:@"确认"
- otherButtonTitles:nil, nil];
- [alertView show];
- }
- }
- #pragma mark - UIImagePickerControllerDelegate<NSObject>
- - (void)imagePickerController:(UIImagePickerController *)picker
- didFinishPickingImage:(UIImage *)image
- editingInfo:(NSDictionary *)editingInfo {
- [picker dismissViewControllerAnimated:YES completion:nil];
- [self.delegate imageDidCapture:image];
- }
- - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
- [picker dismissViewControllerAnimated:YES completion:nil];
- }
- #pragma mark - LocationViewControllerDelegate <NSObject>
- - (void)onSendLocation:(WFCULocationPoint *)locationPoint {
- [self.delegate locationDidSelect:locationPoint.coordinate locationName:locationPoint.title mapScreenShot:locationPoint.thumbnail];
- }
- #pragma mark - UIActionSheetDelegate
- - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
- if (buttonIndex == 0) {
- [self.delegate didTouchVideoBtn:NO];
- } else if(buttonIndex == 1) {
- [self.delegate didTouchVideoBtn:YES];
- }
- }
- #pragma mark - KZVideoViewControllerDelegate
- - (void)videoViewController:(KZVideoViewController *)videoController didCaptureImage:(UIImage *)image {
- [self.delegate imageDidCapture:image];
- }
- - (void)videoViewController:(KZVideoViewController *)videoController didRecordVideo:(KZVideoModel *)videoModel {
- [self.delegate videoDidCapture:videoModel.videoAbsolutePath thumbnail:[UIImage imageWithContentsOfFile:videoModel.thumAbsolutePath] duration:10];
- }
- - (void)videoViewControllerDidCancel:(KZVideoViewController *)videoController {
- }
- #pragma mark - WFCUMentionUserDelegate
- - (void)didMentionType:(int)type user:(NSString *)userId range:(NSRange)range text:(NSString *)text {
- [self textView:self.textInputView shouldChangeTextInRange:NSMakeRange(range.location, 0) replacementText:text];
-
- [self.mentionInfos addObject:[[WFCUMetionInfo alloc] initWithType:type target:userId range:NSMakeRange(range.location, range.length)]];
-
- NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc]initWithString:text];
- UIFont *font = [UIFont fontWithName:@"Heiti SC-Bold" size:16];
- [attStr addAttribute:(__bridge NSString*)kCTFontAttributeName value:(id)CFBridgingRelease(CTFontCreateWithName((CFStringRef)font.fontName,
- 16,
- NULL)) range:NSMakeRange(0, text.length)];
-
- [self.textInputView.textStorage
- insertAttributedString:attStr atIndex:range.location];
-
- self.textInputView.selectedRange = range;
- }
- - (void)didCancelMentionAtRange:(NSRange)range {
- [self.textInputView.textStorage replaceCharactersInRange:NSMakeRange(range.location, 0) withString:@"@"];
-
- self.textInputView.selectedRange = range;
- }
- @end
|