// // ChatListVC.swift // yinmeng-ios // // Created by yinmeng on 2024/2/27. // 回话列表 import UIKit import NIMSDK class ChatListVC: BaseViewController, HiddenNavigationBarProtocol { var list:NSMutableArray? deinit { NIMSDK.shared().loginManager.remove(self) NIMSDK.shared().conversationManager.remove(self) } override func viewDidLoad() { super.viewDidLoad() NIMSDK.shared().conversationManager.add(self) NIMSDK.shared().loginManager.add(self) getList() view.addSubview(backImgView) view.addSubview(tableView) view.addSubview(clearBtn) view.addSubview(titleLb) backImgView.snp.makeConstraints { make in make.edges.equalTo(view) } tableView.snp.makeConstraints { make in make.left.right.bottom.equalToSuperview() make.top.equalToSuperview().offset(NavHeight) } clearBtn.snp.makeConstraints { make in make.size.equalTo(CGSize(width: 28, height: 28)) make.right.equalToSuperview().offset(-16) make.top.equalToSuperview().offset(StatusBarHeight + 8) } titleLb.snp.makeConstraints { make in make.centerY.equalTo(clearBtn) make.left.equalToSuperview().offset(16) } } private lazy var tableView: UITableView = { let tableView = UITableView(frame: .zero, style: .plain) tableView.delegate = self tableView.dataSource = self tableView.tableFooterView = UIView() tableView.separatorStyle = .none tableView.backgroundColor = .clear if #available(iOS 11.0, *) { tableView.contentInsetAdjustmentBehavior = .never } tableView.register(cellType: ChatListCell.self) return tableView }() private lazy var backImgView: UIImageView = { let imageView = UIImageView() imageView.image = UIImage(named: "auth_login_bg") imageView.isUserInteractionEnabled = true imageView.layer.masksToBounds = true imageView.contentMode = .scaleAspectFill return imageView }() private lazy var clearBtn: UIButton = { let button = UIButton(type: .custom) button.setImage(UIImage(named: "chat_clear"), for: .normal) button.setImage(UIImage(named: "chat_clear"), for: .selected) button.addTarget(self, action: #selector(clearClick), for: .touchUpInside) return button }() private lazy var titleLb: UILabel = { let label = UILabel() label.textColor = .white label.text = "消息中心" label.font = UIFont.systemFont(ofSize: 18, weight: .medium) return label }() @objc func clearClick() { NIMSDK.shared().conversationManager.markAllMessagesRead() } } extension ChatListVC: UITableViewDelegate, UITableViewDataSource { func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(for: indexPath, cellType: ChatListCell.self) if indexPath.row < list?.count ?? 0 { let res = list?.object(at: indexPath.row) as? NIMRecentSession cell.rescent = res } return cell } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return list?.count ?? 0 } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 82 } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tableView.deselectRow(at: indexPath, animated: true) if indexPath.row < list?.count ?? 0 { let res = list?.object(at: indexPath.row) as? NIMRecentSession if let session = res?.session { let chat = ChatVC(session: session) self.navigationController?.pushViewController(chat, animated: true) } } } } extension ChatListVC: NIMConversationManagerDelegate, NIMLoginManagerDelegate { func onLogin(_ step: NIMLoginStep) { if (step == .syncOK) { getList() } } func didLoadAllRecentSessionCompletion() { getList() } func didAdd(_ recentSession: NIMRecentSession, totalUnreadCount: Int) { var isInList = false for index in 0..<(list?.count ?? 0) { let recent = list?[index] as? NIMRecentSession if recent?.session?.sessionId == recentSession.session?.sessionId{ isInList = true list?.replaceObject(at: index, with: recentSession) break } } if isInList == false { list?.add(recentSession) } mai_sortMessages() updateItemBadge() } func didUpdate(_ recentSession: NIMRecentSession, totalUnreadCount: Int) { var isInList = false for index in 0..<(list?.count ?? 0) { let session = list?[index] as? NIMRecentSession if session?.session?.sessionId == recentSession.session?.sessionId{ isInList = true list?.replaceObject(at: index, with: recentSession) break } } if isInList == false { list?.add(recentSession) } mai_sortMessages() updateItemBadge() } func didRemove(_ recentSession: NIMRecentSession, totalUnreadCount: Int) { let arr = NSArray.init(object: recentSession.session as Any) NIMSDK.shared().conversationManager.deleteRemoteSessions(arr as! [NIMSession], completion: nil) tableView.reloadData() } } extension ChatListVC { fileprivate func getList() { if let array = NIMSDK.shared().conversationManager.allRecentSessions() { list = NSMutableArray.init(array: array) mai_sortMessages() } } fileprivate func mai_sortMessages() -> Void { var arr = list?.sortedArray(options: NSSortOptions.stable, usingComparator: { obj1, obj2 in let item1 = obj1 as? NIMRecentSession let item2 = obj2 as? NIMRecentSession let time1 = (item1?.lastMessage?.timestamp ?? 0) as Double let time2 = (item2?.lastMessage?.timestamp ?? 0) as Double if time1 < time2{ return .orderedDescending } return .orderedAscending }) if let array = arr { list = NSMutableArray(array: array) } tableView.reloadData() } private func updateItemBadge() { let unreadCount = NIMSDK.shared().conversationManager.allUnreadCount() self.tabBarItem.badgeValue = "\(unreadCount)" } }