feat: Enhance logging and state management in Protocol and MessageRepository; improve dialog read status handling in ChatViewModel

This commit is contained in:
k1ngsterr1
2026-01-16 17:11:50 +05:00
parent 6386164ae7
commit 6d506e681b
4 changed files with 132 additions and 41 deletions

View File

@@ -310,13 +310,17 @@ class MessageRepository private constructor(private val context: Context) {
// ✅ Проверяем существование перед вставкой (защита от дубликатов)
val stillExists = messageDao.messageExists(account, messageId)
android.util.Log.d("MessageRepo", "📥 INCOMING: messageId=${messageId.take(16)}..., stillExists=$stillExists")
if (!stillExists) {
// Сохраняем в БД только если сообщения нет
messageDao.insertMessage(entity)
android.util.Log.d("MessageRepo", "📥 INSERTED message with read=0, fromMe=0")
}
// Обновляем диалог
// Обновляем диалог ПОСЛЕ вставки сообщения
updateDialog(packet.fromPublicKey, plainText, packet.timestamp, incrementUnread = true)
android.util.Log.d("MessageRepo", "📥 Dialog updated")
// 🔥 Запрашиваем информацию о пользователе для отображения имени вместо ключа
requestUserInfo(packet.fromPublicKey)
@@ -469,21 +473,25 @@ class MessageRepository private constructor(private val context: Context) {
val account = currentAccount ?: return
val privateKey = currentPrivateKey ?: return
android.util.Log.d("MessageRepo", "📊 updateDialog: opponent=${opponentKey.take(16)}..., incrementUnread=$incrementUnread")
try {
// 🔥 КРИТИЧНО: Сначала считаем реальное количество непрочитанных из messages
val unreadCount = messageDao.getUnreadCountForDialog(account, opponentKey)
android.util.Log.d("MessageRepo", "📊 unreadCount from DB: $unreadCount")
// 🔒 Шифруем lastMessage
val encryptedLastMessage = CryptoManager.encryptWithPassword(lastMessage, privateKey)
// Проверяем существует ли диалог
val existing = dialogDao.getDialog(account, opponentKey)
android.util.Log.d("MessageRepo", "📊 existing dialog: ${existing != null}, currentUnread=${existing?.unreadCount}")
if (existing != null) {
// Обновляем существующий диалог
dialogDao.updateLastMessage(account, opponentKey, encryptedLastMessage, timestamp)
dialogDao.updateUnreadCount(account, opponentKey, unreadCount)
android.util.Log.d("MessageRepo", "📊 UPDATED dialog unread to: $unreadCount")
} else {
// Создаем новый диалог
dialogDao.insertDialog(DialogEntity(
@@ -493,9 +501,12 @@ class MessageRepository private constructor(private val context: Context) {
lastMessageTimestamp = timestamp,
unreadCount = unreadCount
))
android.util.Log.d("MessageRepo", "📊 CREATED new dialog with unread: $unreadCount")
}
} catch (e: Exception) {
android.util.Log.e("MessageRepo", "📊 ERROR in updateDialog: ${e.message}")
e.printStackTrace()
}
}