XYLiveRoomChatListView.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. //
  2. // XYLiveRoomChatListView.m
  3. // Starbuds
  4. //
  5. // Created by pajia on 2020/1/2.
  6. // Copyright © 2020 翟玉磊. All rights reserved.
  7. //
  8. #import "XYLiveRoomChatListView.h"
  9. #import "XYLiveRoomMessageInfo.h"
  10. #import "KCTimer.h"
  11. #import "WZBGradualTableView.h"
  12. #import "XYLiveRoomChatListTextOrGiftCell.h"
  13. #import "XYLiveRoomChatListAlertCell.h"
  14. #import "XYLiveRoomChatListActivityCell.h"
  15. #define XYLiveRoomChatListMaxNum 500
  16. @interface XYLiveRoomChatListView ()<UITableViewDelegate, UITableViewDataSource>
  17. @property (nonatomic, readwrite, strong) WZBGradualTableView *tableView;
  18. @property (nonatomic, readwrite, strong) NSMutableArray *dataArrs;
  19. @property (nonatomic, readwrite, assign) BOOL tableViewSlide0;
  20. @property (nonatomic, readwrite, assign) BOOL tableViewSlide;
  21. @property (nonatomic, readwrite, assign) float lastContentOffset;
  22. @property (nonatomic, readwrite, strong) UILabel *unreadMsgLab;
  23. @property (nonatomic, readwrite, assign) int unreadMsgCount;
  24. @property (nonatomic, readwrite, strong) UIView *tableHeaderView;
  25. @end
  26. @implementation XYLiveRoomChatListView
  27. - (void)setup{
  28. [self tableView];
  29. [self unreadMsgLab];
  30. [NotificationCenter addObserver:self selector:@selector(XYLiveRoomChatListCellUser:) name:LIVEROOM_CHATLIST_CELL_NOTIFICATION object:nil];
  31. }
  32. - (void)reloadHeaderView:(NSString *)contentTips{
  33. if (contentTips.length) {
  34. AWRichText *richText = [[AWRichText alloc] init];
  35. AWRTTextComponent *textComponent = [[AWRTTextComponent alloc] init]
  36. .AWText(contentTips)
  37. .AWColor([UIColor colorWithHexString:@"#78E5FF" alpha:1])
  38. .AWFont([UIFont fontWithName:kPFSCFont size:14])
  39. .AWPaddingRight(@1);
  40. [richText addComponent:textComponent];
  41. AWRichTextLabel *richTextLabel = richText.createRichTextLabel;
  42. NSAttributedString *abString = [richText attributedString];
  43. float richTextLableHeight = [richText sizeThatFits:CGSizeMake(KLiveRoomChatListCellAlertActivityTichTextLableWidth, 0)].height;
  44. float richTextLableWidth = [richText sizeThatFits:CGSizeMake(0, richTextLableHeight)].width;
  45. if (richTextLableHeight > 0 && !richTextLableWidth) {
  46. richTextLableWidth = KLiveRoomChatListCellWidth;
  47. }
  48. if (richTextLableHeight > 0 && richTextLableWidth > 0) {
  49. richTextLabel.rtFrame = CGRectMake(KLiveRoomChatListCellStringLeftOrRightMargin, KLiveRoomChatListCellStringTopOrBottomMargin, KLiveRoomChatListCellAlertActivityTichTextLableWidth, richTextLableHeight);
  50. float cellHeight = richTextLableHeight + 2*KLiveRoomChatListCellStringTopOrBottomMargin;
  51. float cellShaowWidth = richTextLableWidth + KLiveRoomChatListCellStringLeftOrRightMargin*2;
  52. if (cellShaowWidth > KLiveRoomChatListCellWidth || cellShaowWidth < 30) {
  53. cellShaowWidth = KLiveRoomChatListCellWidth;
  54. }
  55. UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cellShaowWidth, cellHeight)];
  56. headerView.backgroundColor = [UIColor colorWithHexString:@"#000000" alpha:0.25];
  57. [headerView addSubview:richTextLabel];
  58. kViewRadius(headerView, 12);
  59. UIView *tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KLiveRoomChatListCellWidth, cellHeight)];
  60. self.tableHeaderView = tableHeaderView;
  61. [tableHeaderView addSubview:headerView];
  62. self.tableView.tableHeaderView = tableHeaderView;
  63. [self reloadHeaderFrame];
  64. }
  65. }
  66. }
  67. - (void)reloadHeaderFrame{
  68. float tableHeaderViewHeight = self.tableHeaderView.size.height;
  69. if (tableHeaderViewHeight <= 0) { tableHeaderViewHeight = 40; }
  70. float top = self.tableView.f_heigh - tableHeaderViewHeight;
  71. if (top < 0) { top = 0; }
  72. self.tableView.contentInset = UIEdgeInsetsMake(top, 0, 0, 0);
  73. [self.tableView reloadFrame];
  74. }
  75. - (void)reload:(XYLiveRoomMessageInfo *)info{
  76. [self.dataArrs addObject:info];
  77. if (self.dataArrs.count > XYLiveRoomChatListMaxNum) {
  78. [self.dataArrs removeFirstObject];
  79. }
  80. // if (self.dataArrs.count > XYLiveRoomChatListMaxNum+100) {
  81. // [self.dataArrs removeObjectsInRange:NSMakeRange(0, 100)];
  82. // [self unreadMsgTap];
  83. // }
  84. float count = self.tableView.height/40.0;
  85. if (self.dataArrs.count > count) {
  86. self.tableViewSlide0 = YES;
  87. }
  88. //NSLog(@"info=%@ %@",info.giftId,info.giftName);
  89. if (self.tableViewSlide) { self.unreadMsgCount = self.unreadMsgCount + 1; }
  90. [self reloadUnread];
  91. }
  92. /// 关闭聊天公屏
  93. /// @param info 关闭的通知
  94. - (void)chatClose:(XYLiveRoomMessageInfo *)info{
  95. [self.dataArrs removeAllObjects];
  96. [self.tableView reloadData];
  97. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  98. [self.dataArrs addObject:info];
  99. [self unreadMsgTap];
  100. });
  101. }
  102. - (void)reloadUnread{
  103. if (self.tableViewSlide) {
  104. self.unreadMsgLab.hidden = NO;
  105. self.unreadMsgLab.text = [NSString stringWithFormat:@" %d%@ ",self.unreadMsgCount,kLocalizedString(@"条未读消息")];
  106. if (self.unreadMsgCount > XYLiveRoomChatListMaxNum) {
  107. self.unreadMsgLab.text = [NSString stringWithFormat:@" %d+%@ ",XYLiveRoomChatListMaxNum,kLocalizedString(@"条未读消息")];
  108. }
  109. }else{
  110. self.unreadMsgLab.hidden = YES;
  111. [self scrollToBottom];
  112. }
  113. }
  114. - (void)reloadByArray:(NSArray<NSMutableDictionary *> *)array{
  115. for (NSMutableDictionary *dataDict in array) {
  116. if ([self isAdd:dataDict]) {
  117. XYLiveRoomMessageInfo *info = [XYLiveRoomMessageInfo creat:dataDict];
  118. if(self.dataArrs.count < 10){
  119. if (self.tableViewSlide) { self.unreadMsgCount = self.unreadMsgCount + 1; }
  120. [self.dataArrs addObject:info];
  121. }
  122. }
  123. }
  124. [self reloadUnread];
  125. }
  126. - (void)scrollViewDidScroll:(UIScrollView *)scrollView{
  127. if (self.lastContentOffset <= scrollView.contentOffset.y) {
  128. ///NSLog(@"UP");
  129. }else{
  130. if (self.tableViewSlide0 && scrollView.contentOffset.y > 0) {
  131. self.tableViewSlide = YES;
  132. }
  133. ///NSLog(@"DOWN");
  134. }
  135. }
  136. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  137. self.lastContentOffset = scrollView.contentOffset.y;
  138. }
  139. - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
  140. [self parseScrollViewScroll:scrollView];
  141. }
  142. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
  143. [self parseScrollViewScroll:scrollView];
  144. }
  145. //判断滑动到底部
  146. - (void)parseScrollViewScroll:(UIScrollView *)scrollView{
  147. CGFloat scrollViewHeight = scrollView.frame.size.height;
  148. CGFloat contentYoffset = scrollView.contentOffset.y;
  149. CGFloat contentHeight = scrollView.contentSize.height;
  150. CGFloat distanceFromBottom = contentHeight - contentYoffset;
  151. if (distanceFromBottom <= scrollViewHeight) {
  152. [self unreadMsgTap];
  153. }
  154. }
  155. - (void)unreadMsgTap{
  156. self.unreadMsgLab.hidden = YES;
  157. self.unreadMsgCount = 0;
  158. self.tableViewSlide = NO;
  159. self.lastContentOffset = 0.0;
  160. [self scrollToBottom];
  161. }
  162. -(void)scrollToBottom{
  163. [self.tableView reloadData];
  164. NSInteger rows = [self.tableView numberOfRowsInSection:0];
  165. if (rows > 0){
  166. [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:rows - 1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
  167. }
  168. }
  169. - (void)XYLiveRoomChatListCellUser:(NSNotification *)notification{
  170. NSDictionary *parm = notification.userInfo;
  171. XYLiveRoomMessageInfo *info = [[XYLiveRoomMessageInfo alloc] init];
  172. NSString *identifier = parm[@"identifier"];
  173. info.senderId = identifier;
  174. info.type = parm[@"type"];
  175. //标识包含为http为打开h5
  176. if ([identifier containsString:@"http"]) {
  177. info.isActivity = YES;
  178. info.activityUrl = identifier;
  179. }
  180. [self cellContentTapAction:info];
  181. }
  182. - (void)cellContentTapAction:(XYLiveRoomMessageInfo *)info{
  183. if (self.XYLiveRoomChatListViewBlock) {
  184. self.XYLiveRoomChatListViewBlock(info);
  185. }
  186. }
  187. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  188. return 1;
  189. }
  190. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  191. return self.dataArrs.count;
  192. }
  193. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  194. // XYLiveRoomChatListCell *cell = [XYLiveRoomChatListCell cellWithTableView:tableView];
  195. // cell.delegate = self;
  196. // XYLiveRoomMessageInfo *info = self.dataArrs[indexPath.row];
  197. // [cell reload:info];
  198. // return cell;
  199. XYLiveRoomMessageInfo *info = self.dataArrs[indexPath.row];
  200. if (info.isTextOrGift) {
  201. XYLiveRoomChatListTextOrGiftCell *cell = [XYLiveRoomChatListTextOrGiftCell cellWithTableView:tableView];
  202. [cell reload:info];
  203. return cell;
  204. }else if (info.isAlert){
  205. XYLiveRoomChatListAlertCell *cell = [XYLiveRoomChatListAlertCell cellWithTableView:tableView];
  206. [cell reload:info];
  207. return cell;
  208. }else if (info.isActivity){
  209. XYLiveRoomChatListActivityCell *cell = [XYLiveRoomChatListActivityCell cellWithTableView:tableView];
  210. [cell reload:info];
  211. return cell;
  212. }
  213. return [[UITableViewCell alloc] init];;
  214. }
  215. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  216. XYLiveRoomMessageInfo *info = self.dataArrs[indexPath.row];
  217. float height = info.cellHeight;
  218. return height;
  219. }
  220. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  221. // XYLiveRoomMessageInfo *info = self.dataArrs[indexPath.row];
  222. // if (self.XYLiveRoomChatListViewBlock) {
  223. // self.XYLiveRoomChatListViewBlock(info);
  224. // }
  225. }
  226. - (void)clear{
  227. [NotificationCenter removeObserver:self name:LIVEROOM_CHATLIST_CELL_NOTIFICATION object:nil];
  228. }
  229. - (BOOL )isAdd:(NSMutableDictionary *)dataDict{
  230. NSString *type = [NSString kcFormat:dataDict[@"msg"][@"type"]];
  231. // //用户进入直播间
  232. // if ([type isEqualToString:LIVEROOM_USER_JOIN]) {
  233. // return YES;
  234. // }
  235. // //房管设置消息
  236. // if ([type isEqualToString:LIVEROOM_USER_MASTER]) {
  237. // return YES;
  238. // }
  239. // ////被禁止发言当前用户
  240. // if ([type isEqualToString:LIVEROOM_WORD_FORBIDDEN]) {
  241. // return YES;
  242. // }
  243. // ///用户被禁言提示所有人
  244. // if ([type isEqualToString:LIVEROOM_USER_FORBIDDEN]) {
  245. // return YES;
  246. // }
  247. // //直播文本消息
  248. // if ([type isEqualToString:LIVEROOM_MSG_TEXT]) {
  249. // return YES;
  250. // }
  251. // //上麦消息
  252. // if ([type isEqualToString:RTC_SIT_SEAT_MSG]) {
  253. // return YES;
  254. // }
  255. // // 下麦消息
  256. // if ([type isEqualToString:RTC_LEAVE_SEAT_MSG]) {
  257. // return YES;
  258. // }
  259. // //自由上麦状态改变
  260. // if ([type isEqualToString:RTC_CHANGE_MIC_FREE]) {
  261. // return YES;
  262. // }
  263. // //公屏开关状态改变
  264. // if ([type isEqualToString:RTC_CHANGE_CHAT_ENABLED]) {
  265. // return YES;
  266. // }
  267. // //礼物结算
  268. // if ([type isEqualToString:RTC_GIFT_SETTLE]) {
  269. // BOOL showInComment = [dataDict[@"msg"][@"showInComment"] boolValue];
  270. // BOOL isComboTimeout = [dataDict[@"msg"][@"isComboTimeout"] boolValue];
  271. // if (showInComment) { return YES; }
  272. // if (isComboTimeout) { return YES; }
  273. // return NO;
  274. // }
  275. // //语聊房礼物通知
  276. // if ([type isEqualToString:RTC_GIFT_NOTICE]) {
  277. // NSString *noticestyle = [NSString kcFormat:dataDict[@"msg"][@"noticeStyle"]];
  278. // if ([noticestyle integerValue] == 101) {
  279. // NSString *floatScreenAnimationId = [NSString kcFormat:dataDict[@"msg"][@"floatScreenAnimationId"]];
  280. // NSString *giftCombo = [NSString kcFormat:dataDict[@"msg"][@"combo"]];
  281. // if (floatScreenAnimationId.length > 0 || giftCombo.length > 0) {//连击通知
  282. // }else{
  283. // return YES;
  284. // }
  285. // }
  286. // return NO;
  287. // }
  288. // //房间上锁消息
  289. // if ([type isEqualToString:RTC_ROOM_LOCK]) {
  290. // return YES;
  291. // }
  292. // //拉黑(被踢出房间)
  293. // if ([type isEqualToString:LIVEROOM_USER_BLACK]) {
  294. // return YES;
  295. // }
  296. // //中奖喜讯
  297. // if ([type isEqualToString:LIVEROOM_LOTTERY_COMMENT]) {
  298. // return YES;
  299. // }
  300. // //收到表情
  301. // if ([type isEqualToString:RTC_FACIAL_EXPRESSION]) {
  302. // return YES;
  303. // }
  304. return NO;
  305. }
  306. #pragma mark - lazy
  307. - (WZBGradualTableView *)tableView{
  308. if (_tableView == nil) {
  309. _tableView = [[WZBGradualTableView alloc] init];
  310. _tableView.delegate = self;
  311. _tableView.dataSource = self;
  312. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  313. _tableView.backgroundColor = [UIColor clearColor];
  314. _tableView.showsHorizontalScrollIndicator = NO;
  315. _tableView.showsVerticalScrollIndicator = NO;
  316. _tableView.estimatedRowHeight = 0.0f;
  317. _tableView.estimatedSectionHeaderHeight = 0.0f;
  318. _tableView.estimatedSectionFooterHeight = 0.0f;
  319. [self addSubview:_tableView];
  320. [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  321. make.edges.equalTo(self);
  322. }];
  323. [_tableView layoutIfNeeded];
  324. [_tableView setupDirection:WZBTableViewGradualDirectionTop gradualValue:@1];
  325. }
  326. return _tableView;
  327. }
  328. - (UILabel *)unreadMsgLab{
  329. if (_unreadMsgLab == nil) {
  330. _unreadMsgLab = [[UILabel alloc] init];
  331. _unreadMsgLab.backgroundColor = [UIColor whiteColor];
  332. _unreadMsgLab.textColor = [UIColor blackColor];
  333. _unreadMsgLab.font = [UIFont fontWithName:kPFSCFont size:12];
  334. [_unreadMsgLab sizeToFit];
  335. [self addSubview:_unreadMsgLab];
  336. [_unreadMsgLab mas_makeConstraints:^(MASConstraintMaker *make) {
  337. make.left.equalTo(self.tableView.mas_left).offset(0);
  338. make.bottom.equalTo(self.mas_bottom).offset(0);
  339. make.height.offset(30);
  340. }];
  341. kViewRadius(self.unreadMsgLab, 10);
  342. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(unreadMsgTap)];
  343. _unreadMsgLab.userInteractionEnabled = YES;
  344. [_unreadMsgLab addGestureRecognizer:tap];
  345. }
  346. return _unreadMsgLab;
  347. }
  348. - (NSMutableArray *)dataArrs{
  349. if (_dataArrs == nil) {
  350. _dataArrs = [[NSMutableArray alloc] init];
  351. }
  352. return _dataArrs;
  353. }
  354. @end