YOUPAIBBNIMSessionListViewController.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. //
  2. // YOUPAIBBNIMSessionListViewController.m
  3. // VQU
  4. //
  5. // Created by Elaine on 2021/10/22.
  6. // Copyright © 2021 MS. All rights reserved.
  7. //
  8. #import "YOUPAIBBNIMSessionListViewController.h"
  9. #import "YOUPAIBBNIMSessionViewController.h"
  10. #import "YOUPAIBBNIMSessionListCell.h"
  11. #import "UIView+NIM.h"
  12. #import "NIMAvatarImageView.h"
  13. #import "NIMKitUtil.h"
  14. #import "NIMKit.h"
  15. #import "NTESSessionUtil.h"
  16. @interface YOUPAIBBNIMSessionListViewController ()
  17. @end
  18. @implementation YOUPAIBBNIMSessionListViewController
  19. - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
  20. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  21. if (self) {
  22. }
  23. return self;
  24. }
  25. - (void)dealloc{
  26. [[NIMSDK sharedSDK].conversationManager removeDelegate:self];
  27. [[NIMSDK sharedSDK].loginManager removeDelegate:self];
  28. [[NSNotificationCenter defaultCenter] removeObserver:self];
  29. }
  30. - (void)viewDidLoad {
  31. [super viewDidLoad];
  32. self.view.backgroundColor = LCBkgColor;
  33. self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
  34. [self.view addSubview:self.tableView];
  35. self.tableView.delegate = self;
  36. self.tableView.dataSource = self;
  37. self.tableView.tableFooterView = [[UIView alloc] init];
  38. self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  39. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  40. if (@available(iOS 15.0, *)) {
  41. self.tableView.sectionHeaderTopPadding = 0;
  42. }
  43. _recentSessions = [[NIMSDK sharedSDK].conversationManager.allRecentSessions mutableCopy];
  44. if (!self.recentSessions.count)
  45. {
  46. _recentSessions = [NSMutableArray array];
  47. }
  48. else
  49. {
  50. _recentSessions = [self customSortRecents:_recentSessions];
  51. }
  52. [[NIMSDK sharedSDK].conversationManager addDelegate:self];
  53. [[NIMSDK sharedSDK].loginManager addDelegate:self];
  54. extern NSString *const NIMKitTeamInfoHasUpdatedNotification;
  55. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onTeamInfoHasUpdatedNotification:) name:NIMKitTeamInfoHasUpdatedNotification object:nil];
  56. extern NSString *const NIMKitTeamMembersHasUpdatedNotification;
  57. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onTeamMembersHasUpdatedNotification:) name:NIMKitTeamMembersHasUpdatedNotification object:nil];
  58. extern NSString *const NIMKitUserInfoHasUpdatedNotification;
  59. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onUserInfoHasUpdatedNotification:) name:NIMKitUserInfoHasUpdatedNotification object:nil];
  60. }
  61. - (void)refresh{
  62. if (!self.recentSessions.count) {
  63. self.tableView.hidden = YES;
  64. }else{
  65. self.tableView.hidden = NO;
  66. }
  67. //过虑系统通知和动态通知
  68. for (int i = 0; i< self.recentSessions.count; i++) {
  69. NIMRecentSession *recentSession = self.recentSessions[i];
  70. if ([recentSession.session.sessionId isEqualToString:@"11"]) {
  71. [self.recentSessions removeObject:recentSession];
  72. }
  73. }
  74. for (int i = 0; i< self.recentSessions.count; i++) {
  75. NIMRecentSession *recentSession = self.recentSessions[i];
  76. if ([recentSession.session.sessionId isEqualToString:@"2"] ) {
  77. [self.recentSessions removeObject:recentSession];
  78. }
  79. }
  80. [self.tableView reloadData];
  81. }
  82. #pragma mark - UITableViewDelegate
  83. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  84. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  85. NIMRecentSession *recentSession = self.recentSessions[indexPath.row];
  86. [self onSelectedRecent:recentSession atIndexPath:indexPath];
  87. }
  88. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  89. return 74.f;
  90. }
  91. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
  92. return YES;
  93. }
  94. #pragma mark - UITableViewDataSource
  95. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  96. return 1;
  97. }
  98. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  99. return self.recentSessions.count;
  100. }
  101. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  102. static NSString * cellId = @"cellId";
  103. YOUPAIBBNIMSessionListCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
  104. if (!cell) {
  105. cell = [[YOUPAIBBNIMSessionListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
  106. }
  107. [cell.youpaipavatarImageView addTarget:self action:@selector(onTouchAvatar:) forControlEvents:UIControlEventTouchUpInside];
  108. NIMRecentSession *recent = self.recentSessions[indexPath.row];
  109. cell.youpaipnameLabel.text = [self nameForRecentSession:recent];
  110. [cell.youpaipavatarImageView setAvatarBySession:recent.session];
  111. [cell.youpaipnameLabel sizeToFit];
  112. cell.youpaipmessageLabel.attributedText = [self contentForRecentSession:recent];
  113. [cell.youpaipmessageLabel sizeToFit];
  114. cell.youpaiptimeLabel.text = [self timestampDescriptionForRecentSession:recent];
  115. [cell.youpaiptimeLabel sizeToFit];
  116. [cell youpaifrefresh:recent];
  117. // NSString *ext = [self getExtBySession:recent.session];
  118. // cell.vipV.hidden = YES;
  119. // cell.anchorTagImgV.hidden = YES;
  120. // if (ext.length != 0) {
  121. // NSData *data = [ext dataUsingEncoding:NSUTF8StringEncoding];
  122. // NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
  123. // if ([dict.allKeys containsObject:@"vip"]) {
  124. // NSInteger vip = [[dict valueForKey:@"vip"] integerValue];
  125. //
  126. //
  127. //
  128. // NSDictionary *vipInfo = [LCSaveData getNobleInfo];
  129. // NSDictionary *vipItemInfo = [vipInfo objectForKey:[NSString stringWithFormat:@"%@",@(vip)]];
  130. // if (vipItemInfo != nil) {
  131. // cell.vipV.hidden = NO;
  132. // [cell.vipV sd_setImageWithURL:[LCTools getImageUrlWithAddress:[vipItemInfo objectForKey:@"mini_icon"]]];
  133. // [cell.vipV mas_remakeConstraints:^(MASConstraintMaker *make) {
  134. // make.left.equalTo(cell.nameLabel.mas_right).offset(5.0f);
  135. // make.centerY.equalTo(cell.nameLabel);
  136. // make.size.mas_offset(CGSizeMake(30.0f, 19.0f));
  137. // }];
  138. // }
  139. //
  140. // }
  141. // if ([dict.allKeys containsObject:@"is_live"]) {
  142. // NSInteger is_live = [[dict valueForKey:@"is_live"] integerValue];
  143. // if (is_live == 1) {
  144. // cell.anchorTagImgV.hidden = NO;
  145. // }
  146. // }
  147. // }
  148. [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
  149. //v1.5.6 暗黑模式
  150. // [cell.contentView setBackgroundColor:LCWhiteColor];
  151. cell.youpaipavatarImageView.contentMode = UIViewContentModeScaleAspectFill;
  152. return cell;
  153. }
  154. #pragma mark - NIMConversationManagerDelegate
  155. - (void)didAddRecentSession:(NIMRecentSession *)recentSession
  156. totalUnreadCount:(NSInteger)totalUnreadCount{
  157. [self.recentSessions addObject:recentSession];
  158. [self sort];
  159. _recentSessions = [self customSortRecents:_recentSessions];
  160. [self refresh];
  161. }
  162. - (void)didUpdateRecentSession:(NIMRecentSession *)recentSession
  163. totalUnreadCount:(NSInteger)totalUnreadCount{
  164. for (NIMRecentSession *recent in self.recentSessions)
  165. {
  166. if ([recentSession.session.sessionId isEqualToString:recent.session.sessionId])
  167. {
  168. [self.recentSessions removeObject:recent];
  169. break;
  170. }
  171. }
  172. NSInteger insert = [self findInsertPlace:recentSession];
  173. [self.recentSessions insertObject:recentSession atIndex:insert];
  174. _recentSessions = [self customSortRecents:_recentSessions];
  175. [self refresh];
  176. }
  177. - (void)didRemoveRecentSession:(NIMRecentSession *)recentSession
  178. totalUnreadCount:(NSInteger)totalUnreadCount
  179. {
  180. //清理本地数据
  181. NSInteger index = [self.recentSessions indexOfObject:recentSession];
  182. [self.recentSessions removeObjectAtIndex:index];
  183. //如果删除本地会话后就不允许漫游当前会话,则需要进行一次删除服务器会话的操作
  184. if (self.autoRemoveRemoteSession)
  185. {
  186. [[NIMSDK sharedSDK].conversationManager deleteRemoteSessions:@[recentSession.session]
  187. completion:nil];
  188. }
  189. _recentSessions = [self customSortRecents:_recentSessions];
  190. [self refresh];
  191. }
  192. - (void)messagesDeletedInSession:(NIMSession *)session{
  193. _recentSessions = [[NIMSDK sharedSDK].conversationManager.allRecentSessions mutableCopy];
  194. _recentSessions = [self customSortRecents:_recentSessions];
  195. [self refresh];
  196. }
  197. - (void)allMessagesDeleted{
  198. _recentSessions = [[NIMSDK sharedSDK].conversationManager.allRecentSessions mutableCopy];
  199. _recentSessions = [self customSortRecents:_recentSessions];
  200. [self refresh];
  201. }
  202. - (void)allMessagesRead
  203. {
  204. _recentSessions = [[NIMSDK sharedSDK].conversationManager.allRecentSessions mutableCopy];
  205. _recentSessions = [self customSortRecents:_recentSessions];
  206. [self refresh];
  207. }
  208. - (NSMutableArray *)customSortRecents:(NSMutableArray *)recentSessions
  209. {
  210. return self.recentSessions;
  211. }
  212. #pragma mark - NIMLoginManagerDelegate
  213. - (void)onLogin:(NIMLoginStep)step
  214. {
  215. if (step == NIMLoginStepSyncOK) {
  216. [self refresh];
  217. }
  218. }
  219. #pragma mark - Override
  220. - (void)onSelectedAvatar:(NSString *)userId
  221. atIndexPath:(NSIndexPath *)indexPath{};
  222. - (void)onSelectedRecent:(NIMRecentSession *)recentSession atIndexPath:(NSIndexPath *)indexPath{
  223. YOUPAIBBNIMSessionViewController *vc = [[YOUPAIBBNIMSessionViewController alloc] initWithSession:recentSession.session];
  224. [self.navigationController pushViewController:vc animated:YES];
  225. }
  226. - (NSString *)getExtBySession:(NIMSession *)session{
  227. NIMUser *nimUser = [[[NIMSDK sharedSDK] userManager] userInfo:session.sessionId];
  228. return nimUser.userInfo.ext;
  229. }
  230. - (NSString *)nameForRecentSession:(NIMRecentSession *)recent{
  231. if (recent.session.sessionType == NIMSessionTypeP2P) {
  232. return [NIMKitUtil showNick:recent.session.sessionId inSession:recent.session];
  233. }else{
  234. NIMTeam *team = [[NIMSDK sharedSDK].teamManager teamById:recent.session.sessionId];
  235. return team.teamName;
  236. }
  237. }
  238. - (NSAttributedString *)contentForRecentSession:(NIMRecentSession *)recent{
  239. NSString *content = [self messageContent:recent.lastMessage];
  240. return [[NSAttributedString alloc] initWithString:content ?: @""];
  241. }
  242. - (NSString *)timestampDescriptionForRecentSession:(NIMRecentSession *)recent{
  243. return [NIMKitUtil showTime:recent.lastMessage.timestamp showDetail:NO];
  244. }
  245. #pragma mark - Misc
  246. - (NSInteger)findInsertPlace:(NIMRecentSession *)recentSession{
  247. __block NSUInteger matchIdx = 0;
  248. __block BOOL find = NO;
  249. [self.recentSessions enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  250. NIMRecentSession *item = obj;
  251. if (item.lastMessage.timestamp <= recentSession.lastMessage.timestamp) {
  252. *stop = YES;
  253. find = YES;
  254. matchIdx = idx;
  255. }
  256. }];
  257. if (find) {
  258. return matchIdx;
  259. }else{
  260. return self.recentSessions.count;
  261. }
  262. }
  263. - (void)sort{
  264. [self.recentSessions sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
  265. NIMRecentSession *item1 = obj1;
  266. NIMRecentSession *item2 = obj2;
  267. if (item1.lastMessage.timestamp < item2.lastMessage.timestamp) {
  268. return NSOrderedDescending;
  269. }
  270. if (item1.lastMessage.timestamp > item2.lastMessage.timestamp) {
  271. return NSOrderedAscending;
  272. }
  273. return NSOrderedSame;
  274. }];
  275. }
  276. - (void)onTouchAvatar:(id)sender{
  277. UIView *view = [sender superview];
  278. while (![view isKindOfClass:[UITableViewCell class]]) {
  279. view = view.superview;
  280. }
  281. UITableViewCell *cell = (UITableViewCell *)view;
  282. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  283. NIMRecentSession *recent = self.recentSessions[indexPath.row];
  284. [self onSelectedAvatar:recent atIndexPath:indexPath];
  285. }
  286. #pragma mark - Private
  287. - (NSString *)messageContent:(NIMMessage*)lastMessage{
  288. NSString *text = @"";
  289. switch (lastMessage.messageType) {
  290. case NIMMessageTypeText:
  291. text = lastMessage.text;
  292. break;
  293. case NIMMessageTypeAudio:
  294. text = @"[语音]";
  295. break;
  296. case NIMMessageTypeImage:
  297. text = @"[图片]";
  298. break;
  299. case NIMMessageTypeVideo:
  300. text = @"[视频]";
  301. break;
  302. case NIMMessageTypeLocation:
  303. text = @"[位置]";
  304. break;
  305. case NIMMessageTypeNotification:{
  306. return [self notificationMessageContent:lastMessage];
  307. }
  308. case NIMMessageTypeFile:
  309. text = @"[文件]";
  310. break;
  311. case NIMMessageTypeTip:
  312. text = lastMessage.text;
  313. break;
  314. case NIMMessageTypeRobot:
  315. text = [self robotMessageContent:lastMessage];
  316. break;
  317. default:
  318. text = @"[未知消息]";
  319. }
  320. if (lastMessage.session.sessionType == NIMSessionTypeP2P || lastMessage.messageType == NIMMessageTypeTip)
  321. {
  322. return text;
  323. }
  324. else
  325. {
  326. NSString *from = lastMessage.from;
  327. if (lastMessage.messageType == NIMMessageTypeRobot)
  328. {
  329. NIMRobotObject *object = (NIMRobotObject *)lastMessage.messageObject;
  330. if (object.isFromRobot)
  331. {
  332. from = object.robotId;
  333. }
  334. }
  335. NSString *nickName = [NIMKitUtil showNick:from inSession:lastMessage.session];
  336. return nickName.length ? [nickName stringByAppendingFormat:@" : %@",text] : @"";
  337. }
  338. }
  339. - (NSString *)notificationMessageContent:(NIMMessage *)lastMessage{
  340. NIMNotificationObject *object = lastMessage.messageObject;
  341. if (object.notificationType == NIMNotificationTypeNetCall) {
  342. NIMNetCallNotificationContent *content = (NIMNetCallNotificationContent *)object.content;
  343. if (content.callType == NIMNetCallTypeAudio) {
  344. return @"[网络通话]";
  345. }
  346. return @"[视频聊天]";
  347. }
  348. if (object.notificationType == NIMNotificationTypeTeam) {
  349. NIMTeam *team = [[NIMSDK sharedSDK].teamManager teamById:lastMessage.session.sessionId];
  350. if (team.type == NIMTeamTypeNormal) {
  351. return @"[讨论组信息更新]";
  352. }else{
  353. return @"[群信息更新]";
  354. }
  355. }
  356. return @"[未知消息]";
  357. }
  358. - (NSString *)robotMessageContent:(NIMMessage *)lastMessage{
  359. NIMRobotObject *object = lastMessage.messageObject;
  360. if (object.isFromRobot)
  361. {
  362. return @"[机器人消息]";
  363. }
  364. else
  365. {
  366. return lastMessage.text;
  367. }
  368. }
  369. #pragma mark - Notification
  370. - (void)onUserInfoHasUpdatedNotification:(NSNotification *)notification{
  371. [self refresh];
  372. }
  373. - (void)onTeamInfoHasUpdatedNotification:(NSNotification *)notification{
  374. [self refresh];
  375. }
  376. - (void)onTeamMembersHasUpdatedNotification:(NSNotification *)notification{
  377. [self refresh];
  378. }
  379. @end