// // RoomPublicScreenView.m // DingDangApp // // Created by apple on 2023/5/27. // #import "RoomPublicMessageView.h" #import "RoomPublicScreenTextCell.h" #import "RoomPublicScreenCell.h" #import "RoomPublicScreenEmojiCell.h" #import "RoomPublicScreenGiftCell.h" #import "RoomPublicScreenModel.h" #import "DDClientConfig.h" static NSInteger kRoomMessageMaxLength = 1000; @interface RoomPublicMessageView () /**列表**/ @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) RoomInfoModel *roomInfo; ///数据源 //@property (nonatomic,strong) NSMutableArray *datasource; ///数据源 //@property (nonatomic,strong) NSMutableArray *datasource; @end @implementation RoomPublicMessageView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { [self initWithSubView]; } return self; } - (void)initWithSubView { [self addSubview:self.tableView]; [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.top.bottom.equalTo(self).inset(0); }]; } - (void)refreshTableView { [self.tableView reloadData]; if ([self.dataArr count] > 0) { NSInteger toRow = self.dataArr.count - 1; NSIndexPath *toIndexPath = [NSIndexPath indexPathForRow:toRow inSection:0]; NSInteger rows = [self.tableView numberOfRowsInSection:0]; if (toRow < rows) { [self.tableView scrollToRowAtIndexPath:toIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO]; }else{ NSLog(@"越界了。。。"); } } } - (void)roomInfo:(RoomInfoModel *)roomInfo { self.roomInfo = roomInfo; } - (void)handleRecvMessages:(NIMMessage *)message { NIMNotificationObject *notiMsg = (NIMNotificationObject *)message.messageObject; NIMChatroomNotificationContent *content = (NIMChatroomNotificationContent *)notiMsg.content; if (content.eventType == NIMChatroomEventTypeEnter) { NIMChatroomNotificationMember *member = content.targets[0]; if (member.userId.integerValue == [AccountInfoStorage instance].getUid.integerValue) { [self handleFetchHistoryMessage:message]; } } } - (void)handleFetchHistoryMessage:(NIMMessage *)message { NSString *roomId = [NSString stringWithFormat:@"%ld", self.roomInfo.roomId]; NIMHistoryMessageSearchOption *option = [[NIMHistoryMessageSearchOption alloc] init]; option.limit = [DDClientConfig shareConfig].configInfo.roomMessageCount; option.startTime = self.roomInfo.clearScreenTime / 1000.0; option.order = NIMMessageSearchOrderAsc; option.messageTypes = @[@(NIMMessageTypeText)]; [[NIMSDK sharedSDK].chatroomManager fetchMessageHistory:roomId option:option result:^(NSError * _Nullable error, NSArray * _Nullable messages) { if (messages.count) { //如果拉取的数量等于请求的数量,说明这个时间点以后的消息数量大于等于需要拉取的数量,直接拉取最新的50条 if (messages.count == [DDClientConfig shareConfig].configInfo.roomMessageCount) { NIMHistoryMessageSearchOption *option = [[NIMHistoryMessageSearchOption alloc] init]; option.limit = [DDClientConfig shareConfig].configInfo.roomMessageCount; option.order = NIMMessageSearchOrderDesc; option.messageTypes = @[@(NIMMessageTypeText)]; [[NIMSDK sharedSDK].chatroomManager fetchMessageHistory:roomId option:option result:^(NSError * _Nullable error, NSArray * _Nullable messages) { if (self.dataArr.count > kRoomMessageMaxLength) { NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, kRoomMessageMaxLength/2)]; NSArray *needRemoveMsgArray = [self.dataArr objectsAtIndexes:set]; [self.dataArr removeObjectsInArray:needRemoveMsgArray]; } // 执行插入 // for (NIMMessage *item in messages.reverseObjectEnumerator) { // [self.dataArr addObject:[self.messageParser parseMessageAttribute:item]]; // if (!self.isMiniEnter) { /// 最小化进房的话 不需要重新保存 // if (self.roomInfo.isCloseScreen) { // NIMCustomObject *obj = (NIMCustomObject *)item.messageObject; // if (obj.attachment != nil && [obj.attachment isKindOfClass:[AttachmentModel class]]) { // AttachmentModel *attachment = (AttachmentModel *)obj.attachment; // if (attachment.first == CustomMessageType_Update_RoomInfo && attachment.second == Custom_Message_Sub_Update_RoomInfo_MessageState){ // [[XPRoomMiniManager shareManager] saveRoomMessage:item]; // } // } // } else { // [[XPRoomMiniManager shareManager] saveRoomMessage:item]; // } // } // } // [self.messageTableView reloadData]; // //执行插入动画并滚动 // [self scrollToBottom:YES]; // BOOL hideEnter = [self handleHideEnter:message]; // if (!hideEnter) { // ///插入进房消息及房间公告提示 // [self addRoomMessage:message]; // } // if (!roomInfo.hasAnimationEffect) { // [self roomInfoNoGiftAnimationMessage:message]; // } // [self createUserEnterRoomAddRoomtopicMessage]; }]; // return; } if (self.dataArr.count > kRoomMessageMaxLength) { NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, kRoomMessageMaxLength/2)]; NSArray *needRemoveMsgArray = [self.dataArr objectsAtIndexes:set]; [self.dataArr removeObjectsInArray:needRemoveMsgArray]; } // 执行插入 for (NIMMessage *item in messages) { // [RoomPublicScreenModel initMessage:<#(nonnull NSString *)#> user:<#(nonnull UserInfoModel *)#> other_user:<#(nonnull UserInfoModel *)#> giftModel:<#(nonnull DDGiftWallModel *)#> message_type:<#(NSInteger)#> handle_type:<#(NSInteger)#>] // self.dataArr addObject:<#(nonnull id)#> // [self.dataArr addObject:[self.messageParser parseMessageAttribute:item]]; // if (!self.isMiniEnter) {/// 最小化进房的话 不需要重新保存 // if (self.hostDelegate.getRoomInfo.isCloseScreen) { // NIMCustomObject *obj = (NIMCustomObject *)item.messageObject; // if (obj.attachment != nil && [obj.attachment isKindOfClass:[AttachmentModel class]]) { // AttachmentModel *attachment = (AttachmentModel *)obj.attachment; // if (attachment.first == CustomMessageType_Update_RoomInfo && attachment.second == Custom_Message_Sub_Update_RoomInfo_MessageState){ // [[XPRoomMiniManager shareManager] saveRoomMessage:item]; // } // } // } else { // [[XPRoomMiniManager shareManager] saveRoomMessage:item]; // } // } } // [self.messageTableView reloadData]; // //执行插入动画并滚动 // [self scrollToBottom:YES]; } }]; } #pragma mark --- UITableViewDelegate || UITableViewDataSource --- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 10; // return self.dataArr.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (![ToolsObject IsNullWithObject:self.dataArr]) { RoomPublicScreenModel *model; if (indexPath.row < [self.dataArr count]) { model = self.dataArr[indexPath.row]; if (!model) { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"]; } cell.selectionStyle = UITableViewCellSelectionStyleNone; return cell; } if (model.msg_type == 1 || model.msg_type == 2 || model.msg_type == 6 || model.msg_type == 7 ) {//存文本消息 RoomPublicScreenTextCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RoomPublicScreenTextCell"]; cell.selectionStyle = UITableViewCellSelectionStyleNone; if (indexPath.row < self.dataArr.count) { cell.model = self.dataArr[indexPath.row]; WeakSelf(ws); cell.cellTapGesActionBlock = ^(RoomUserInfoModel * _Nonnull userModel) { if (ws.cellTapGesActionBlock) { ws.cellTapGesActionBlock(userModel); } }; } return cell; } if (model.msg_type == 3) {//聊天消息 RoomPublicScreenCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RoomPublicScreenCell"]; cell.selectionStyle = UITableViewCellSelectionStyleNone; if (indexPath.row < self.dataArr.count) { cell.model = self.dataArr[indexPath.row]; WeakSelf(ws); cell.cellTapGesActionBlock = ^(RoomUserInfoModel * _Nonnull userModel) { if (ws.cellTapGesActionBlock) { ws.cellTapGesActionBlock(userModel); } }; } return cell; } if (model.msg_type == 5) {//表情 RoomPublicScreenEmojiCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RoomPublicScreenEmojiCell"]; cell.selectionStyle = UITableViewCellSelectionStyleNone; if (indexPath.row < self.dataArr.count) { cell.model = self.dataArr[indexPath.row]; WeakSelf(ws); cell.cellTapGesActionBlock = ^(RoomUserInfoModel * _Nonnull userModel) { if (ws.cellTapGesActionBlock) { ws.cellTapGesActionBlock(userModel); } }; } return cell; } if (model.msg_type == 4 || model.msg_type == 8) {//抽奖礼物消息 RoomPublicScreenGiftCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RoomPublicScreenGiftCell"]; cell.selectionStyle = UITableViewCellSelectionStyleNone; if (indexPath.row < self.dataArr.count) { cell.model = self.dataArr[indexPath.row]; WeakSelf(ws); cell.cellTapGesActionBlock = ^(RoomUserInfoModel * _Nonnull userModel) { if (ws.cellTapGesActionBlock) { ws.cellTapGesActionBlock(userModel); } }; } return cell; } } } UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"]; } cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.textLabel.text = @"测试展示公屏"; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { // if (indexPath.row < [self.dataArr count]) { // RoomPublicScreenModel * model = self.dataArr[indexPath.row]; // return model.cellH; // }else{ return 40; // } } ///追加数据源 //- (void)appendAndScrollToBottom { // if (self.incomingMessages.count < 1) { // ///滚动到底部(如果有人at自己后,income消息在点击at按钮处做了拼接处理,因为点击at按钮跳转到的是对应的at人消息,如果后面有其他消息时,点有更多按钮时需要滚动到最底部) // [self scrollToBottom:YES]; // return; // } // // if (self.datasource.count > kRoomMessageMaxLength) { // NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, kRoomMessageMaxLength/2)]; // NSArray *needRemoveMsgArray = [self.datasource objectsAtIndexes:set]; // [self.datasource removeObjectsInArray:needRemoveMsgArray]; // } // // // 执行插入 // NSMutableArray *indexPaths = [NSMutableArray array]; // for (NIMMessage *item in self.incomingMessages) { // [self.datasource addObject:[self.messageParser parseMessageAttribute:item]]; // [indexPaths addObject:[NSIndexPath indexPathForRow:self.datasource.count - 1 inSection:0]]; // } // [self.incomingMessages removeAllObjects]; // [self.messageTableView reloadData]; // // //执行插入动画并滚动 // [self scrollToBottom:YES]; //} #pragma mark ------ 懒加载 ------ - (UITableView *)tableView { if (!_tableView) { _tableView = [UITableView new]; _tableView.backgroundColor = UIColor.clearColor; _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; _tableView.delaysContentTouches = NO; _tableView.canCancelContentTouches = YES; _tableView.estimatedRowHeight = 0; _tableView.estimatedSectionHeaderHeight = 0; _tableView.estimatedSectionFooterHeight = 0; _tableView.showsVerticalScrollIndicator = NO; _tableView.showsHorizontalScrollIndicator = NO; // _tableView.rowHeight = 40; if (@available(iOS 11.0, *)) { _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } _tableView.tableFooterView = [UIView new]; if (@available(iOS 15.0, *)) { _tableView.sectionHeaderTopPadding = 0; } else { // Fallback on earlier versions } _tableView.delegate = self; _tableView.dataSource = self; [_tableView registerClass:NSClassFromString(@"RoomPublicScreenTextCell") forCellReuseIdentifier:@"RoomPublicScreenTextCell"]; [_tableView registerClass:NSClassFromString(@"RoomPublicScreenCell") forCellReuseIdentifier:@"RoomPublicScreenCell"]; [_tableView registerClass:NSClassFromString(@"RoomPublicScreenEmojiCell") forCellReuseIdentifier:@"RoomPublicScreenEmojiCell"]; [_tableView registerClass:NSClassFromString(@"RoomPublicScreenGiftCell") forCellReuseIdentifier:@"RoomPublicScreenGiftCell"]; } return _tableView; } @end