802 lines
30 KiB
Objective-C
802 lines
30 KiB
Objective-C
//
|
||
// MSSessionPublicChatHallVC.m
|
||
// YuMi
|
||
//
|
||
// Created by duoban on 2024/5/6.
|
||
//
|
||
|
||
#import "MSSessionPublicChatHallVC.h"
|
||
#import <IQKeyboardManager/IQKeyboardManager.h>
|
||
#import <TZImagePickerController/TZImagePickerController.h>
|
||
#import <MJRefresh/MJRefresh.h>
|
||
#import <Photos/Photos.h>
|
||
///Tool
|
||
#import "XCCurrentVCStackManager.h"
|
||
#import "NIMMessageMaker.h"
|
||
#import "UITableView+NIMScrollToBottom.h"
|
||
#import "QEmotionHelper.h"
|
||
#import "QKeyboardManager.h"
|
||
#import "ClientConfig.h"
|
||
///Model
|
||
#import "QPhotoImageModel.h"
|
||
#import "AttachmentModel.h"
|
||
#import "MessageBaseModel.h"
|
||
#import "MessageTextModel.h"
|
||
#import "MessageAudioModel.h"
|
||
#import "MessageTimeModel.h"
|
||
#import "MessageImageModel.h"
|
||
#import "MessageUnSupportModel.h"
|
||
#import "MessageTipsModel.h"
|
||
///View
|
||
#import "MessageCell.h"
|
||
#import "XPMineUserInfoViewController.h"
|
||
#import "SessionNavView.h"
|
||
#import "QInputBarView.h"
|
||
#import "QKeyboardManager.h"
|
||
#import "QEmotionBoardView.h"
|
||
#import "QinputPhotoView.h"
|
||
#import "SessionUserInfoTableViewCell.h"
|
||
///P
|
||
#import "MessagePresenter.h"
|
||
#import "MessageProtocol.h"
|
||
#import "Api+Message.h"
|
||
#import "XPRoomMessageConstant.h"
|
||
|
||
@interface MSSessionPublicChatHallVC ()<SessionNavViewDelegate,MessageProtocol, UITableViewDelegate, UITableViewDataSource, NIMChatManagerDelegate, NIMConversationManagerDelegate, NIMMediaManagerDelegate, MessageCellDelegate, TZImagePickerControllerDelegate, SessionNavViewDelegate, QInputBarViewDelegate, QInputBarViewDataSource,InputBoardDataSource ,InputBoardDelegate , QEmotionBoardViewDelegate, QinputPhotoViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate,NIMChatroomManager>
|
||
@property (nonatomic, strong) NIMSession * session;
|
||
@property (nonatomic, strong) NSMutableArray<MessageBaseModel *> * messages;
|
||
@property (nonatomic,strong) SessionNavView *sessionNavView;
|
||
@property (nonatomic, strong) UITableView * sessionTableView;
|
||
@property (nonatomic,strong) NIMMessage *lastMessage;
|
||
@property(nonatomic,strong)QInputBarView *inputBarView;
|
||
@property(nonatomic,strong)QKeyboardManager *keyboardManager;
|
||
@property (nonatomic, strong) UserInfoModel *userInfo;
|
||
///相册
|
||
@property (nonatomic,strong) QinputPhotoView *photoView;
|
||
///
|
||
@property (nonatomic,strong) NSArray *phototArray;
|
||
@property(nonatomic,assign) BOOL isLoadHistoryMessage;
|
||
@property(nonatomic,copy) NSString *publicChatMessageRoomId;
|
||
@end
|
||
|
||
@implementation MSSessionPublicChatHallVC
|
||
- (BOOL)isHiddenNavBar {
|
||
return YES;
|
||
}
|
||
|
||
- (void)dealloc {
|
||
[[NIMSDK sharedSDK].chatManager removeDelegate:self];
|
||
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
||
}
|
||
|
||
- (MessagePresenter *)createPresenter {
|
||
return [[MessagePresenter alloc] init];
|
||
}
|
||
|
||
|
||
|
||
- (void)viewDidLoad {
|
||
[super viewDidLoad];
|
||
self.isLoadHistoryMessage = YES;
|
||
[self initViews];
|
||
[self initLayout];
|
||
[self initHeaderAndFooterRrfresh];
|
||
[IQKeyboardManager sharedManager].enable = NO;
|
||
[IQKeyboardManager sharedManager].enableAutoToolbar = NO;
|
||
|
||
[[NIMSDK sharedSDK].chatManager addDelegate:self];
|
||
|
||
}
|
||
|
||
|
||
|
||
- (void)initHeaderAndFooterRrfresh {
|
||
[self initData];
|
||
}
|
||
|
||
|
||
|
||
#pragma mark - 提前加载相册中的图片
|
||
- (void)loadAlbumPhotos {
|
||
[YYUtility checkAssetsLibrayAvailable:^{
|
||
} denied:^{
|
||
} restriction:^{
|
||
}];
|
||
}
|
||
|
||
#pragma mark - cell的样式
|
||
- (MessageBaseModel *)modeTransformMessage:(NIMMessage *)message {
|
||
MessageBaseModel * model;
|
||
switch (message.messageType) {
|
||
case NIMMessageTypeText:
|
||
model = [[MessageTextModel alloc] initWithMessage:message];
|
||
break;
|
||
case NIMMessageTypeAudio:
|
||
model = [[MessageAudioModel alloc] initWithMessage:message];
|
||
break;
|
||
case NIMMessageTypeImage:
|
||
model = [[MessageImageModel alloc] initWithMessage:message];
|
||
break;
|
||
case NIMMessageTypeTip:
|
||
model = [[MessageTipsModel alloc] initWithMessage:message];
|
||
break;
|
||
|
||
default:
|
||
model = [[MessageUnSupportModel alloc] initWithMessage:message];
|
||
break;
|
||
break;
|
||
}
|
||
return model;
|
||
}
|
||
|
||
|
||
|
||
- (UINavigationController *)getKeyWindowNav {
|
||
if ([XCCurrentVCStackManager shareManager].getCurrentVC) {
|
||
return [XCCurrentVCStackManager shareManager].getCurrentVC.navigationController;
|
||
}
|
||
return self.navigationController;
|
||
}
|
||
|
||
- (MessageBaseModel *)createTimeMessage:(NIMMessage *)message {
|
||
MessageTimeModel * timeModel = [[MessageTimeModel alloc] initWithMessage:message];
|
||
timeModel.time = [self timestrToTimeSecond:[NSString stringWithFormat:@"%f", message.timestamp]];
|
||
return timeModel;
|
||
}
|
||
|
||
- (NSString *)timestrToTimeSecond:(NSString *)timeStr {//timestr 豪秒
|
||
NSTimeInterval interval = [timeStr doubleValue];
|
||
NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
|
||
return [NSDate stringFromDate:date];
|
||
}
|
||
|
||
- (void)addTimeMessage:(NIMMessage *)message {
|
||
if (self.messages.count > 0) {
|
||
NIMMessage * lastMessage = self.messages.lastObject.message;
|
||
CGFloat dur = message.timestamp - lastMessage.timestamp;
|
||
if (dur / 60 > 5) {
|
||
NIMMessage * newMessage = [[NIMMessage alloc] init];
|
||
newMessage.timestamp = message.timestamp;
|
||
[newMessage setValue:self.session forKey:@"session"];
|
||
MessageBaseModel * timeModel = [self createTimeMessage:newMessage];
|
||
[self.messages addObject:timeModel];
|
||
}
|
||
}
|
||
}
|
||
|
||
- (void)addRevokeMessage:(NIMMessage *)message title:(NSString *)title {
|
||
NIMMessage * revokeMessage = [[NIMMessage alloc] init];
|
||
[revokeMessage setValue:self.session forKey:@"session"];
|
||
[revokeMessage setValue:@(NIMMessageTypeTip) forKey:@"messageType"];
|
||
revokeMessage.timestamp = [PLTimeUtil getNowTimeTimestampMillisecond].longLongValue;
|
||
revokeMessage.text = title;
|
||
[[NIMSDK sharedSDK].conversationManager saveMessage:revokeMessage forSession:self.session completion:nil];
|
||
}
|
||
|
||
#pragma mark - Private Method
|
||
|
||
- (void)initInputView {
|
||
// 初始化输入工具条,frame可以先这样临时设置,下面的addBottomInputBarView方法会重置输入条frame
|
||
// 如果你想要自定义输入条View,请参考TextFieldViewController代码
|
||
QInputBarViewConfiguration *inputBarViewConfiguration = [QInputBarViewConfiguration defaultInputBarViewConfiguration];
|
||
inputBarViewConfiguration.voiceButtonHidden = NO;//隐藏语音按钮
|
||
inputBarViewConfiguration.extendButtonHidden = YES;//隐藏拓展按钮
|
||
inputBarViewConfiguration.toolHidden = NO;
|
||
inputBarViewConfiguration.giftButtonHidden = NO;
|
||
inputBarViewConfiguration.cameraButtonHidden = NO;
|
||
inputBarViewConfiguration.photoButtonHidden = NO;
|
||
_inputBarView = [[QInputBarView alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,inputBarViewConfiguration.inputViewHeight)];
|
||
[_inputBarView setupWithConfiguration:inputBarViewConfiguration];
|
||
_inputBarView.delegate = self;
|
||
//keyboard管理类,用来管理键盘,各大面板的切换
|
||
_keyboardManager = [[QKeyboardManager alloc] initWithViewController:self];
|
||
_keyboardManager.dataSource = self;
|
||
//因为addBottomInputBarView方法会立刻触发delegate,所以这里需要先设置delegate
|
||
_keyboardManager.delegate = self;
|
||
[_keyboardManager addBottomInputBarView:_inputBarView belowViewController:NO];
|
||
//把输入框(如果有的话)绑定给管理类
|
||
[_keyboardManager bindTextView:_inputBarView.inputTextView];
|
||
}
|
||
|
||
|
||
- (void)initViews {
|
||
[self.view addSubview:self.sessionNavView];
|
||
[self.view addSubview:self.sessionTableView];
|
||
[self initInputView];
|
||
}
|
||
|
||
- (void)initLayout {
|
||
[self.sessionNavView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.height.mas_equalTo(kNavigationHeight);
|
||
make.leading.top.trailing.mas_equalTo(self.view);
|
||
}];
|
||
}
|
||
|
||
- (void)viewDidLayoutSubviews{
|
||
|
||
}
|
||
|
||
- (void)initData {
|
||
// NIMUser * user = [[NIMSDK sharedSDK].userManager userInfo:self.session.sessionId];
|
||
// if (user) {
|
||
// self.sessionNavView.title = user.userInfo.nickName;
|
||
// } else {
|
||
// self.sessionNavView.title = YMLocalizedString(@"XNDJTDDLoadingTool0");
|
||
// }
|
||
|
||
[self.presenter getUserInfo:self.session.sessionId];
|
||
}
|
||
|
||
- (BOOL)isExistMessages:(NIMMessage *)message{
|
||
BOOL isExist = NO;
|
||
NIMMessage * model;
|
||
for (NIMMessage *item in self.messages.reverseObjectEnumerator.allObjects) {
|
||
if ([item isKindOfClass:[NIMMessage class]] && [item.messageId isEqualToString:message.messageId]) {
|
||
model = item;
|
||
isExist = YES;
|
||
break;
|
||
}
|
||
}
|
||
if (model) {
|
||
model = message;
|
||
}
|
||
return isExist;
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
///获取用户信息成功
|
||
- (void)onGetSessionUserInfoSuccess:(UserInfoModel *)userInfo {
|
||
_userInfo = userInfo;
|
||
self.sessionNavView.userInfo = userInfo;
|
||
self.publicChatMessageRoomId = [ClientConfig shareConfig].configInfo.publicChatRoomIdMap[_userInfo.partitionId];
|
||
|
||
if(self.publicChatMessageRoomId.length > 0){
|
||
_session = [NIMSession session:self.publicChatMessageRoomId type:NIMSessionTypeChatroom];
|
||
[self.presenter enterNIMRoom:self.publicChatMessageRoomId user:userInfo];
|
||
}
|
||
|
||
|
||
}
|
||
|
||
///进入聊天室成功
|
||
-(void)enterPublicChatHallSuccess:(NIMChatroom *)chatRoom{
|
||
[self handleFetchHistoryMessage];
|
||
}
|
||
///进入聊天室失败
|
||
-(void)enterPublicChatHallFail:(NSInteger)code{
|
||
|
||
}
|
||
|
||
- (void)handleFetchHistoryMessage {
|
||
|
||
NSString *roomId = self.publicChatMessageRoomId;
|
||
NIMHistoryMessageSearchOption *option = [[NIMHistoryMessageSearchOption alloc] init];
|
||
option.limit = 100;
|
||
option.order = NIMMessageSearchOrderAsc;
|
||
option.messageTypes = @[@(NIMMessageTypeText),@(NIMMessageTypeImage),@(NIMMessageTypeAudio)];
|
||
[[NIMSDK sharedSDK].chatroomManager fetchMessageHistory:roomId option:option result:^(NSError * _Nullable error, NSArray<NIMMessage *> * _Nullable messages) {
|
||
if(error != nil){
|
||
self.isLoadHistoryMessage = NO;
|
||
}
|
||
NIMHistoryMessageSearchOption *option = [[NIMHistoryMessageSearchOption alloc] init];
|
||
option.limit = [ClientConfig shareConfig].configInfo.roomMessageCount;
|
||
option.order = NIMMessageSearchOrderDesc;
|
||
option.messageTypes = @[@(NIMMessageTypeText),@(NIMMessageTypeCustom)];
|
||
[[NIMSDK sharedSDK].chatroomManager fetchMessageHistory:roomId option:option result:^(NSError * _Nullable error, NSArray<NIMMessage *> * _Nullable messages) {
|
||
if(error != nil){
|
||
self.isLoadHistoryMessage = NO;
|
||
}
|
||
dispatch_async(dispatch_get_main_queue(), ^{///回到主线程
|
||
///
|
||
if (self.messages.count > kRoomMessageMaxLength) {
|
||
NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, kRoomMessageMaxLength/2)];
|
||
NSArray *needRemoveMsgArray = [self.messages objectsAtIndexes:set];
|
||
[self.messages removeObjectsInArray:needRemoveMsgArray];
|
||
}else{
|
||
|
||
for (NIMMessage *message in messages) {
|
||
[self addTimeMessage:message];
|
||
if (message.isDeleted) {
|
||
continue;
|
||
}
|
||
MessageBaseModel * model = [self modeTransformMessage:message];
|
||
[self.messages addObject:model];
|
||
}
|
||
}
|
||
// 执行插入
|
||
|
||
[self.sessionTableView reloadData];
|
||
//执行插入动画并滚动
|
||
if(self.messages.count > 0){
|
||
NSIndexPath *ip = [NSIndexPath indexPathForRow:self.messages.count-1 inSection:0]; //取最后一行数据
|
||
[self.sessionTableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionBottom animated:YES]; //滚动到最后一行
|
||
|
||
}
|
||
|
||
});
|
||
}];
|
||
|
||
}];
|
||
}
|
||
|
||
#pragma mark - MessageCellDelegate
|
||
- (void)updateMessageSuccess:(NIMMessage *)message {
|
||
if ([message.session.sessionId isEqualToString:self.session.sessionId]) {
|
||
[self.sessionTableView reloadData];
|
||
}
|
||
}
|
||
|
||
- (void)didTapAvatar:(NSString *)uid {
|
||
XPMineUserInfoViewController * infoVC = [[XPMineUserInfoViewController alloc] init];
|
||
infoVC.uid = uid.integerValue;
|
||
[[self getKeyWindowNav] pushViewController:infoVC animated:YES];
|
||
}
|
||
|
||
|
||
|
||
|
||
#pragma mark - SessionNavViewDelegate
|
||
|
||
|
||
- (void)sessionNavView:(SessionNavView *)view didClickBack:(UIButton *)sender {
|
||
[self.navigationController popViewControllerAnimated:YES];
|
||
}
|
||
|
||
|
||
|
||
|
||
#pragma mark - UITableViewDataSource
|
||
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
|
||
return 1;
|
||
}
|
||
|
||
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
|
||
|
||
return self.messages.count;
|
||
|
||
}
|
||
|
||
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
|
||
|
||
MessageBaseModel * message = [self.messages safeObjectAtIndex1:indexPath.row];
|
||
NSString * identifier = [message cellContent:message];
|
||
///从复用池中获取所需要的cell
|
||
MessageCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
|
||
if (!cell) {
|
||
///如果没有的话 根据identifier 注册一下 重新获取一下即可
|
||
[tableView registerClass:[MessageCell class] forCellReuseIdentifier:identifier];
|
||
///如果注册过了 就不需要判断cell是否为空
|
||
cell = [tableView dequeueReusableCellWithIdentifier:identifier];
|
||
}
|
||
cell.delegate = self;
|
||
[cell renderWithMessage:[self.messages safeObjectAtIndex1:indexPath.row]];
|
||
return cell;
|
||
|
||
}
|
||
|
||
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
|
||
MessageBaseModel *msg = [self.messages safeObjectAtIndex1:indexPath.row];
|
||
return msg.height;
|
||
}
|
||
|
||
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
|
||
[self.keyboardManager hideAllBoardView];
|
||
}
|
||
|
||
#pragma mark - NIMChatManagerDelegate
|
||
- (void)onRecvMessages:(NSArray<NIMMessage *> *)messages {
|
||
if(!messages.count){
|
||
return;
|
||
}
|
||
NIMMessage *message = messages.firstObject;
|
||
NIMSession *session = message.session;
|
||
|
||
if (self.messages.count > kRoomMessageMaxLength) {
|
||
NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, kRoomMessageMaxLength/2)];
|
||
NSArray *needRemoveMsgArray = [self.messages objectsAtIndexes:set];
|
||
[self.messages removeObjectsInArray:needRemoveMsgArray];
|
||
}
|
||
for (NIMMessage *message in messages) {
|
||
[self addTimeMessage:message];
|
||
if (message.isDeleted) {
|
||
continue;
|
||
}
|
||
MessageBaseModel * model = [self modeTransformMessage:message];
|
||
[self.messages addObject:model];
|
||
}
|
||
[self.sessionTableView reloadData];
|
||
[self.sessionTableView nim_scrollToBottom:YES];
|
||
|
||
}
|
||
|
||
- (void)sendMessage:(NIMMessage *)message didCompleteWithError:(NSError *)error {
|
||
if (message.session.sessionType != NIMSessionTypeChatroom) {
|
||
return;
|
||
}
|
||
|
||
[self addTimeMessage:message];
|
||
|
||
if (![self isExistMessages:message]) {
|
||
MessageBaseModel * model = [self modeTransformMessage:message];
|
||
[self.messages addObject:model];
|
||
}
|
||
|
||
if (message.yidunAntiSpamRes) {
|
||
NSDictionary * spamRes = message.yidunAntiSpamRes.toJSONObject;
|
||
NSDictionary * spamResExt = ((NSString *)spamRes[@"ext"]).toJSONObject;
|
||
if ([spamResExt[@"antispam"][@"suggestion"] intValue] == 2) {
|
||
NSDictionary * dic = @{@"suggestion": @"2"};
|
||
message.localExt = dic;
|
||
[[NIMSDK sharedSDK].conversationManager updateMessage:message forSession:self.session completion:nil];
|
||
}
|
||
}
|
||
[self.sessionTableView reloadData];
|
||
[self.sessionTableView nim_scrollToBottom:YES];
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
- (void)sendTextMessage:(NSString *)text {
|
||
|
||
NIMMessage *message = [NIMMessageMaker msgWithText:text];
|
||
|
||
[[[NIMSDK sharedSDK] chatManager] sendMessage:message toSession:self.session error:nil];
|
||
|
||
|
||
|
||
}
|
||
|
||
- (void)audioRecordCompletion:(NSString *)recordPath {
|
||
|
||
NIMMessage *message = [NIMMessageMaker msgWithAudio:recordPath];
|
||
|
||
[[[NIMSDK sharedSDK] chatManager] sendMessage:message toSession:self.session error:nil];
|
||
|
||
|
||
}
|
||
|
||
#pragma mark - TZImagePickerControllerDelegate
|
||
- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray<UIImage *> *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto infos:(NSArray<NSDictionary *> *)infos {
|
||
[photos enumerateObjectsUsingBlock:^(UIImage * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
|
||
if (obj) {
|
||
NIMMessage * message = [NIMMessageMaker msgWithImage:obj];
|
||
// 发送消息
|
||
[[NIMSDK sharedSDK].chatManager sendMessage:message toSession:self.session error:nil];
|
||
|
||
|
||
}
|
||
}];
|
||
}
|
||
|
||
|
||
|
||
#pragma mark - SessionUserInfoTableViewCellDelegate
|
||
- (void)sessionUserInfoTableViewCell:(SessionUserInfoTableViewCell *)view showUserInfoVC:(UserInfoModel *)userInfo {
|
||
[self didTapAvatar:[NSString stringWithFormat:@"%ld", userInfo.uid]];
|
||
}
|
||
|
||
#pragma mark - InputBoardDataSource
|
||
//@return 点加号按钮弹出的拓展面板View,且无需设置frame
|
||
- (UIView *)keyboardManagerExtendBoardView:(QKeyboardManager *)keyboardManager {
|
||
UIView *boardView = [UIView new];
|
||
boardView.backgroundColor = UIColor.clearColor;
|
||
return boardView;
|
||
}
|
||
|
||
//@return 点表情按钮弹出的表情面板View,且无需设置frame
|
||
- (UIView *)keyboardManagerEmotionBoardView:(QKeyboardManager *)keyboardManager {
|
||
QEmotionBoardView *emotionView = [[QEmotionBoardView alloc] init];
|
||
QEmotionHelper *faceManager = [QEmotionHelper sharedEmotionHelper];
|
||
emotionView.emotions = faceManager.emotionArray;
|
||
emotionView.delegate = self;
|
||
if (@available(iOS 11.0, *)) {
|
||
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
|
||
emotionView.backgroundColor = [UIColor colorNamed:@"q_input_extend_bg" inBundle:bundle compatibleWithTraitCollection:nil];
|
||
} else {
|
||
emotionView.backgroundColor = [UIColor colorWithRed:(246)/255.0f green:(246)/255.0f blue:(246)/255.0f alpha:1];
|
||
}
|
||
return emotionView;
|
||
}
|
||
|
||
//@return 点表情按钮弹出的表情面板View,且无需设置frame
|
||
- (UIView *)keyboardManagerPhotoBoardView:(QKeyboardManager *)keyboardManager{
|
||
QinputPhotoView *emotionView = [[QinputPhotoView alloc] init];
|
||
self.photoView = emotionView;
|
||
emotionView.photoList = self.phototArray;
|
||
emotionView.delegate = self;
|
||
emotionView.backgroundColor = [UIColor colorWithRed:(246)/255.0f green:(246)/255.0f blue:(246)/255.0f alpha:1];
|
||
return emotionView;
|
||
}
|
||
|
||
//@return 点表情按钮弹出的表情面板View的高度
|
||
- (CGFloat)keyboardManagerEmotionBoardHeight:(QKeyboardManager *)keyboardManager {
|
||
return 294;
|
||
}
|
||
|
||
//@return 点加号按钮弹出的拓展面板View的高度
|
||
- (CGFloat)keyboardManagerExtendBoardHeight:(QKeyboardManager *)keyboardManager {
|
||
return 174;
|
||
}
|
||
|
||
- (CGFloat)keyboardManagerPhotoBoardHeight:(QKeyboardManager *)keyboardManager {
|
||
return 184 + kSafeAreaBottomHeight;
|
||
}
|
||
|
||
//点击表情按钮,切换到表情面板
|
||
- (void)inputBarView:(QInputBarView *)inputBarView onEmotionButtonClick:(UIButton *)emotionSwitchButton {
|
||
if (emotionSwitchButton.isSelected) {
|
||
[_keyboardManager switchToEmotionBoardKeyboard];
|
||
} else {
|
||
_inputBarView.inputTextView.text = @"";
|
||
[_inputBarView textViewBecomeFirstResponder];
|
||
}
|
||
}
|
||
|
||
- (void)inputBarView:(QInputBarView *)inputBarView onVoiceSwitchButtonClick:(UIButton *)voiceSwitchButton {
|
||
if (!voiceSwitchButton.isSelected) {
|
||
_inputBarView.inputTextView.text = @"";
|
||
[_inputBarView textViewBecomeFirstResponder];
|
||
} else {
|
||
[_keyboardManager hideAllBoardView];
|
||
}
|
||
}
|
||
|
||
- (void)inputBarView:(QInputBarView *)inputBarView onGiftButtonClick:(UIButton *)emotionSwitchButton {
|
||
[_keyboardManager hideAllBoardView];
|
||
|
||
}
|
||
|
||
- (void)inputBarView:(QInputBarView *)inputBarView onCameraButtonClick:(UIButton *)emotionSwitchButton {
|
||
[_keyboardManager hideAllBoardView];
|
||
@kWeakify(self);
|
||
[YYUtility checkCameraAvailable:^{
|
||
@kStrongify(self);
|
||
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
|
||
imagePicker.delegate = self;
|
||
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
|
||
imagePicker.allowsEditing = YES;
|
||
[self presentViewController:imagePicker animated:YES completion:NULL];
|
||
} denied:^{
|
||
@kStrongify(self);
|
||
[self showNotPhoto:YMLocalizedString(@"SessionViewController19") content:YMLocalizedString(@"SessionViewController10")];
|
||
} restriction:^{
|
||
@kStrongify(self);
|
||
[self showNotPhoto:YMLocalizedString(@"SessionViewController19") content:YMLocalizedString(@"SessionViewController12")];
|
||
}];
|
||
|
||
}
|
||
|
||
- (void)inputBarView:(QInputBarView *)inputBarView onPhototButtonClick:(UIButton *)emotionSwitchButton {
|
||
if (emotionSwitchButton.isSelected) {
|
||
@kWeakify(self);
|
||
[YYUtility checkAssetsLibrayAvailable:^{
|
||
@kStrongify(self);
|
||
[TZImagePickerConfig sharedInstance].allowPickingImage = YES;
|
||
dispatch_async(dispatch_get_global_queue(0, 0), ^{
|
||
[[TZImageManager manager] getCameraRollAlbumWithFetchAssets:NO completion:^(TZAlbumModel *model) {
|
||
[[TZImageManager manager] getAssetsFromFetchResult:model.result completion:^(NSArray<TZAssetModel *> *models) {
|
||
dispatch_async(dispatch_get_main_queue(), ^{
|
||
NSMutableArray * array = [NSMutableArray array];
|
||
if (models.count > 40) {
|
||
for (int i = 0; i < 40; i++) {
|
||
TZAssetModel * assets = [models objectAtIndex:i];
|
||
QPhotoImageModel * infor = [[QPhotoImageModel alloc] init];
|
||
infor.isOrigin = NO;
|
||
infor.assetInfo = assets;
|
||
[array addObject:infor];
|
||
}
|
||
} else {
|
||
for (int i = 0; i < models.count; i++) {
|
||
TZAssetModel * assets = [models objectAtIndex:i];
|
||
QPhotoImageModel * infor = [[QPhotoImageModel alloc] init];
|
||
infor.isOrigin = NO;
|
||
infor.assetInfo = assets;
|
||
[array addObject:infor];
|
||
}
|
||
}
|
||
self.phototArray = array;
|
||
if (self.phototArray.count > 0) {
|
||
self.photoView.photoList = self.phototArray;
|
||
[self.keyboardManager switchToPhotoBoardKeyboard];
|
||
} else {
|
||
TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:self];
|
||
imagePickerVc.modalPresentationStyle = UIModalPresentationOverFullScreen;
|
||
imagePickerVc.allowPickingVideo = NO;
|
||
imagePickerVc.allowTakeVideo = NO;
|
||
imagePickerVc.naviBgColor = [DJDKMIMOMColor appCellBackgroundColor];
|
||
imagePickerVc.naviTitleColor = [DJDKMIMOMColor mainTextColor];
|
||
imagePickerVc.barItemTextColor = [DJDKMIMOMColor mainTextColor];
|
||
[self presentViewController:imagePickerVc animated:YES completion:nil];
|
||
}
|
||
});
|
||
}];
|
||
}];
|
||
});
|
||
} denied:^{
|
||
@kStrongify(self);
|
||
[self showNotPhoto:YMLocalizedString(@"SessionViewController20") content:YMLocalizedString(@"SessionViewController14")];
|
||
} restriction:^{
|
||
@kStrongify(self);
|
||
[self showNotPhoto:YMLocalizedString(@"SessionViewController20") content:YMLocalizedString(@"SessionViewController16")];
|
||
}];
|
||
} else {
|
||
[_keyboardManager hideAllBoardView];
|
||
_inputBarView.inputTextView.text = @"";
|
||
[_inputBarView textViewResignFirstResponder];
|
||
}
|
||
|
||
}
|
||
|
||
- (void)showNotPhoto:(NSString *)title content:(NSString *)content {
|
||
TTAlertConfig *config = [[TTAlertConfig alloc] init];
|
||
config.title = title;
|
||
config.message = content;
|
||
|
||
[TTPopup alertWithConfig:config confirmHandler:^{
|
||
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
|
||
if ([[UIApplication sharedApplication] canOpenURL:url]) {
|
||
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
|
||
|
||
}];
|
||
}
|
||
} cancelHandler:^{
|
||
}];
|
||
}
|
||
|
||
#pragma mark - InputBoardDelegate
|
||
- (void)keyboardManager:(QKeyboardManager *)keyboardManager onWholeInputViewHeightDidChange:(CGFloat)wholeInputViewHeight reason:(WholeInputViewHeightDidChangeReason)reason {
|
||
[UIView animateWithDuration:0.2 animations:^{
|
||
[self.sessionTableView nim_scrollToBottom:NO];
|
||
[self.sessionTableView mas_remakeConstraints:^(MASConstraintMaker *make) {
|
||
make.leading.trailing.mas_equalTo(self.view);
|
||
make.top.mas_equalTo(self.sessionNavView.mas_bottom);
|
||
make.bottom.mas_equalTo(self.view).offset(-wholeInputViewHeight - kSafeAreaBottomHeight);
|
||
}];
|
||
}];
|
||
}
|
||
|
||
#pragma mark - QEmotionBoardViewDelegate
|
||
/**
|
||
* 选中表情时的回调
|
||
* @param index 被选中的表情在`emotions`里的索引
|
||
* @param emotion 被选中的表情对应的`QMUIEmotion`对象
|
||
*/
|
||
- (void)emotionView:(QEmotionBoardView *)emotionView didSelectEmotion:(QEmotion *)emotion atIndex:(NSInteger)index {
|
||
//把😊插入到输入栏
|
||
[_inputBarView insertEmotion:emotion.displayName];
|
||
}
|
||
|
||
// 删除按钮的点击事件回调
|
||
- (void)emotionViewDidSelectDeleteButton:(QEmotionBoardView *)emotionView {
|
||
if (![_inputBarView deleteEmotion]){
|
||
//根据当前的光标,这次点击删除按钮并没有删除表情,那么就删除文字
|
||
[_inputBarView.inputTextView deleteBackward];
|
||
}
|
||
}
|
||
|
||
// 发送按钮的点击事件回调
|
||
- (void)emotionViewDidSelectSendButton:(QEmotionBoardView *)emotionView {
|
||
[self sendTextMessage:[_inputBarView textViewInputNormalText]];
|
||
_inputBarView.inputTextView.text = @"";
|
||
[_keyboardManager hideAllBoardView];
|
||
|
||
}
|
||
|
||
/**
|
||
* 点击了系统键盘的发送按钮
|
||
* @param inputNormalText :"害~你好啊[微笑]"
|
||
*/
|
||
- (void)inputBarView:(QInputBarView *)inputBarView onKeyboardSendClick:(NSString *)inputNormalText {
|
||
[self sendTextMessage:inputNormalText];
|
||
_inputBarView.inputTextView.text = @"";
|
||
[_keyboardManager hideAllBoardView];
|
||
}
|
||
|
||
#pragma mark - QinputPhotoViewDelegate
|
||
- (void)qinputPhotoView:(QinputPhotoView *)view didClickSend:(NSArray *)photoList {
|
||
for (int i = 0; i < photoList.count; i++) {
|
||
QPhotoImageModel * imageInfo = [photoList safeObjectAtIndex1:i];
|
||
UIImage * image;
|
||
if (view.isOrigin) {
|
||
image = imageInfo.originImage;
|
||
} else {
|
||
image = imageInfo.compressImage;
|
||
}
|
||
if (image == nil){
|
||
continue;
|
||
}
|
||
// 发送消息
|
||
NIMMessage * message = [NIMMessageMaker msgWithImage:image];
|
||
|
||
|
||
[[NIMSDK sharedSDK].chatManager sendMessage:message toSession:self.session error:nil];
|
||
[view resetChoosePhotos];
|
||
|
||
|
||
|
||
|
||
}
|
||
}
|
||
|
||
- (void)qinputPhotoView:(QinputPhotoView *)view didClickPhoto:(UIButton *)sender {
|
||
TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:self];
|
||
imagePickerVc.modalPresentationStyle = UIModalPresentationOverFullScreen;
|
||
imagePickerVc.allowPickingVideo = NO;
|
||
imagePickerVc.allowTakeVideo = NO;
|
||
imagePickerVc.naviBgColor = [DJDKMIMOMColor appCellBackgroundColor];
|
||
imagePickerVc.naviTitleColor = [DJDKMIMOMColor mainTextColor];
|
||
imagePickerVc.barItemTextColor = [DJDKMIMOMColor mainTextColor];
|
||
[self presentViewController:imagePickerVc animated:YES completion:nil];
|
||
}
|
||
|
||
#pragma mark - UIImagePickerControllerDelegate
|
||
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
|
||
{
|
||
UIImage *selectedPhoto = [info objectForKey:UIImagePickerControllerEditedImage];
|
||
if (selectedPhoto) {
|
||
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
|
||
UIImageWriteToSavedPhotosAlbum(selectedPhoto, nil, nil, nil);
|
||
}
|
||
NIMMessage * message = [NIMMessageMaker msgWithImage:selectedPhoto];
|
||
|
||
// 发送消息
|
||
[[NIMSDK sharedSDK].chatManager sendMessage:message toSession:self.session error:nil];
|
||
|
||
|
||
}
|
||
[picker dismissViewControllerAnimated:YES completion:^{}];
|
||
}
|
||
|
||
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
|
||
[picker dismissViewControllerAnimated:YES completion:^{
|
||
}];
|
||
}
|
||
|
||
|
||
#pragma mark - Getters And Setters
|
||
- (NSArray<MessageBaseModel *> *)messages {
|
||
if (!_messages) {
|
||
_messages = [[NSMutableArray alloc] init];
|
||
}
|
||
return _messages;
|
||
}
|
||
|
||
#pragma mark - Event Response
|
||
- (void)closeKeyBoard {
|
||
[self.view endEditing:YES];
|
||
[self.inputBarView textViewResignFirstResponder];
|
||
}
|
||
|
||
- (UITableView *)sessionTableView {
|
||
if (!_sessionTableView) {
|
||
_sessionTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
|
||
_sessionTableView.delegate = self;
|
||
_sessionTableView.dataSource = self;
|
||
_sessionTableView.backgroundColor = UIColor.clearColor;
|
||
_sessionTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
|
||
_sessionTableView.showsVerticalScrollIndicator = NO;
|
||
if (@available(iOS 11.0, *)) {
|
||
_sessionTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
|
||
}
|
||
}
|
||
return _sessionTableView;
|
||
}
|
||
|
||
- (SessionNavView *)sessionNavView {
|
||
if (!_sessionNavView) {
|
||
_sessionNavView = [[SessionNavView alloc] init];
|
||
_sessionNavView.delegate = self;
|
||
_sessionNavView.isPublicChatHall = YES;
|
||
}
|
||
return _sessionNavView;
|
||
}
|
||
|
||
|
||
@end
|