|
|
@@ -10,17 +10,16 @@ import Foundation
|
|
|
|
|
|
protocol LNIMManagerNotify {
|
|
|
func onConversationListChanged()
|
|
|
- func onIMUserStatusChanged(uid: String, status: V2TIMUserStatusType)
|
|
|
}
|
|
|
extension LNIMManagerNotify {
|
|
|
func onConversationListChanged() {}
|
|
|
- func onIMUserStatusChanged(uid: String, status: V2TIMUserStatusType) {}
|
|
|
}
|
|
|
|
|
|
|
|
|
extension String {
|
|
|
var isImOfficialId: Bool {
|
|
|
- (Int(self) ?? -1) <= (Int(LNIMManager.officialId) ?? 0)
|
|
|
+ guard let intValue = Int(self) else { return false }
|
|
|
+ return intValue <= LNIMManager.maxOfficialId
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -30,15 +29,20 @@ enum LNIMCustomErrorCode: Int {
|
|
|
case userNotExist = 120002
|
|
|
}
|
|
|
|
|
|
+enum LNIMOfficialIds: String {
|
|
|
+ case officialMessage = "10000"
|
|
|
+}
|
|
|
+
|
|
|
|
|
|
class LNIMManager: NSObject {
|
|
|
private static let appId: Int32 = 20030346
|
|
|
|
|
|
static var shared = LNIMManager()
|
|
|
- static let officialId = "10000"
|
|
|
+
|
|
|
+ static let maxOfficialId = 10000
|
|
|
|
|
|
private(set) var conversationList: [V2TIMConversation] = []
|
|
|
- private(set) var userStatus: [String: V2TIMUserStatusType] = [:]
|
|
|
+ private(set) var userOnlineMap: [String: Bool] = [:]
|
|
|
|
|
|
private override init() {
|
|
|
super.init()
|
|
|
@@ -63,17 +67,16 @@ extension LNIMManager {
|
|
|
|
|
|
if list.firstIndex(where: { $0.userID?.isImOfficialId == true }) == nil {
|
|
|
// 插入官方消息
|
|
|
- V2TIMManager.sharedInstance().setConversationDraft(conversationID: "c2c_" + Self.officialId, draftText: " ") {
|
|
|
- V2TIMManager.sharedInstance().setConversationDraft(conversationID: "c2c_" + Self.officialId, draftText: nil, succ: nil)
|
|
|
+ let officialId = "c2c_" + LNIMOfficialIds.officialMessage.rawValue
|
|
|
+ V2TIMManager.sharedInstance().setConversationDraft(conversationID: officialId, draftText: " ") {
|
|
|
+ V2TIMManager.sharedInstance().setConversationDraft(conversationID: officialId, draftText: nil, succ: nil)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
conversationList = list
|
|
|
handler?(true)
|
|
|
notifyConversationListChanged()
|
|
|
-
|
|
|
- V2TIMManager.sharedInstance().subscribeUserStatus(userIDList: list.compactMap({ $0.userID }), succ: nil)
|
|
|
- loadUsersOnlineStatus()
|
|
|
+ getUserOnlineState()
|
|
|
} fail: { code, err in
|
|
|
handler?(false)
|
|
|
}
|
|
|
@@ -85,26 +88,20 @@ extension LNIMManager {
|
|
|
conversationID: conversationId,
|
|
|
cleanTimestamp: 0, cleanSequence: 0, succ: nil)
|
|
|
}
|
|
|
-}
|
|
|
-
|
|
|
-extension LNIMManager {
|
|
|
- func isUserOnline(uid: String) -> Bool {
|
|
|
- userStatus[uid] == .USER_STATUS_ONLINE
|
|
|
- }
|
|
|
|
|
|
- private func loadUsersOnlineStatus() {
|
|
|
+ func getUserOnlineState() {
|
|
|
let uids = conversationList.compactMap { $0.userID }
|
|
|
- guard !uids.isEmpty else { return }
|
|
|
- V2TIMManager.sharedInstance().getUserStatus(userIDList: uids)
|
|
|
- { [weak self] userStatusList in
|
|
|
+ if uids.isEmpty { return }
|
|
|
+ LNProfileManager.shared.getUserOnlineState(uids: uids) { [weak self] map in
|
|
|
guard let self else { return }
|
|
|
- userStatusList?.forEach {
|
|
|
- if let uid = $0.userID {
|
|
|
- self.userStatus[uid] = $0.statusType
|
|
|
- self.notifyUserStatusChanged(uid: uid, status: $0.statusType)
|
|
|
- }
|
|
|
- }
|
|
|
- } fail: { _, _ in }
|
|
|
+ userOnlineMap = map
|
|
|
+ notifyConversationListChanged()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func isUserOnline(uid: String?) -> Bool {
|
|
|
+ guard let uid else { return false }
|
|
|
+ return userOnlineMap[uid] == true
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -124,12 +121,6 @@ extension LNIMManager: V2TIMConversationListener {
|
|
|
|
|
|
extension LNIMManager: V2TIMSDKListener {
|
|
|
func onUserStatusChanged(userStatusList: [V2TIMUserStatus]!) {
|
|
|
- userStatusList.forEach {
|
|
|
- if let uid = $0.userID {
|
|
|
- userStatus[uid] = $0.statusType
|
|
|
- notifyUserStatusChanged(uid: uid, status: $0.statusType)
|
|
|
- }
|
|
|
- }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -160,7 +151,6 @@ extension LNIMManager: LNAccountManagerNotify {
|
|
|
|
|
|
func onUserLogout() {
|
|
|
V2TIMManager.sharedInstance().logout(succ: nil)
|
|
|
- userStatus.removeAll()
|
|
|
|
|
|
Self.shared = LNIMManager()
|
|
|
}
|
|
|
@@ -180,10 +170,4 @@ extension LNIMManager {
|
|
|
($0 as? LNIMManagerNotify)?.onConversationListChanged()
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- private func notifyUserStatusChanged(uid: String, status: V2TIMUserStatusType) {
|
|
|
- LNEventDeliver.notifyEvent {
|
|
|
- ($0 as? LNIMManagerNotify)?.onIMUserStatusChanged(uid: uid, status: status)
|
|
|
- }
|
|
|
- }
|
|
|
}
|