optimize: optimize chatList

This commit is contained in:
2026-02-08 07:07:43 +05:00
parent 162747ea35
commit 58b754d5ba
3 changed files with 76 additions and 46 deletions

View File

@@ -32,6 +32,20 @@ import com.rosetta.messenger.utils.AvatarFileManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
/**
* LRU-кэш декодированных Bitmap аватаров (макс. 100 записей, ~11 MB)
*/
private object AvatarBitmapCache {
private val cache = object : LinkedHashMap<String, Bitmap>(50, 0.75f, true) {
override fun removeEldestEntry(eldest: MutableMap.MutableEntry<String, Bitmap>?): Boolean {
return size > 100
}
}
fun get(key: String): Bitmap? = synchronized(cache) { cache[key] }
fun put(key: String, bitmap: Bitmap) = synchronized(cache) { cache[key] = bitmap }
}
/**
* Composable для отображения аватара пользователя
* Совместимо с desktop версией (AvatarProvider)
@@ -80,15 +94,20 @@ fun AvatarImage(
LaunchedEffect(avatarKey, avatars.isEmpty()) {
val currentAvatars = avatars
if (currentAvatars.isNotEmpty()) {
val newBitmap = withContext(Dispatchers.IO) {
AvatarFileManager.base64ToBitmap(currentAvatars.first().base64Data)
}
// Устанавливаем новый bitmap только если декодирование успешно
if (newBitmap != null) {
bitmap = newBitmap
val cacheKey = "${publicKey}_${avatarKey}"
val cachedBitmap = AvatarBitmapCache.get(cacheKey)
if (cachedBitmap != null) {
bitmap = cachedBitmap
} else {
val newBitmap = withContext(Dispatchers.IO) {
AvatarFileManager.base64ToBitmap(currentAvatars.first().base64Data)
}
if (newBitmap != null) {
AvatarBitmapCache.put(cacheKey, newBitmap)
bitmap = newBitmap
}
}
} else {
// 🔥 FIX: Если аватары удалены - сбрасываем bitmap чтобы показался placeholder
bitmap = null
}
}