feat: Enhance message status tracking and logging in MessageRepository and ChatsListViewModel for improved clarity and debugging
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.rosetta.messenger.data
|
||||
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import com.rosetta.messenger.crypto.CryptoManager
|
||||
import com.rosetta.messenger.crypto.MessageCrypto
|
||||
import com.rosetta.messenger.database.*
|
||||
@@ -224,10 +225,17 @@ class MessageRepository private constructor(private val context: Context) {
|
||||
dialogKey = dialogKey
|
||||
)
|
||||
messageDao.insertMessage(entity)
|
||||
|
||||
Log.d("MessageRepository", "<EFBFBD> Inserted OUTGOING message: fromMe=1, delivered=${entity.delivered}, read=0")
|
||||
}
|
||||
|
||||
// Обновляем диалог
|
||||
updateDialog(toPublicKey, text.trim(), timestamp)
|
||||
// 🔥 КРИТИЧНО: Обновляем диалог через updateDialogFromMessages
|
||||
Log.d("MessageRepository", "🔄 Calling updateDialogFromMessages after sending...")
|
||||
dialogDao.updateDialogFromMessages(account, toPublicKey)
|
||||
|
||||
// 🔥 Логируем что записалось в диалог
|
||||
val dialog = dialogDao.getDialog(account, toPublicKey)
|
||||
Log.d("MessageRepository", "🎯 DIALOG AFTER SEND: lastMsgFromMe=${dialog?.lastMessageFromMe}, delivered=${dialog?.lastMessageDelivered}, read=${dialog?.lastMessageRead}")
|
||||
|
||||
// 🔥 Отмечаем что я отправлял сообщения в этот диалог (перемещает из requests в chats)
|
||||
val updatedRows = dialogDao.markIHaveSent(account, toPublicKey)
|
||||
@@ -336,10 +344,17 @@ class MessageRepository private constructor(private val context: Context) {
|
||||
if (!stillExists) {
|
||||
// Сохраняем в БД только если сообщения нет
|
||||
messageDao.insertMessage(entity)
|
||||
|
||||
Log.d("MessageRepository", "<EFBFBD> Inserted INCOMING message: fromMe=0, delivered=${entity.delivered}, read=0")
|
||||
}
|
||||
|
||||
// Обновляем диалог ПОСЛЕ вставки сообщения
|
||||
updateDialog(packet.fromPublicKey, plainText, packet.timestamp, incrementUnread = true)
|
||||
// 🔥 КРИТИЧНО: Обновляем диалог через updateDialogFromMessages
|
||||
Log.d("MessageRepository", "🔄 Calling updateDialogFromMessages after incoming message...")
|
||||
dialogDao.updateDialogFromMessages(account, packet.fromPublicKey)
|
||||
|
||||
// 🔥 Логируем что записалось в диалог
|
||||
val dialog = dialogDao.getDialog(account, packet.fromPublicKey)
|
||||
Log.d("MessageRepository", "🎯 DIALOG AFTER INCOMING: lastMsgFromMe=${dialog?.lastMessageFromMe}, delivered=${dialog?.lastMessageDelivered}, read=${dialog?.lastMessageRead}")
|
||||
|
||||
// 🔥 Запрашиваем информацию о пользователе для отображения имени вместо ключа
|
||||
requestUserInfo(packet.fromPublicKey)
|
||||
@@ -381,8 +396,19 @@ class MessageRepository private constructor(private val context: Context) {
|
||||
suspend fun handleRead(packet: PacketRead) {
|
||||
val account = currentAccount ?: return
|
||||
|
||||
Log.d("MessageRepository", "🔥🔥🔥 handleRead START: fromPublicKey=${packet.fromPublicKey.take(16)}...")
|
||||
|
||||
// Проверяем последнее сообщение ДО обновления
|
||||
val lastMsgBefore = messageDao.getLastMessageDebug(account, packet.fromPublicKey)
|
||||
Log.d("MessageRepository", "📊 BEFORE markAllAsRead: fromMe=${lastMsgBefore?.fromMe}, delivered=${lastMsgBefore?.delivered}, read=${lastMsgBefore?.read}, timestamp=${lastMsgBefore?.timestamp}")
|
||||
|
||||
// Отмечаем все наши исходящие сообщения к этому собеседнику как прочитанные
|
||||
messageDao.markAllAsRead(account, packet.fromPublicKey)
|
||||
Log.d("MessageRepository", "✅ markAllAsRead completed")
|
||||
|
||||
// 🔥 DEBUG: Проверяем последнее сообщение ПОСЛЕ обновления
|
||||
val lastMsgAfter = messageDao.getLastMessageDebug(account, packet.fromPublicKey)
|
||||
Log.d("MessageRepository", "📊 AFTER markAllAsRead: fromMe=${lastMsgAfter?.fromMe}, delivered=${lastMsgAfter?.delivered}, read=${lastMsgAfter?.read}, timestamp=${lastMsgAfter?.timestamp}")
|
||||
|
||||
// Обновляем кэш - все исходящие сообщения помечаем как прочитанные
|
||||
val dialogKey = getDialogKey(packet.fromPublicKey)
|
||||
@@ -394,7 +420,13 @@ class MessageRepository private constructor(private val context: Context) {
|
||||
}
|
||||
|
||||
// 🔥 КРИТИЧНО: Обновляем диалог чтобы lastMessageRead обновился
|
||||
Log.d("MessageRepository", "🔄 Calling updateDialogFromMessages...")
|
||||
dialogDao.updateDialogFromMessages(account, packet.fromPublicKey)
|
||||
|
||||
// Логируем что записалось в диалог
|
||||
val dialog = dialogDao.getDialog(account, packet.fromPublicKey)
|
||||
Log.d("MessageRepository", "🎯 DIALOG AFTER UPDATE: lastMsgFromMe=${dialog?.lastMessageFromMe}, delivered=${dialog?.lastMessageDelivered}, read=${dialog?.lastMessageRead}")
|
||||
Log.d("MessageRepository", "🔥🔥🔥 handleRead END")
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user