chore: import upstream snapshot with attribution
This commit is contained in:
+30
@@ -0,0 +1,30 @@
|
||||
//
|
||||
// TLChatBaseViewController+ChatBar.h
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/3/17.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TLChatBaseViewController.h"
|
||||
#import "TLMoreKeyboard.h"
|
||||
#import "TLEmojiKeyboard.h"
|
||||
|
||||
@interface TLChatBaseViewController (ChatBar) <TLChatBarDelegate, TLKeyboardDelegate, TLEmojiKeyboardDelegate>
|
||||
|
||||
/// 表情键盘
|
||||
@property (nonatomic, strong, readonly) TLEmojiKeyboard *emojiKeyboard;
|
||||
|
||||
/// 更多键盘
|
||||
@property (nonatomic, strong, readonly) TLMoreKeyboard *moreKeyboard;
|
||||
|
||||
- (void)loadKeyboard;
|
||||
- (void)dismissKeyboard;
|
||||
|
||||
- (void)keyboardWillShow:(NSNotification *)notification;
|
||||
- (void)keyboardDidShow:(NSNotification *)notification;
|
||||
- (void)keyboardWillHide:(NSNotification *)notification;
|
||||
- (void)keyboardFrameWillChange:(NSNotification *)notification;
|
||||
|
||||
|
||||
@end
|
||||
+319
@@ -0,0 +1,319 @@
|
||||
//
|
||||
// TLChatBaseViewController+ChatBar.m
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/3/17.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TLChatBaseViewController+ChatBar.h"
|
||||
#import "TLChatBaseViewController+Proxy.h"
|
||||
#import "TLChatBaseViewController+MessageDisplayView.h"
|
||||
#import "TLAudioRecorder.h"
|
||||
#import "TLAudioPlayer.h"
|
||||
#import "TLUserHelper.h"
|
||||
#import "NSFileManager+TLChat.h"
|
||||
|
||||
@implementation TLChatBaseViewController (ChatBar)
|
||||
|
||||
#pragma mark - # Public Methods
|
||||
- (void)loadKeyboard
|
||||
{
|
||||
[self.emojiKeyboard setKeyboardDelegate:self];
|
||||
[self.emojiKeyboard setDelegate:self];
|
||||
[self.moreKeyboard setKeyboardDelegate:self];
|
||||
[self.moreKeyboard setDelegate:self];
|
||||
}
|
||||
|
||||
- (void)dismissKeyboard
|
||||
{
|
||||
if (curStatus == TLChatBarStatusMore) {
|
||||
[self.moreKeyboard dismissWithAnimation:YES];
|
||||
curStatus = TLChatBarStatusInit;
|
||||
}
|
||||
else if (curStatus == TLChatBarStatusEmoji) {
|
||||
[self.emojiKeyboard dismissWithAnimation:YES];
|
||||
curStatus = TLChatBarStatusInit;
|
||||
}
|
||||
[self.chatBar resignFirstResponder];
|
||||
}
|
||||
|
||||
//MARK: 系统键盘回调
|
||||
- (void)keyboardWillShow:(NSNotification *)notification
|
||||
{
|
||||
if (curStatus != TLChatBarStatusKeyboard) {
|
||||
return;
|
||||
}
|
||||
[self.messageDisplayView scrollToBottomWithAnimation:YES];
|
||||
}
|
||||
|
||||
- (void)keyboardDidShow:(NSNotification *)notification
|
||||
{
|
||||
if (curStatus != TLChatBarStatusKeyboard) {
|
||||
return;
|
||||
}
|
||||
if (lastStatus == TLChatBarStatusMore) {
|
||||
[self.moreKeyboard dismissWithAnimation:NO];
|
||||
}
|
||||
else if (lastStatus == TLChatBarStatusEmoji) {
|
||||
[self.emojiKeyboard dismissWithAnimation:NO];
|
||||
}
|
||||
[self.messageDisplayView scrollToBottomWithAnimation:YES];
|
||||
}
|
||||
|
||||
- (void)keyboardFrameWillChange:(NSNotification *)notification
|
||||
{
|
||||
if (curStatus != TLChatBarStatusKeyboard && lastStatus != TLChatBarStatusKeyboard) {
|
||||
return;
|
||||
}
|
||||
CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
|
||||
if (lastStatus == TLChatBarStatusMore || lastStatus == TLChatBarStatusEmoji) {
|
||||
if (keyboardFrame.size.height <= HEIGHT_CHAT_KEYBOARD) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
else if (curStatus == TLChatBarStatusEmoji || curStatus == TLChatBarStatusMore) {
|
||||
return;
|
||||
}
|
||||
[self.chatBar mas_updateConstraints:^(MASConstraintMaker *make) {
|
||||
make.bottom.mas_equalTo(self.view).mas_offset(-keyboardFrame.size.height);
|
||||
}];
|
||||
[self.view layoutIfNeeded];
|
||||
[self.messageDisplayView scrollToBottomWithAnimation:YES];
|
||||
}
|
||||
|
||||
- (void)keyboardWillHide:(NSNotification *)notification
|
||||
{
|
||||
if (curStatus != TLChatBarStatusKeyboard && lastStatus != TLChatBarStatusKeyboard) {
|
||||
return;
|
||||
}
|
||||
if (curStatus == TLChatBarStatusEmoji || curStatus == TLChatBarStatusMore) {
|
||||
return;
|
||||
}
|
||||
[self.chatBar mas_updateConstraints:^(MASConstraintMaker *make) {
|
||||
make.bottom.mas_equalTo(self.view);
|
||||
}];
|
||||
[self.view layoutIfNeeded];
|
||||
}
|
||||
|
||||
#pragma mark - Delegate
|
||||
//MARK: TLChatBarDelegate
|
||||
// 发送文本消息
|
||||
- (void)chatBar:(TLChatBar *)chatBar sendText:(NSString *)text
|
||||
{
|
||||
TLTextMessage *message = [[TLTextMessage alloc] init];
|
||||
message.text = text;
|
||||
[self sendMessage:message];
|
||||
}
|
||||
|
||||
//MARK: - 录音相关
|
||||
- (void)chatBarStartRecording:(TLChatBar *)chatBar
|
||||
{
|
||||
// 先停止播放
|
||||
if ([TLAudioPlayer sharedAudioPlayer].isPlaying) {
|
||||
[[TLAudioPlayer sharedAudioPlayer] stopPlayingAudio];
|
||||
}
|
||||
|
||||
[self.recorderIndicatorView setStatus:TLRecorderStatusRecording];
|
||||
[self.messageDisplayView addSubview:self.recorderIndicatorView];
|
||||
[self.recorderIndicatorView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.center.mas_equalTo(self.view);
|
||||
make.size.mas_equalTo(CGSizeMake(150, 150));
|
||||
}];
|
||||
|
||||
__block NSInteger time_count = 0;
|
||||
TLVoiceMessage *message = [[TLVoiceMessage alloc] init];
|
||||
message.ownerTyper = TLMessageOwnerTypeSelf;
|
||||
message.userID = [TLUserHelper sharedHelper].userID;
|
||||
message.fromUser = (id<TLChatUserProtocol>)[TLUserHelper sharedHelper].user;
|
||||
message.msgStatus = TLVoiceMessageStatusRecording;
|
||||
message.date = [NSDate date];
|
||||
[[TLAudioRecorder sharedRecorder] startRecordingWithVolumeChangedBlock:^(CGFloat volume) {
|
||||
time_count ++;
|
||||
if (time_count == 2) {
|
||||
[self addToShowMessage:message];
|
||||
}
|
||||
[self.recorderIndicatorView setVolume:volume];
|
||||
} completeBlock:^(NSString *filePath, CGFloat time) {
|
||||
if (time < 1.0) {
|
||||
[self.recorderIndicatorView setStatus:TLRecorderStatusTooShort];
|
||||
return;
|
||||
}
|
||||
[self.recorderIndicatorView removeFromSuperview];
|
||||
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
|
||||
NSString *fileName = [NSString stringWithFormat:@"%.0lf.caf", [NSDate date].timeIntervalSince1970 * 1000];
|
||||
NSString *path = [NSFileManager pathUserChatVoice:fileName];
|
||||
NSError *error;
|
||||
[[NSFileManager defaultManager] moveItemAtPath:filePath toPath:path error:&error];
|
||||
if (error) {
|
||||
DDLogError(@"录音文件出错: %@", error);
|
||||
return;
|
||||
}
|
||||
|
||||
message.recFileName = fileName;
|
||||
message.time = time;
|
||||
message.msgStatus = TLVoiceMessageStatusNormal;
|
||||
[message resetMessageFrame];
|
||||
[self sendMessage:message];
|
||||
}
|
||||
} cancelBlock:^{
|
||||
[self.messageDisplayView deleteMessage:message];
|
||||
[self.recorderIndicatorView removeFromSuperview];
|
||||
}];
|
||||
}
|
||||
|
||||
- (void)chatBarWillCancelRecording:(TLChatBar *)chatBar cancel:(BOOL)cancel
|
||||
{
|
||||
[self.recorderIndicatorView setStatus:cancel ? TLRecorderStatusWillCancel : TLRecorderStatusRecording];
|
||||
}
|
||||
|
||||
- (void)chatBarFinishedRecoding:(TLChatBar *)chatBar
|
||||
{
|
||||
[[TLAudioRecorder sharedRecorder] stopRecording];
|
||||
}
|
||||
|
||||
- (void)chatBarDidCancelRecording:(TLChatBar *)chatBar
|
||||
{
|
||||
[[TLAudioRecorder sharedRecorder] cancelRecording];
|
||||
}
|
||||
|
||||
//MARK: - chatBar状态切换
|
||||
- (void)chatBar:(TLChatBar *)chatBar changeStatusFrom:(TLChatBarStatus)fromStatus to:(TLChatBarStatus)toStatus
|
||||
{
|
||||
if (curStatus == toStatus) {
|
||||
return;
|
||||
}
|
||||
lastStatus = fromStatus;
|
||||
curStatus = toStatus;
|
||||
if (toStatus == TLChatBarStatusInit) {
|
||||
if (fromStatus == TLChatBarStatusMore) {
|
||||
[self.moreKeyboard dismissWithAnimation:YES];
|
||||
}
|
||||
else if (fromStatus == TLChatBarStatusEmoji) {
|
||||
[self.emojiKeyboard dismissWithAnimation:YES];
|
||||
}
|
||||
}
|
||||
else if (toStatus == TLChatBarStatusVoice) {
|
||||
if (fromStatus == TLChatBarStatusMore) {
|
||||
[self.moreKeyboard dismissWithAnimation:YES];
|
||||
}
|
||||
else if (fromStatus == TLChatBarStatusEmoji) {
|
||||
[self.emojiKeyboard dismissWithAnimation:YES];
|
||||
}
|
||||
}
|
||||
else if (toStatus == TLChatBarStatusEmoji) {
|
||||
[self.emojiKeyboard showInView:self.view withAnimation:YES];
|
||||
}
|
||||
else if (toStatus == TLChatBarStatusMore) {
|
||||
[self.moreKeyboard showInView:self.view withAnimation:YES];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)chatBar:(TLChatBar *)chatBar didChangeTextViewHeight:(CGFloat)height
|
||||
{
|
||||
[self.messageDisplayView scrollToBottomWithAnimation:NO];
|
||||
}
|
||||
|
||||
//MARK: TLKeyboardDelegate
|
||||
- (void)chatKeyboardWillShow:(id)keyboard animated:(BOOL)animated
|
||||
{
|
||||
[self.messageDisplayView scrollToBottomWithAnimation:YES];
|
||||
}
|
||||
|
||||
- (void)chatKeyboardDidShow:(id)keyboard animated:(BOOL)animated
|
||||
{
|
||||
if (curStatus == TLChatBarStatusMore && lastStatus == TLChatBarStatusEmoji) {
|
||||
[self.emojiKeyboard dismissWithAnimation:NO];
|
||||
}
|
||||
else if (curStatus == TLChatBarStatusEmoji && lastStatus == TLChatBarStatusMore) {
|
||||
[self.moreKeyboard dismissWithAnimation:NO];
|
||||
}
|
||||
[self.messageDisplayView scrollToBottomWithAnimation:YES];
|
||||
}
|
||||
|
||||
- (void)chatKeyboard:(id)keyboard didChangeHeight:(CGFloat)height
|
||||
{
|
||||
[self.chatBar mas_updateConstraints:^(MASConstraintMaker *make) {
|
||||
make.bottom.mas_equalTo(self.view).mas_offset(-height);
|
||||
}];
|
||||
[self.view layoutIfNeeded];
|
||||
[self.messageDisplayView scrollToBottomWithAnimation:YES];
|
||||
}
|
||||
|
||||
//MARK: TLEmojiKeyboardDelegate
|
||||
- (void)emojiKeyboard:(TLEmojiKeyboard *)emojiKB didSelectedEmojiItem:(TLExpressionModel *)emoji
|
||||
{
|
||||
if (emoji.type == TLEmojiTypeEmoji || emoji.type == TLEmojiTypeFace) {
|
||||
[self.chatBar addEmojiString:emoji.name];
|
||||
}
|
||||
else {
|
||||
TLExpressionMessage *message = [[TLExpressionMessage alloc] init];
|
||||
message.emoji = emoji;
|
||||
[self sendMessage:message];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)emojiKeyboardSendButtonDown
|
||||
{
|
||||
[self.chatBar sendCurrentText];
|
||||
}
|
||||
|
||||
- (void)emojiKeyboardDeleteButtonDown
|
||||
{
|
||||
[self.chatBar deleteLastCharacter];
|
||||
}
|
||||
|
||||
- (void)emojiKeyboard:(TLEmojiKeyboard *)emojiKB didTouchEmojiItem:(TLExpressionModel *)emoji atRect:(CGRect)rect
|
||||
{
|
||||
if (emoji.type == TLEmojiTypeEmoji || emoji.type == TLEmojiTypeFace) {
|
||||
if (self.emojiDisplayView.superview == nil) {
|
||||
[self.emojiKeyboard addSubview:self.emojiDisplayView];
|
||||
}
|
||||
[self.emojiDisplayView displayEmoji:emoji atRect:rect];
|
||||
}
|
||||
else {
|
||||
if (self.imageExpressionDisplayView.superview == nil) {
|
||||
[self.emojiKeyboard addSubview:self.imageExpressionDisplayView];
|
||||
}
|
||||
[self.imageExpressionDisplayView displayEmoji:emoji atRect:rect];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)emojiKeyboardCancelTouchEmojiItem:(TLEmojiKeyboard *)emojiKB
|
||||
{
|
||||
if (self.emojiDisplayView.superview != nil) {
|
||||
[self.emojiDisplayView removeFromSuperview];
|
||||
}
|
||||
else if (self.imageExpressionDisplayView.superview != nil) {
|
||||
[self.imageExpressionDisplayView removeFromSuperview];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)emojiKeyboard:(TLEmojiKeyboard *)emojiKB selectedEmojiGroupType:(TLEmojiType)type
|
||||
{
|
||||
if (type == TLEmojiTypeEmoji || type == TLEmojiTypeFace) {
|
||||
[self.chatBar setActivity:YES];
|
||||
}
|
||||
else {
|
||||
[self.chatBar setActivity:NO];
|
||||
}
|
||||
}
|
||||
|
||||
- (BOOL)chatInputViewHasText
|
||||
{
|
||||
return self.chatBar.curText.length == 0 ? NO : YES;
|
||||
}
|
||||
|
||||
#pragma mark - # Getter
|
||||
- (TLEmojiKeyboard *)emojiKeyboard
|
||||
{
|
||||
return [TLEmojiKeyboard keyboard];
|
||||
}
|
||||
|
||||
- (TLMoreKeyboard *)moreKeyboard
|
||||
{
|
||||
return [TLMoreKeyboard keyboard];
|
||||
}
|
||||
|
||||
|
||||
@end
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
//
|
||||
// TLChatBaseViewController+MessageDisplayView.h
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/3/17.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TLChatBaseViewController.h"
|
||||
#import "TLAudioPlayer.h"
|
||||
|
||||
#define MAX_SHOWTIME_MSG_COUNT 10
|
||||
#define MAX_SHOWTIME_MSG_SECOND 30
|
||||
|
||||
@interface TLChatBaseViewController (MessageDisplayView) <TLChatMessageDisplayViewDelegate>
|
||||
|
||||
/**
|
||||
* 添加展示消息(添加到chatVC)
|
||||
*/
|
||||
- (void)addToShowMessage:(TLMessage *)message;
|
||||
|
||||
- (void)resetChatTVC;
|
||||
|
||||
@end
|
||||
+146
@@ -0,0 +1,146 @@
|
||||
//
|
||||
// TLChatBaseViewController+MessageDisplayView.m
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/3/17.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TLChatBaseViewController+MessageDisplayView.h"
|
||||
#import "TLChatBaseViewController+ChatBar.h"
|
||||
#import "TLTextDisplayView.h"
|
||||
|
||||
@implementation TLChatBaseViewController (MessageDisplayView)
|
||||
|
||||
#pragma mark - # Public Methods
|
||||
- (void)addToShowMessage:(TLMessage *)message
|
||||
{
|
||||
message.showTime = [self p_needShowTime:message.date];
|
||||
[self.messageDisplayView addMessage:message];
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
[self.messageDisplayView scrollToBottomWithAnimation:YES];
|
||||
});
|
||||
}
|
||||
|
||||
- (void)addVoiceRecordingMessage:(TLMessage *)message
|
||||
{
|
||||
message.date = [NSDate date];
|
||||
}
|
||||
|
||||
- (void)resetChatTVC
|
||||
{
|
||||
[self.messageDisplayView resetMessageView];
|
||||
lastDateInterval = 0;
|
||||
msgAccumulate = 0;
|
||||
}
|
||||
|
||||
#pragma mark - # Delegate
|
||||
//MARK: TLChatMessageDisplayViewDelegate
|
||||
// chatView 点击事件
|
||||
- (void)chatMessageDisplayViewDidTouched:(TLChatMessageDisplayView *)chatTVC
|
||||
{
|
||||
[self dismissKeyboard];
|
||||
}
|
||||
|
||||
// chatView 获取历史记录
|
||||
- (void)chatMessageDisplayView:(TLChatMessageDisplayView *)chatTVC getRecordsFromDate:(NSDate *)date count:(NSUInteger)count completed:(void (^)(NSDate *, NSArray *, BOOL))completed
|
||||
{
|
||||
TLWeakSelf(self);
|
||||
[[TLMessageManager sharedInstance] messageRecordForPartner:[self.partner chat_userID] fromDate:date count:count complete:^(NSArray *array, BOOL hasMore) {
|
||||
if (array.count > 0) {
|
||||
int count = 0;
|
||||
NSTimeInterval tm = 0;
|
||||
for (TLMessage *message in array) {
|
||||
if (++count > MAX_SHOWTIME_MSG_COUNT || tm == 0 || message.date.timeIntervalSince1970 - tm > MAX_SHOWTIME_MSG_SECOND) {
|
||||
tm = message.date.timeIntervalSince1970;
|
||||
count = 0;
|
||||
message.showTime = YES;
|
||||
}
|
||||
if (message.ownerTyper == TLMessageOwnerTypeSelf) {
|
||||
message.fromUser = weakself.user;
|
||||
}
|
||||
else {
|
||||
if ([weakself.partner chat_userType] == TLChatUserTypeUser) {
|
||||
message.fromUser = weakself.partner;
|
||||
}
|
||||
else if ([weakself.partner chat_userType] == TLChatUserTypeGroup){
|
||||
if ([weakself.partner respondsToSelector:@selector(groupMemberByID:)]) {
|
||||
message.fromUser = [weakself.partner groupMemberByID:message.friendID];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
completed(date, array, hasMore);
|
||||
}];
|
||||
}
|
||||
|
||||
- (BOOL)chatMessageDisplayView:(TLChatMessageDisplayView *)chatTVC deleteMessage:(TLMessage *)message
|
||||
{
|
||||
return [[TLMessageManager sharedInstance] deleteMessageByMsgID:message.messageID];
|
||||
}
|
||||
|
||||
- (void)chatMessageDisplayView:(TLChatMessageDisplayView *)chatTVC didClickUserAvatar:(TLUser *)user
|
||||
{
|
||||
if ([self respondsToSelector:@selector(didClickedUserAvatar:)]) {
|
||||
[self didClickedUserAvatar:user];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)chatMessageDisplayView:(TLChatMessageDisplayView *)chatTVC didDoubleClickMessage:(TLMessage *)message
|
||||
{
|
||||
if (message.messageType == TLMessageTypeText) {
|
||||
TLTextDisplayView *displayView = [[TLTextDisplayView alloc] init];
|
||||
[displayView showInView:self.navigationController.view withAttrText:[(TLTextMessage *)message attrText] animation:YES];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)chatMessageDisplayView:(TLChatMessageDisplayView *)chatTVC didClickMessage:(TLMessage *)message
|
||||
{
|
||||
if (message.messageType == TLMessageTypeImage && [self respondsToSelector:@selector(didClickedImageMessages:atIndex:)]) {
|
||||
// 展示聊天图片
|
||||
[[TLMessageManager sharedInstance] chatImagesAndVideosForPartnerID:[self.partner chat_userID] completed:^(NSArray *imagesData) {
|
||||
NSInteger index = -1;
|
||||
for (int i = 0; i < imagesData.count; i ++) {
|
||||
if ([message.messageID isEqualToString:[imagesData[i] messageID]]) {
|
||||
index = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (index >= 0) {
|
||||
[self didClickedImageMessages:imagesData atIndex:index];
|
||||
}
|
||||
}];
|
||||
}
|
||||
else if (message.messageType == TLMessageTypeVoice) {
|
||||
if ([(TLVoiceMessage *)message msgStatus] == TLVoiceMessageStatusNormal) {
|
||||
// 播放语音消息
|
||||
[(TLVoiceMessage *)message setMsgStatus:TLVoiceMessageStatusPlaying];
|
||||
|
||||
[[TLAudioPlayer sharedAudioPlayer] playAudioAtPath:[(TLVoiceMessage *)message path] complete:^(BOOL finished) {
|
||||
[(TLVoiceMessage *)message setMsgStatus:TLVoiceMessageStatusNormal];
|
||||
[self.messageDisplayView updateMessage:message];
|
||||
}];
|
||||
}
|
||||
else {
|
||||
// 停止播放语音消息
|
||||
[[TLAudioPlayer sharedAudioPlayer] stopPlayingAudio];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - # Private Methods
|
||||
static NSTimeInterval lastDateInterval = 0;
|
||||
static NSInteger msgAccumulate = 0;
|
||||
- (BOOL)p_needShowTime:(NSDate *)date
|
||||
{
|
||||
if (++msgAccumulate > MAX_SHOWTIME_MSG_COUNT || lastDateInterval == 0 || date.timeIntervalSince1970 - lastDateInterval > MAX_SHOWTIME_MSG_SECOND) {
|
||||
lastDateInterval = date.timeIntervalSince1970;
|
||||
msgAccumulate = 0;
|
||||
return YES;
|
||||
}
|
||||
return NO;
|
||||
}
|
||||
|
||||
|
||||
@end
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
//
|
||||
// TLChatBaseViewController+Proxy.h
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/3/17.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TLChatBaseViewController.h"
|
||||
|
||||
@interface TLChatBaseViewController (Proxy) <TLMessageManagerChatVCDelegate>
|
||||
|
||||
/**
|
||||
* 发送消息
|
||||
*/
|
||||
- (void)sendMessage:(TLMessage *)message;
|
||||
|
||||
@end
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
//
|
||||
// TLChatBaseViewController+Proxy.m
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/3/17.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TLChatBaseViewController+Proxy.h"
|
||||
#import "TLChatBaseViewController+MessageDisplayView.h"
|
||||
#import "TLUserHelper.h"
|
||||
|
||||
@implementation TLChatBaseViewController (Proxy)
|
||||
|
||||
- (void)sendMessage:(TLMessage *)message
|
||||
{
|
||||
message.ownerTyper = TLMessageOwnerTypeSelf;
|
||||
message.userID = [TLUserHelper sharedHelper].userID;
|
||||
message.fromUser = (id<TLChatUserProtocol>)[TLUserHelper sharedHelper].user;
|
||||
message.date = [NSDate date];
|
||||
|
||||
if ([self.partner chat_userType] == TLChatUserTypeUser) {
|
||||
message.partnerType = TLPartnerTypeUser;
|
||||
message.friendID = [self.partner chat_userID];
|
||||
}
|
||||
else if ([self.partner chat_userType] == TLChatUserTypeGroup) {
|
||||
message.partnerType = TLPartnerTypeGroup;
|
||||
message.groupID = [self.partner chat_userID];
|
||||
}
|
||||
|
||||
if (message.messageType != TLMessageTypeVoice) {
|
||||
[self addToShowMessage:message]; // 添加到列表
|
||||
}
|
||||
else {
|
||||
[self.messageDisplayView updateMessage:message];
|
||||
}
|
||||
|
||||
[[TLMessageManager sharedInstance] sendMessage:message progress:^(TLMessage * message, CGFloat pregress) {
|
||||
|
||||
} success:^(TLMessage * message) {
|
||||
NSLog(@"send success");
|
||||
} failure:^(TLMessage * message) {
|
||||
NSLog(@"send failure");
|
||||
}];
|
||||
}
|
||||
|
||||
- (void)didReceivedMessage:(TLMessage *)message;
|
||||
{
|
||||
if ([message.userID isEqualToString:self.user.chat_userID]) {
|
||||
[self addToShowMessage:message]; // 添加到列表
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
//
|
||||
// TLChatBaseViewController.h
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/2/15.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
#import "TLChatViewControllerProxy.h"
|
||||
#import "TLChatMessageDisplayView.h"
|
||||
#import "TLEmojiDisplayView.h"
|
||||
#import "TLImageExpressionDisplayView.h"
|
||||
#import "TLRecorderIndicatorView.h"
|
||||
|
||||
#import "TLMoreKeyboardDelegate.h"
|
||||
#import "TLMessageManager+MessageRecord.h"
|
||||
|
||||
#import "TLChatBar.h"
|
||||
#import "TLMoreKeyboardDelegate.h"
|
||||
|
||||
#import "TLChatUserProtocol.h"
|
||||
#import "TLUser.h"
|
||||
|
||||
@interface TLChatBaseViewController : UIViewController <TLChatViewControllerProxy, TLMoreKeyboardDelegate>
|
||||
{
|
||||
TLChatBarStatus lastStatus;
|
||||
TLChatBarStatus curStatus;
|
||||
}
|
||||
|
||||
/// 用户信息
|
||||
@property (nonatomic, strong) id<TLChatUserProtocol> user;
|
||||
|
||||
/// 聊天对象
|
||||
@property (nonatomic, strong) id<TLChatUserProtocol> partner;
|
||||
|
||||
/// 消息展示页面
|
||||
@property (nonatomic, strong) TLChatMessageDisplayView *messageDisplayView;
|
||||
|
||||
/// 聊天输入栏
|
||||
@property (nonatomic, strong) TLChatBar *chatBar;
|
||||
|
||||
/// emoji展示view
|
||||
@property (nonatomic, strong) TLEmojiDisplayView *emojiDisplayView;
|
||||
|
||||
/// 图片表情展示view
|
||||
@property (nonatomic, strong) TLImageExpressionDisplayView *imageExpressionDisplayView;
|
||||
|
||||
/// 录音展示view
|
||||
@property (nonatomic, strong) TLRecorderIndicatorView *recorderIndicatorView;;
|
||||
|
||||
/**
|
||||
* 设置“更多”键盘元素
|
||||
*/
|
||||
- (void)setChatMoreKeyboardData:(NSMutableArray *)moreKeyboardData;
|
||||
|
||||
/**
|
||||
* 设置“表情”键盘元素
|
||||
*/
|
||||
- (void)setChatEmojiKeyboardData:(NSMutableArray *)emojiKeyboardData;
|
||||
|
||||
/**
|
||||
* 重置chatVC
|
||||
*/
|
||||
- (void)resetChatVC;
|
||||
|
||||
/**
|
||||
* 发送图片信息
|
||||
*/
|
||||
- (void)sendImageMessage:(UIImage *)image;
|
||||
|
||||
@end
|
||||
+189
@@ -0,0 +1,189 @@
|
||||
//
|
||||
// TLChatBaseViewController.m
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/2/15.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TLChatBaseViewController.h"
|
||||
#import "TLChatBaseViewController+Proxy.h"
|
||||
#import "TLChatBaseViewController+ChatBar.h"
|
||||
#import "TLChatBaseViewController+MessageDisplayView.h"
|
||||
#import "UIImage+Size.h"
|
||||
#import "NSFileManager+TLChat.h"
|
||||
|
||||
@implementation TLChatBaseViewController
|
||||
|
||||
- (void)loadView
|
||||
{
|
||||
[super loadView];
|
||||
|
||||
[[TLMessageManager sharedInstance] setMessageDelegate:self];
|
||||
|
||||
[self.view addSubview:self.messageDisplayView];
|
||||
[self.view addSubview:self.chatBar];
|
||||
|
||||
[self p_addMasonry];
|
||||
}
|
||||
|
||||
- (void)viewDidLoad
|
||||
{
|
||||
[super viewDidLoad];
|
||||
|
||||
[self loadKeyboard];
|
||||
}
|
||||
|
||||
- (void)viewWillAppear:(BOOL)animated
|
||||
{
|
||||
[super viewWillAppear:animated];
|
||||
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
|
||||
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardFrameWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
|
||||
}
|
||||
|
||||
- (void)viewWillDisappear:(BOOL)animated
|
||||
{
|
||||
[super viewWillDisappear:animated];
|
||||
[[TLAudioPlayer sharedAudioPlayer] stopPlayingAudio];
|
||||
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
||||
}
|
||||
|
||||
- (void)dealloc
|
||||
{
|
||||
[[TLMoreKeyboard keyboard] dismissWithAnimation:NO];
|
||||
[[TLEmojiKeyboard keyboard] dismissWithAnimation:NO];
|
||||
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
||||
#ifdef DEBUG_MEMERY
|
||||
NSLog(@"dealloc ChatBaseVC");
|
||||
#endif
|
||||
}
|
||||
|
||||
#pragma mark - # Public Methods
|
||||
- (void)setPartner:(id<TLChatUserProtocol>)partner
|
||||
{
|
||||
if (_partner && [[_partner chat_userID] isEqualToString:[partner chat_userID]]) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
[self.messageDisplayView scrollToBottomWithAnimation:NO];
|
||||
});
|
||||
return;
|
||||
}
|
||||
_partner = partner;
|
||||
[self.navigationItem setTitle:[_partner chat_username]];
|
||||
[self resetChatVC];
|
||||
}
|
||||
|
||||
- (void)setChatMoreKeyboardData:(NSMutableArray *)moreKeyboardData
|
||||
{
|
||||
[self.moreKeyboard setChatMoreKeyboardData:moreKeyboardData];
|
||||
}
|
||||
|
||||
- (void)setChatEmojiKeyboardData:(NSMutableArray *)emojiKeyboardData
|
||||
{
|
||||
[self.emojiKeyboard setEmojiGroupData:emojiKeyboardData];
|
||||
}
|
||||
|
||||
- (void)resetChatVC
|
||||
{
|
||||
NSString *chatViewBGImage;
|
||||
if (self.partner) {
|
||||
chatViewBGImage = [[NSUserDefaults standardUserDefaults] objectForKey:[@"CHAT_BG_" stringByAppendingString:[self.partner chat_userID]]];
|
||||
}
|
||||
if (chatViewBGImage == nil) {
|
||||
chatViewBGImage = [[NSUserDefaults standardUserDefaults] objectForKey:@"CHAT_BG_ALL"];
|
||||
if (chatViewBGImage == nil) {
|
||||
[self.view setBackgroundColor:[UIColor colorGrayCharcoalBG]];
|
||||
}
|
||||
else {
|
||||
NSString *imagePath = [NSFileManager pathUserChatBackgroundImage:chatViewBGImage];
|
||||
UIImage *image = [UIImage imageNamed:imagePath];
|
||||
[self.view setBackgroundColor:[UIColor colorWithPatternImage:image]];
|
||||
}
|
||||
}
|
||||
else {
|
||||
NSString *imagePath = [NSFileManager pathUserChatBackgroundImage:chatViewBGImage];
|
||||
UIImage *image = [UIImage imageNamed:imagePath];
|
||||
[self.view setBackgroundColor:[UIColor colorWithPatternImage:image]];
|
||||
}
|
||||
|
||||
[self resetChatTVC];
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送图片消息
|
||||
*/
|
||||
- (void)sendImageMessage:(UIImage *)image
|
||||
{
|
||||
NSData *imageData = (UIImagePNGRepresentation(image) ? UIImagePNGRepresentation(image) :UIImageJPEGRepresentation(image, 0.5));
|
||||
NSString *imageName = [NSString stringWithFormat:@"%lf.jpg", [NSDate date].timeIntervalSince1970];
|
||||
NSString *imagePath = [NSFileManager pathUserChatImage:imageName];
|
||||
[[NSFileManager defaultManager] createFileAtPath:imagePath contents:imageData attributes:nil];
|
||||
|
||||
TLImageMessage *message = [[TLImageMessage alloc] init];
|
||||
message.fromUser = self.user;
|
||||
message.ownerTyper = TLMessageOwnerTypeSelf;
|
||||
message.imagePath = imageName;
|
||||
message.imageSize = image.size;
|
||||
[self sendMessage:message];
|
||||
}
|
||||
|
||||
#pragma mark - # Private Methods
|
||||
- (void)p_addMasonry
|
||||
{
|
||||
[self.messageDisplayView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.top.and.left.and.right.mas_equalTo(self.view);
|
||||
make.bottom.mas_equalTo(self.chatBar.mas_top);
|
||||
}];
|
||||
[self.chatBar mas_makeConstraints:^(MASConstraintMaker *make) {
|
||||
make.left.and.right.and.bottom.mas_equalTo(self.view);
|
||||
make.height.mas_greaterThanOrEqualTo(TABBAR_HEIGHT);
|
||||
}];
|
||||
[self.view layoutIfNeeded];
|
||||
}
|
||||
|
||||
#pragma mark - # Getter
|
||||
- (TLChatMessageDisplayView *)messageDisplayView
|
||||
{
|
||||
if (_messageDisplayView == nil) {
|
||||
_messageDisplayView = [[TLChatMessageDisplayView alloc] init];
|
||||
[_messageDisplayView setDelegate:self];
|
||||
}
|
||||
return _messageDisplayView;
|
||||
}
|
||||
|
||||
- (TLChatBar *)chatBar
|
||||
{
|
||||
if (_chatBar == nil) {
|
||||
_chatBar = [[TLChatBar alloc] init];
|
||||
[_chatBar setDelegate:self];
|
||||
}
|
||||
return _chatBar;
|
||||
}
|
||||
|
||||
- (TLEmojiDisplayView *)emojiDisplayView
|
||||
{
|
||||
if (_emojiDisplayView == nil) {
|
||||
_emojiDisplayView = [[TLEmojiDisplayView alloc] init];
|
||||
}
|
||||
return _emojiDisplayView;
|
||||
}
|
||||
|
||||
- (TLImageExpressionDisplayView *)imageExpressionDisplayView
|
||||
{
|
||||
if (_imageExpressionDisplayView == nil) {
|
||||
_imageExpressionDisplayView = [[TLImageExpressionDisplayView alloc] init];
|
||||
}
|
||||
return _imageExpressionDisplayView;
|
||||
}
|
||||
|
||||
- (TLRecorderIndicatorView *)recorderIndicatorView
|
||||
{
|
||||
if (_recorderIndicatorView == nil) {
|
||||
_recorderIndicatorView = [[TLRecorderIndicatorView alloc] init];
|
||||
}
|
||||
return _recorderIndicatorView;
|
||||
}
|
||||
|
||||
@end
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
//
|
||||
// TLChatViewControllerProxy.h
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/5/1.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@class TLUser;
|
||||
@class TLImageMessage;
|
||||
@protocol TLChatViewControllerProxy <NSObject>
|
||||
|
||||
@optional;
|
||||
- (void)didClickedUserAvatar:(TLUser *)user;
|
||||
|
||||
- (void)didClickedImageMessages:(NSArray *)imageMessages atIndex:(NSInteger)index;
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user