fix: fix image loading & caching state
This commit is contained in:
@@ -236,6 +236,12 @@ interface MessageDao {
|
|||||||
@Query("UPDATE messages SET delivered = :status WHERE account = :account AND message_id = :messageId")
|
@Query("UPDATE messages SET delivered = :status WHERE account = :account AND message_id = :messageId")
|
||||||
suspend fun updateDeliveryStatus(account: String, messageId: String, status: Int)
|
suspend fun updateDeliveryStatus(account: String, messageId: String, status: Int)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 🔄 Обновить статус доставки и attachments (для очистки localUri после отправки)
|
||||||
|
*/
|
||||||
|
@Query("UPDATE messages SET delivered = :status, attachments = :attachments WHERE account = :account AND message_id = :messageId")
|
||||||
|
suspend fun updateDeliveryStatusAndAttachments(account: String, messageId: String, status: Int, attachments: String)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Обновить статус прочтения
|
* Обновить статус прочтения
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -385,6 +385,36 @@ if (message.id in currentIds) {
|
|||||||
_messages.value = _messages.value.map { msg ->
|
_messages.value = _messages.value.map { msg ->
|
||||||
if (msg.id == messageId) msg.copy(status = status) else msg
|
if (msg.id == messageId) msg.copy(status = status) else msg
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 🔥 Также обновляем кэш!
|
||||||
|
updateCacheFromCurrentMessages()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 🔄 Очистить localUri в attachments сообщения (после успешной отправки)
|
||||||
|
*/
|
||||||
|
private fun updateMessageAttachments(messageId: String, localUri: String?) {
|
||||||
|
_messages.value = _messages.value.map { msg ->
|
||||||
|
if (msg.id == messageId) {
|
||||||
|
val updatedAttachments = msg.attachments.map { att ->
|
||||||
|
att.copy(localUri = localUri ?: "")
|
||||||
|
}
|
||||||
|
msg.copy(attachments = updatedAttachments)
|
||||||
|
} else msg
|
||||||
|
}
|
||||||
|
|
||||||
|
// 🔥 Также обновляем кэш!
|
||||||
|
updateCacheFromCurrentMessages()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 🔥 Обновить кэш из текущих сообщений (для синхронизации после изменений)
|
||||||
|
*/
|
||||||
|
private fun updateCacheFromCurrentMessages() {
|
||||||
|
val account = myPublicKey ?: return
|
||||||
|
val opponent = opponentKey ?: return
|
||||||
|
val dialogKey = getDialogKey(account, opponent)
|
||||||
|
updateCacheWithLimit(dialogKey, _messages.value)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -398,6 +428,17 @@ if (message.id in currentIds) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 🔄 Обновить статус и attachments в БД (для очистки localUri после отправки)
|
||||||
|
*/
|
||||||
|
private suspend fun updateMessageStatusAndAttachmentsInDb(messageId: String, delivered: Int, attachmentsJson: String) {
|
||||||
|
val account = myPublicKey ?: return
|
||||||
|
try {
|
||||||
|
messageDao.updateDeliveryStatusAndAttachments(account, messageId, delivered, attachmentsJson)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Установить ключи пользователя
|
* Установить ключи пользователя
|
||||||
*/
|
*/
|
||||||
@@ -1702,24 +1743,20 @@ val newList = messages + optimisticMessages
|
|||||||
})
|
})
|
||||||
}.toString()
|
}.toString()
|
||||||
|
|
||||||
saveMessageToDatabase(
|
// 🔄 Обновляем статус на SENT и очищаем localUri в attachments
|
||||||
messageId = messageId,
|
// (сообщение уже существует в БД от optimistic UI, поэтому просто обновляем)
|
||||||
text = caption,
|
val finalAttachmentsJson = attachmentsJson // Уже без localUri
|
||||||
encryptedContent = encryptedContent,
|
|
||||||
encryptedKey = encryptedKey,
|
|
||||||
timestamp = timestamp,
|
|
||||||
isFromMe = true,
|
|
||||||
delivered = if (isSavedMessages) 2 else 0,
|
|
||||||
attachmentsJson = attachmentsJson
|
|
||||||
)
|
|
||||||
|
|
||||||
// Обновляем статус на SENT
|
|
||||||
if (!isSavedMessages) {
|
if (!isSavedMessages) {
|
||||||
updateMessageStatusInDb(messageId, 2)
|
updateMessageStatusAndAttachmentsInDb(messageId, 2, finalAttachmentsJson)
|
||||||
|
} else {
|
||||||
|
updateMessageStatusAndAttachmentsInDb(messageId, 2, finalAttachmentsJson)
|
||||||
}
|
}
|
||||||
|
|
||||||
withContext(Dispatchers.Main) {
|
withContext(Dispatchers.Main) {
|
||||||
updateMessageStatus(messageId, MessageStatus.SENT)
|
updateMessageStatus(messageId, MessageStatus.SENT)
|
||||||
|
// Также очищаем localUri в UI
|
||||||
|
updateMessageAttachments(messageId, null)
|
||||||
}
|
}
|
||||||
|
|
||||||
saveDialog(if (caption.isNotEmpty()) caption else "photo", timestamp)
|
saveDialog(if (caption.isNotEmpty()) caption else "photo", timestamp)
|
||||||
|
|||||||
Reference in New Issue
Block a user