100 lines
3.2 KiB
Swift
100 lines
3.2 KiB
Swift
//
|
|
// GroupIMService.swift
|
|
// QuickLocation
|
|
//
|
|
// Created by 八条 on 2026/6/4.
|
|
//
|
|
|
|
import Foundation
|
|
import OpenIMSDK
|
|
|
|
final class GroupIMService {
|
|
|
|
static let shared = GroupIMService()
|
|
|
|
private var isLogined = false
|
|
|
|
private init() {}
|
|
|
|
// MARK: - Init SDK
|
|
func initSDK() {
|
|
let config = OIMInitConfig()
|
|
config.apiAddr = URLManager.shared.openIM_API
|
|
config.wsAddr = URLManager.shared.openIM_WS
|
|
config.platform = .iPhone
|
|
OIMManager.manager.initSDK(with: config,
|
|
onConnecting: {},
|
|
onConnectFailure: { _, _ in },
|
|
onConnectSuccess: {},
|
|
onKickedOffline: {},
|
|
onUserTokenExpired: {},
|
|
onUserTokenInvalid: { _ in })
|
|
}
|
|
|
|
// MARK: - Login
|
|
func login(completion: @escaping (Bool) -> Void) {
|
|
guard !isLogined, let token = AppContextManager.shared.imToken else {
|
|
completion(true)
|
|
return
|
|
}
|
|
let userId = AppContextManager.shared.userId
|
|
guard !userId.isEmpty, !token.isEmpty else {
|
|
completion(false)
|
|
return
|
|
}
|
|
OIMManager.manager.login(userId, token: token) { [weak self] _ in
|
|
self?.isLogined = true
|
|
completion(true)
|
|
} onFailure: { code, msg in
|
|
print("OpenIM login failed: \(code) \(msg ?? "")")
|
|
completion(false)
|
|
}
|
|
}
|
|
|
|
// MARK: - Get Joined Groups
|
|
func getJoinedGroups(completion: @escaping ([OIMGroupInfo]) -> Void) {
|
|
print("GroupIMService: getJoinedGroups called, isLogined=\(isLogined)")
|
|
OIMManager.manager.getJoinedGroupListWith(onSuccess: { groups in
|
|
print("GroupIMService: getJoinedGroups success, count=\(groups?.count ?? 0)")
|
|
completion(groups ?? [])
|
|
}, onFailure: { code, msg in
|
|
print("GroupIMService: getJoinedGroups failed: \(code) \(msg ?? "")")
|
|
completion([])
|
|
})
|
|
}
|
|
|
|
// MARK: - Conversation List (for unread count & last msg time)
|
|
func getConversationList(completion: @escaping ([OIMConversationInfo]) -> Void) {
|
|
OIMManager.manager.getAllConversationListWith(onSuccess: { list in
|
|
completion(list ?? [])
|
|
}, onFailure: { code, msg in
|
|
completion([])
|
|
})
|
|
}
|
|
|
|
// MARK: - Conversation Listener
|
|
private var conversationListener: ConversationListenerProxy?
|
|
|
|
func setConversationListener(_ handler: @escaping ([OIMConversationInfo]) -> Void) {
|
|
conversationListener = ConversationListenerProxy(handler: handler)
|
|
OIMManager.callbacker.addConversationListener(listener: conversationListener!)
|
|
}
|
|
}
|
|
|
|
// MARK: - ConversationListenerProxy
|
|
private class ConversationListenerProxy: NSObject, OIMConversationListener {
|
|
private let handler: ([OIMConversationInfo]) -> Void
|
|
|
|
init(handler: @escaping ([OIMConversationInfo]) -> Void) {
|
|
self.handler = handler
|
|
}
|
|
|
|
func onConversationChanged(_ conversations: [OIMConversationInfo]) {
|
|
handler(conversations)
|
|
}
|
|
|
|
func onNewConversation(_ conversations: [OIMConversationInfo]) {
|
|
handler(conversations)
|
|
}
|
|
}
|