Bläddra i källkod

fix: 修复杀进程后麦位信息会丢失的问题

陈文艺 1 vecka sedan
förälder
incheckning
e77e8b5dc7

+ 1 - 1
Lanu/Manager/GameMate/LNGameMateManager.swift

@@ -368,7 +368,7 @@ extension LNGameMateManager {
             if let err {
                 showToast(err.errorDesc)
             } else {
-                LNProfileManager.shared.reloadMyProfile()
+                LNProfileManager.shared.reloadMyProfileDetail()
             }
         }
     }

+ 103 - 8
Lanu/Manager/Profile/LNProfileManager.swift

@@ -27,6 +27,12 @@ var myVoiceBarInfo: LNUserVoiceStateVO {
     LNProfileManager.shared.myVoiceState
 }
 
+class LNProfileUserInfo {
+    var uid: String = ""
+    var name: String = ""
+    var avatar: String = ""
+}
+
 class LNProfileManager {
     static let shared = LNProfileManager()
     static let nameMaxInput = 25
@@ -37,6 +43,11 @@ class LNProfileManager {
     
     private var randomProfile: LNRandomProfileResponse?
     
+    private static let profileCacheLimit = 300
+    private var profileCache: [String: LNProfileUserInfo] = [:]
+    private var profileCacheOrder: [String] = []
+    private let profileCacheLock = NSLock()
+    
     private let captchaCoolDown = 60
     private var captchaRemain = 0
     private var captchaTimer: Timer?
@@ -50,8 +61,8 @@ class LNProfileManager {
 }
 
 extension LNProfileManager {
-    func reloadMyProfile() {
-        LNHttpManager.shared.getMyProfile { [weak self] res, err in
+    func reloadMyProfileDetail() {
+        LNHttpManager.shared.getMyProfileDetail { [weak self] res, err in
             guard let self else { return }
             guard err == nil, let res else { return }
             if let relation = res.userFollowStat {
@@ -62,7 +73,7 @@ extension LNProfileManager {
                 notifyUserVoiceBarInfoChanged()
             }
         }
-        getUserProfile(uid: myUid) { _ in }
+        getUserProfileDetail(uid: myUid) { _ in }
     }
     
     func modifyMyProfile(config: LNProfileUpdateConfig, queue: DispatchQueue = .main,
@@ -73,7 +84,7 @@ extension LNProfileManager {
             if let err {
                 showToast(err.errorDesc)
             } else {
-                reloadMyProfile()
+                reloadMyProfileDetail()
                 if config.interest != nil {
                     LNGameMateManager.shared.getGameTypeList { _ in }
                 }
@@ -94,7 +105,7 @@ extension LNProfileManager {
                 showToast(err.errorDesc)
             } else {
                 guard let self else { return }
-                reloadMyProfile()
+                reloadMyProfileDetail()
             }
         }
     }
@@ -108,14 +119,14 @@ extension LNProfileManager {
                 showToast(err.errorDesc)
             } else {
                 guard let self else { return }
-                reloadMyProfile()
+                reloadMyProfileDetail()
             }
         }
     }
 }
 
 extension LNProfileManager {
-    func getUserProfile(uid: String, queue: DispatchQueue = .main, handler: @escaping (LNUserProfileVO?) -> Void) {
+    func getUserProfileDetail(uid: String, queue: DispatchQueue = .main, handler: @escaping (LNUserProfileVO?) -> Void) {
         LNHttpManager.shared.getUsersInfo(uids: [uid]) { [weak self] res, err in
             guard let self else { return }
             if let res, err == nil {
@@ -125,6 +136,11 @@ extension LNProfileManager {
                             self.notifyUserInfoChanged(newInfo: $0)
                         }
                     } else {
+                        self.updateProfileCache(
+                            uid: $0.userNo,
+                            name: $0.nickname,
+                            avatar: $0.cover
+                        )
                         self.notifyUserInfoChanged(newInfo: $0)
                     }
                 }
@@ -164,6 +180,85 @@ extension LNProfileManager {
 //    }
 }
 
+extension LNProfileManager {
+    func getCachedProfileUserInfo(uid: String) -> LNProfileUserInfo? {
+        profileCacheLock.lock()
+        defer { profileCacheLock.unlock() }
+        guard let info = profileCache[uid] else {
+            return nil
+        }
+        touchProfileCacheKey(uid)
+        return info
+    }
+    
+    func getCachedProfileUserInfo(uid: String,
+                                  fetchIfNeeded: Bool = true,
+                                  queue: DispatchQueue = .main,
+                                  handler: @escaping (LNProfileUserInfo?) -> Void) {
+        if let info = getCachedProfileUserInfo(uid: uid) {
+            queue.asyncIfNotGlobal {
+                handler(info)
+            }
+            return
+        }
+        
+        guard fetchIfNeeded else {
+            queue.asyncIfNotGlobal {
+                handler(nil)
+            }
+            return
+        }
+        
+        getUserProfileDetail(uid: uid, queue: queue) { [weak self] profile in
+            guard let self, let profile else {
+                handler(nil)
+                return
+            }
+            
+            self.updateProfileCache(
+                uid: profile.userNo,
+                name: profile.nickname,
+                avatar: profile.cover
+            )
+            handler(self.getCachedProfileUserInfo(uid: uid))
+        }
+    }
+    
+    func updateProfileCache(uid: String,
+                            name: String,
+                            avatar: String) {
+        guard !uid.isEmpty else {
+            return
+        }
+        
+        profileCacheLock.lock()
+        defer { profileCacheLock.unlock() }
+        
+        let info = profileCache[uid] ?? LNProfileUserInfo()
+        info.uid = uid
+        info.name = name
+        info.avatar = avatar
+        
+        profileCache[uid] = info
+        touchProfileCacheKey(uid)
+        trimProfileCacheIfNeeded()
+    }
+    
+    private func touchProfileCacheKey(_ uid: String) {
+        if let index = profileCacheOrder.firstIndex(of: uid) {
+            profileCacheOrder.remove(at: index)
+        }
+        profileCacheOrder.append(uid)
+    }
+    
+    private func trimProfileCacheIfNeeded() {
+        while profileCacheOrder.count > Self.profileCacheLimit {
+            let expiredUid = profileCacheOrder.removeFirst()
+            profileCache.removeValue(forKey: expiredUid)
+        }
+    }
+}
+
 extension LNProfileManager {
     func getBindPhoneCaptcha(code: String, phone: String,
                              queue: DispatchQueue = .main,
@@ -234,7 +329,7 @@ extension LNProfileManager {
 
 extension LNProfileManager: LNAccountManagerNotify {
     func onUserLogin() {
-        reloadMyProfile()
+        reloadMyProfileDetail()
     }
     
     func onUserLogout() {

+ 1 - 1
Lanu/Manager/Profile/Network/LNHttpManager+Profile.swift

@@ -35,7 +35,7 @@ class LNProfileUpdateConfig {
 }
 
 extension LNHttpManager {
-    func getMyProfile(completion: @escaping (LNMyProfileResponseVO?, LNHttpError?) -> Void) {
+    func getMyProfileDetail(completion: @escaping (LNMyProfileResponseVO?, LNHttpError?) -> Void) {
         post(path: kNetPath_Profile_MyInfo, completion: completion)
     }
     

+ 1 - 1
Lanu/Manager/Relation/LNRelationManager.swift

@@ -60,7 +60,7 @@ class LNRelationManager {
     func reloadRelationInfoIfNeed() {
         guard relationIsChanged else { return }
         
-        LNProfileManager.shared.reloadMyProfile()
+        LNProfileManager.shared.reloadMyProfileDetail()
         relationIsChanged = false
     }
     

+ 1 - 1
Lanu/Views/IM/Chat/ViewModel/LNIMChatViewModel.swift

@@ -109,7 +109,7 @@ extension LNIMChatViewModel {
     }
     
     private func loadUserInfo() {
-        LNProfileManager.shared.getUserProfile(uid: userId) { [weak self] info in
+        LNProfileManager.shared.getUserProfileDetail(uid: userId) { [weak self] info in
             guard let self else { return }
             guard let info else { return }
             userInfo = info

+ 1 - 1
Lanu/Views/IM/Chat/VoiceCall/LNAudioCallPanel.swift

@@ -80,7 +80,7 @@ class LNAudioCallPanel: LNPopupView {
 
 extension LNAudioCallPanel {
     private func reloadUserInfo(uid: String) {
-        LNProfileManager.shared.getUserProfile(uid: uid) { [weak self] info in
+        LNProfileManager.shared.getUserProfileDetail(uid: uid) { [weak self] info in
             guard let self else { return }
             guard let info else { return }
             background.showAvatar(info.avatar)

+ 1 - 1
Lanu/Views/IM/ConversationList/LNIMConversationCell.swift

@@ -65,7 +65,7 @@ class LNIMConversationCell: UITableViewCell {
         if let userInfo = item.extraInfo?.userInfo {
             avatar.showAvatar(userInfo.avatar)
         } else if let userId = item.userID, !userId.isImOfficialId {
-            LNProfileManager.shared.getUserProfile(uid: userId) { [weak self] info in
+            LNProfileManager.shared.getUserProfileDetail(uid: userId) { [weak self] info in
                 guard let self else { return }
                 guard let info else { return }
                 item.extraInfo?.userInfo = info

+ 1 - 1
Lanu/Views/Profile/Mine/LNMineViewController.swift

@@ -36,7 +36,7 @@ class LNMineViewController: UIViewController {
         super.viewWillAppear(animated)
         
         LNRelationManager.shared.reloadRelationInfoIfNeed()
-        LNProfileManager.shared.reloadMyProfile()
+        LNProfileManager.shared.reloadMyProfileDetail()
         LNPurchaseManager.shared.reloadWalletInfo()
         
         if myUserInfo.playmate {

+ 1 - 1
Lanu/Views/Profile/Profile/LNProfileViewController.swift

@@ -61,7 +61,7 @@ class LNProfileViewController: LNViewController {
         
         LNStatisticManager.shared.reportVisitor(uid: uid) { _ in }
         LNStatisticManager.shared.reportViewProfile(uid: uid)
-        LNProfileManager.shared.getUserProfile(uid: uid) { [weak self] info in
+        LNProfileManager.shared.getUserProfileDetail(uid: uid) { [weak self] info in
             guard let self else { return }
             guard let info else { return }
             

+ 1 - 1
Lanu/Views/Room/Profile/LNRoomProfileCardPanel.swift

@@ -35,7 +35,7 @@ class LNRoomProfileCardPanel: LNPopupView {
         skillSection.isHidden = true
         actionsSection.update(uid)
         
-        LNProfileManager.shared.getUserProfile(uid: uid) { [weak self] detail in
+        LNProfileManager.shared.getUserProfileDetail(uid: uid) { [weak self] detail in
             guard let self else { return }
             guard let detail else {
                 dismiss()

+ 10 - 2
Lanu/Views/Room/Seats/LNRoomUserSeatView.swift

@@ -213,9 +213,17 @@ extension LNRoomUserSeatView: LNRoomViewModelNotify {
             nameLabel.text = seatNum.title
         } else {
             userView.isHidden = false
-            userAvatar.sd_setImage(with: URL(string: curSeat.avatar))
             
-            nameLabel.text = curSeat.nickname
+            if !curSeat.nickname.isEmpty {
+                userAvatar.sd_setImage(with: URL(string: curSeat.avatar))
+                nameLabel.text = curSeat.nickname
+            } else {
+                LNProfileManager.shared.getCachedProfileUserInfo(uid: curSeat.uid) { [weak self] info in
+                    guard let self, let info, info.uid == curSeat.uid else { return }
+                    userAvatar.sd_setImage(with: URL(string: info.avatar))
+                    nameLabel.text = info.name
+                }
+            }
         }
         
         muteIc.isHidden = !curSeat.isMute