feat: Enhance logging in MessageRepository and ChatsListViewModel for better debugging and flow tracking

This commit is contained in:
k1ngsterr1
2026-01-13 23:28:48 +05:00
parent 14ef342e80
commit 2c173bda26
6 changed files with 232 additions and 94 deletions

View File

@@ -198,6 +198,19 @@ interface MessageDao {
@Query("DELETE FROM messages WHERE account = :account AND message_id = :messageId")
suspend fun deleteMessage(account: String, messageId: String)
/**
* Получить количество непрочитанных сообщений для диалога
* Считает только входящие сообщения (from_me = 0) которые не прочитаны (read = 0)
*/
@Query("""
SELECT COUNT(*) FROM messages
WHERE account = :account
AND from_public_key = :opponentKey
AND from_me = 0
AND read = 0
""")
suspend fun getUnreadCountForDialog(account: String, opponentKey: String): Int
/**
* Удалить все сообщения диалога
*/
@@ -345,8 +358,74 @@ interface DialogDao {
fun getTotalUnreadCountExcludingFlow(account: String, excludeOpponentKey: String): Flow<Int>
/**
* Получить общее количество непрочитанных сообщений
* Обновить диалог, пересчитав счетчики из таблицы messages
* Этот метод аналогичен updateDialog из Архива - обновляет все поля диалога одним запросом
*
* Логика:
* 1. Берем последнее сообщение (по timestamp DESC)
* 2. Считаем количество непрочитанных сообщений (from_me = 0 AND read = 0)
* 3. Обновляем диалог или создаем новый
*/
@Query("SELECT COALESCE(SUM(unread_count), 0) FROM dialogs WHERE account = :account")
fun getTotalUnreadCountFlow(account: String): Flow<Int>
@Query("""
INSERT OR REPLACE INTO dialogs (
account,
opponent_key,
opponent_title,
opponent_username,
last_message,
last_message_timestamp,
unread_count,
is_online,
last_seen,
verified
)
SELECT
:account AS account,
:opponentKey AS opponent_key,
COALESCE(
(SELECT opponent_title FROM dialogs WHERE account = :account AND opponent_key = :opponentKey),
''
) AS opponent_title,
COALESCE(
(SELECT opponent_username FROM dialogs WHERE account = :account AND opponent_key = :opponentKey),
''
) AS opponent_username,
COALESCE(
(SELECT plain_message FROM messages
WHERE account = :account
AND ((from_public_key = :opponentKey AND to_public_key = :account)
OR (from_public_key = :account AND to_public_key = :opponentKey))
ORDER BY timestamp DESC LIMIT 1),
''
) AS last_message,
COALESCE(
(SELECT MAX(timestamp) FROM messages
WHERE account = :account
AND ((from_public_key = :opponentKey AND to_public_key = :account)
OR (from_public_key = :account AND to_public_key = :opponentKey))),
0
) AS last_message_timestamp,
COALESCE(
(SELECT COUNT(*) FROM messages
WHERE account = :account
AND from_public_key = :opponentKey
AND to_public_key = :account
AND from_me = 0
AND read = 0),
0
) AS unread_count,
COALESCE(
(SELECT is_online FROM dialogs WHERE account = :account AND opponent_key = :opponentKey),
0
) AS is_online,
COALESCE(
(SELECT last_seen FROM dialogs WHERE account = :account AND opponent_key = :opponentKey),
0
) AS last_seen,
COALESCE(
(SELECT verified FROM dialogs WHERE account = :account AND opponent_key = :opponentKey),
0
) AS verified
""")
suspend fun updateDialogFromMessages(account: String, opponentKey: String)
}