Files
yingmeng-ios-switf/yinmeng-ios/Modules/Chat/ChatListVC.swift
2024-02-29 23:49:12 +08:00

142 lines
3.6 KiB
Swift

//
// ChatListVC.swift
// yinmeng-ios
//
// Created by MaiMang on 2024/2/27.
//
import UIKit
import NIMSDK
class ChatListVC: BaseViewController {
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()
}
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
}()
}
extension ChatListVC: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(for: indexPath, cellType: ChatListCell.self)
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return list?.count ?? 0
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 64
}
}
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)"
}
}