Files
yingmeng-ios-switf/yinmeng-ios/Modules/Chat/ChatListVC.swift

211 lines
5.7 KiB
Swift
Raw Normal View History

2024-02-27 23:34:55 +08:00
//
// ChatListVC.swift
// yinmeng-ios
//
// Created by MaiMang on 2024/2/27.
//
import UIKit
2024-02-29 23:49:12 +08:00
import NIMSDK
2024-03-01 01:27:25 +08:00
class ChatListVC: BaseViewController, HiddenNavigationBarProtocol {
2024-02-27 23:34:55 +08:00
2024-02-29 23:49:12 +08:00
var list:NSMutableArray?
deinit {
NIMSDK.shared().loginManager.remove(self)
NIMSDK.shared().conversationManager.remove(self)
}
2024-02-27 23:34:55 +08:00
override func viewDidLoad() {
super.viewDidLoad()
2024-02-29 23:49:12 +08:00
NIMSDK.shared().conversationManager.add(self)
NIMSDK.shared().loginManager.add(self)
getList()
2024-03-01 01:27:25 +08:00
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)
}
2024-02-27 23:34:55 +08:00
}
2024-02-29 23:49:12 +08:00
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
}()
2024-03-01 01:27:25 +08:00
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()
}
2024-02-29 23:49:12 +08:00
}
extension ChatListVC: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(for: indexPath, cellType: ChatListCell.self)
2024-03-01 01:27:25 +08:00
if indexPath.row < list?.count ?? 0 {
let res = list?.object(at: indexPath.row) as? NIMRecentSession
cell.rescent = res
}
2024-02-29 23:49:12 +08:00
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return list?.count ?? 0
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
2024-03-01 01:27:25 +08:00
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)
}
}
2024-02-29 23:49:12 +08:00
}
}
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)"
}
2024-02-27 23:34:55 +08:00
}