// // XYLiveRoomChatListView.m // Starbuds // // Created by pajia on 2020/1/2. // Copyright © 2020 翟玉磊. All rights reserved. // #import "XYLiveRoomChatListView.h" #import "XYLiveRoomMessageInfo.h" #import "KCTimer.h" #import "WZBGradualTableView.h" #import "XYLiveRoomChatListTextOrGiftCell.h" #import "XYLiveRoomChatListAlertCell.h" #import "XYLiveRoomChatListActivityCell.h" #define XYLiveRoomChatListMaxNum 500 @interface XYLiveRoomChatListView () @property (nonatomic, readwrite, strong) WZBGradualTableView *tableView; @property (nonatomic, readwrite, strong) NSMutableArray *dataArrs; @property (nonatomic, readwrite, assign) BOOL tableViewSlide0; @property (nonatomic, readwrite, assign) BOOL tableViewSlide; @property (nonatomic, readwrite, assign) float lastContentOffset; @property (nonatomic, readwrite, strong) UILabel *unreadMsgLab; @property (nonatomic, readwrite, assign) int unreadMsgCount; @property (nonatomic, readwrite, strong) UIView *tableHeaderView; @end @implementation XYLiveRoomChatListView - (void)setup{ [self tableView]; [self unreadMsgLab]; [NotificationCenter addObserver:self selector:@selector(XYLiveRoomChatListCellUser:) name:LIVEROOM_CHATLIST_CELL_NOTIFICATION object:nil]; } - (void)reloadHeaderView:(NSString *)contentTips{ if (contentTips.length) { AWRichText *richText = [[AWRichText alloc] init]; AWRTTextComponent *textComponent = [[AWRTTextComponent alloc] init] .AWText(contentTips) .AWColor([UIColor colorWithHexString:@"#78E5FF" alpha:1]) .AWFont([UIFont fontWithName:kPFSCFont size:14]) .AWPaddingRight(@1); [richText addComponent:textComponent]; AWRichTextLabel *richTextLabel = richText.createRichTextLabel; NSAttributedString *abString = [richText attributedString]; float richTextLableHeight = [richText sizeThatFits:CGSizeMake(KLiveRoomChatListCellAlertActivityTichTextLableWidth, 0)].height; float richTextLableWidth = [richText sizeThatFits:CGSizeMake(0, richTextLableHeight)].width; if (richTextLableHeight > 0 && !richTextLableWidth) { richTextLableWidth = KLiveRoomChatListCellWidth; } if (richTextLableHeight > 0 && richTextLableWidth > 0) { richTextLabel.rtFrame = CGRectMake(KLiveRoomChatListCellStringLeftOrRightMargin, KLiveRoomChatListCellStringTopOrBottomMargin, KLiveRoomChatListCellAlertActivityTichTextLableWidth, richTextLableHeight); float cellHeight = richTextLableHeight + 2*KLiveRoomChatListCellStringTopOrBottomMargin; float cellShaowWidth = richTextLableWidth + KLiveRoomChatListCellStringLeftOrRightMargin*2; if (cellShaowWidth > KLiveRoomChatListCellWidth || cellShaowWidth < 30) { cellShaowWidth = KLiveRoomChatListCellWidth; } UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cellShaowWidth, cellHeight)]; headerView.backgroundColor = [UIColor colorWithHexString:@"#000000" alpha:0.25]; [headerView addSubview:richTextLabel]; kViewRadius(headerView, 12); UIView *tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KLiveRoomChatListCellWidth, cellHeight)]; self.tableHeaderView = tableHeaderView; [tableHeaderView addSubview:headerView]; self.tableView.tableHeaderView = tableHeaderView; [self reloadHeaderFrame]; } } } - (void)reloadHeaderFrame{ float tableHeaderViewHeight = self.tableHeaderView.size.height; if (tableHeaderViewHeight <= 0) { tableHeaderViewHeight = 40; } float top = self.tableView.f_heigh - tableHeaderViewHeight; if (top < 0) { top = 0; } self.tableView.contentInset = UIEdgeInsetsMake(top, 0, 0, 0); [self.tableView reloadFrame]; } - (void)reload:(XYLiveRoomMessageInfo *)info{ [self.dataArrs addObject:info]; if (self.dataArrs.count > XYLiveRoomChatListMaxNum) { [self.dataArrs removeFirstObject]; } // if (self.dataArrs.count > XYLiveRoomChatListMaxNum+100) { // [self.dataArrs removeObjectsInRange:NSMakeRange(0, 100)]; // [self unreadMsgTap]; // } float count = self.tableView.height/40.0; if (self.dataArrs.count > count) { self.tableViewSlide0 = YES; } //NSLog(@"info=%@ %@",info.giftId,info.giftName); if (self.tableViewSlide) { self.unreadMsgCount = self.unreadMsgCount + 1; } [self reloadUnread]; } /// 关闭聊天公屏 /// @param info 关闭的通知 - (void)chatClose:(XYLiveRoomMessageInfo *)info{ [self.dataArrs removeAllObjects]; [self.tableView reloadData]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self.dataArrs addObject:info]; [self unreadMsgTap]; }); } - (void)reloadUnread{ if (self.tableViewSlide) { self.unreadMsgLab.hidden = NO; self.unreadMsgLab.text = [NSString stringWithFormat:@" %d%@ ",self.unreadMsgCount,kLocalizedString(@"条未读消息")]; if (self.unreadMsgCount > XYLiveRoomChatListMaxNum) { self.unreadMsgLab.text = [NSString stringWithFormat:@" %d+%@ ",XYLiveRoomChatListMaxNum,kLocalizedString(@"条未读消息")]; } }else{ self.unreadMsgLab.hidden = YES; [self scrollToBottom]; } } - (void)reloadByArray:(NSArray *)array{ for (NSMutableDictionary *dataDict in array) { if ([self isAdd:dataDict]) { XYLiveRoomMessageInfo *info = [XYLiveRoomMessageInfo creat:dataDict]; if(self.dataArrs.count < 10){ if (self.tableViewSlide) { self.unreadMsgCount = self.unreadMsgCount + 1; } [self.dataArrs addObject:info]; } } } [self reloadUnread]; } - (void)scrollViewDidScroll:(UIScrollView *)scrollView{ if (self.lastContentOffset <= scrollView.contentOffset.y) { ///NSLog(@"UP"); }else{ if (self.tableViewSlide0 && scrollView.contentOffset.y > 0) { self.tableViewSlide = YES; } ///NSLog(@"DOWN"); } } - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { self.lastContentOffset = scrollView.contentOffset.y; } - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { [self parseScrollViewScroll:scrollView]; } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ [self parseScrollViewScroll:scrollView]; } //判断滑动到底部 - (void)parseScrollViewScroll:(UIScrollView *)scrollView{ CGFloat scrollViewHeight = scrollView.frame.size.height; CGFloat contentYoffset = scrollView.contentOffset.y; CGFloat contentHeight = scrollView.contentSize.height; CGFloat distanceFromBottom = contentHeight - contentYoffset; if (distanceFromBottom <= scrollViewHeight) { [self unreadMsgTap]; } } - (void)unreadMsgTap{ self.unreadMsgLab.hidden = YES; self.unreadMsgCount = 0; self.tableViewSlide = NO; self.lastContentOffset = 0.0; [self scrollToBottom]; } -(void)scrollToBottom{ [self.tableView reloadData]; NSInteger rows = [self.tableView numberOfRowsInSection:0]; if (rows > 0){ [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:rows - 1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES]; } } - (void)XYLiveRoomChatListCellUser:(NSNotification *)notification{ NSDictionary *parm = notification.userInfo; XYLiveRoomMessageInfo *info = [[XYLiveRoomMessageInfo alloc] init]; NSString *identifier = parm[@"identifier"]; info.senderId = identifier; info.type = parm[@"type"]; //标识包含为http为打开h5 if ([identifier containsString:@"http"]) { info.isActivity = YES; info.activityUrl = identifier; } [self cellContentTapAction:info]; } - (void)cellContentTapAction:(XYLiveRoomMessageInfo *)info{ if (self.XYLiveRoomChatListViewBlock) { self.XYLiveRoomChatListViewBlock(info); } } -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.dataArrs.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ // XYLiveRoomChatListCell *cell = [XYLiveRoomChatListCell cellWithTableView:tableView]; // cell.delegate = self; // XYLiveRoomMessageInfo *info = self.dataArrs[indexPath.row]; // [cell reload:info]; // return cell; XYLiveRoomMessageInfo *info = self.dataArrs[indexPath.row]; if (info.isTextOrGift) { XYLiveRoomChatListTextOrGiftCell *cell = [XYLiveRoomChatListTextOrGiftCell cellWithTableView:tableView]; [cell reload:info]; return cell; }else if (info.isAlert){ XYLiveRoomChatListAlertCell *cell = [XYLiveRoomChatListAlertCell cellWithTableView:tableView]; [cell reload:info]; return cell; }else if (info.isActivity){ XYLiveRoomChatListActivityCell *cell = [XYLiveRoomChatListActivityCell cellWithTableView:tableView]; [cell reload:info]; return cell; } return [[UITableViewCell alloc] init];; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ XYLiveRoomMessageInfo *info = self.dataArrs[indexPath.row]; float height = info.cellHeight; return height; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ // XYLiveRoomMessageInfo *info = self.dataArrs[indexPath.row]; // if (self.XYLiveRoomChatListViewBlock) { // self.XYLiveRoomChatListViewBlock(info); // } } - (void)clear{ [NotificationCenter removeObserver:self name:LIVEROOM_CHATLIST_CELL_NOTIFICATION object:nil]; } - (BOOL )isAdd:(NSMutableDictionary *)dataDict{ NSString *type = [NSString kcFormat:dataDict[@"msg"][@"type"]]; // //用户进入直播间 // if ([type isEqualToString:LIVEROOM_USER_JOIN]) { // return YES; // } // //房管设置消息 // if ([type isEqualToString:LIVEROOM_USER_MASTER]) { // return YES; // } // ////被禁止发言当前用户 // if ([type isEqualToString:LIVEROOM_WORD_FORBIDDEN]) { // return YES; // } // ///用户被禁言提示所有人 // if ([type isEqualToString:LIVEROOM_USER_FORBIDDEN]) { // return YES; // } // //直播文本消息 // if ([type isEqualToString:LIVEROOM_MSG_TEXT]) { // return YES; // } // //上麦消息 // if ([type isEqualToString:RTC_SIT_SEAT_MSG]) { // return YES; // } // // 下麦消息 // if ([type isEqualToString:RTC_LEAVE_SEAT_MSG]) { // return YES; // } // //自由上麦状态改变 // if ([type isEqualToString:RTC_CHANGE_MIC_FREE]) { // return YES; // } // //公屏开关状态改变 // if ([type isEqualToString:RTC_CHANGE_CHAT_ENABLED]) { // return YES; // } // //礼物结算 // if ([type isEqualToString:RTC_GIFT_SETTLE]) { // BOOL showInComment = [dataDict[@"msg"][@"showInComment"] boolValue]; // BOOL isComboTimeout = [dataDict[@"msg"][@"isComboTimeout"] boolValue]; // if (showInComment) { return YES; } // if (isComboTimeout) { return YES; } // return NO; // } // //语聊房礼物通知 // if ([type isEqualToString:RTC_GIFT_NOTICE]) { // NSString *noticestyle = [NSString kcFormat:dataDict[@"msg"][@"noticeStyle"]]; // if ([noticestyle integerValue] == 101) { // NSString *floatScreenAnimationId = [NSString kcFormat:dataDict[@"msg"][@"floatScreenAnimationId"]]; // NSString *giftCombo = [NSString kcFormat:dataDict[@"msg"][@"combo"]]; // if (floatScreenAnimationId.length > 0 || giftCombo.length > 0) {//连击通知 // }else{ // return YES; // } // } // return NO; // } // //房间上锁消息 // if ([type isEqualToString:RTC_ROOM_LOCK]) { // return YES; // } // //拉黑(被踢出房间) // if ([type isEqualToString:LIVEROOM_USER_BLACK]) { // return YES; // } // //中奖喜讯 // if ([type isEqualToString:LIVEROOM_LOTTERY_COMMENT]) { // return YES; // } // //收到表情 // if ([type isEqualToString:RTC_FACIAL_EXPRESSION]) { // return YES; // } return NO; } #pragma mark - lazy - (WZBGradualTableView *)tableView{ if (_tableView == nil) { _tableView = [[WZBGradualTableView alloc] init]; _tableView.delegate = self; _tableView.dataSource = self; _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; _tableView.backgroundColor = [UIColor clearColor]; _tableView.showsHorizontalScrollIndicator = NO; _tableView.showsVerticalScrollIndicator = NO; _tableView.estimatedRowHeight = 0.0f; _tableView.estimatedSectionHeaderHeight = 0.0f; _tableView.estimatedSectionFooterHeight = 0.0f; [self addSubview:_tableView]; [_tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self); }]; [_tableView layoutIfNeeded]; [_tableView setupDirection:WZBTableViewGradualDirectionTop gradualValue:@1]; } return _tableView; } - (UILabel *)unreadMsgLab{ if (_unreadMsgLab == nil) { _unreadMsgLab = [[UILabel alloc] init]; _unreadMsgLab.backgroundColor = [UIColor whiteColor]; _unreadMsgLab.textColor = [UIColor blackColor]; _unreadMsgLab.font = [UIFont fontWithName:kPFSCFont size:12]; [_unreadMsgLab sizeToFit]; [self addSubview:_unreadMsgLab]; [_unreadMsgLab mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.tableView.mas_left).offset(0); make.bottom.equalTo(self.mas_bottom).offset(0); make.height.offset(30); }]; kViewRadius(self.unreadMsgLab, 10); UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(unreadMsgTap)]; _unreadMsgLab.userInteractionEnabled = YES; [_unreadMsgLab addGestureRecognizer:tap]; } return _unreadMsgLab; } - (NSMutableArray *)dataArrs{ if (_dataArrs == nil) { _dataArrs = [[NSMutableArray alloc] init]; } return _dataArrs; } @end