Always calculate snippet when reading conversations

This ensures that correct snippet is displayed, because of moving
messages to recycle bin. This must be done for conversations in
recycle bin as well as regular conversations, because both can be
affected, depending on which messages are moved to recycle bin.
This commit is contained in:
Ensar Sarajčić 2023-07-25 15:38:24 +02:00
parent 24756285cc
commit b85661eb4d
2 changed files with 35 additions and 6 deletions

View File

@ -2,20 +2,33 @@ package com.simplemobiletools.smsmessenger.interfaces
import androidx.room.*
import com.simplemobiletools.smsmessenger.models.Conversation
import com.simplemobiletools.smsmessenger.models.ConversationWithSnippetOverride
@Dao
interface ConversationsDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertOrUpdate(conversation: Conversation): Long
@Query("SELECT * FROM conversations WHERE archived = 0")
fun getNonArchived(): List<Conversation>
@Query("SELECT (SELECT body FROM messages LEFT OUTER JOIN recycle_bin_messages ON messages.id = recycle_bin_messages.id WHERE recycle_bin_messages.id IS NULL AND messages.thread_id = conversations.thread_id ORDER BY messages.date DESC LIMIT 1) as new_snippet, * FROM conversations WHERE archived = 0")
fun getNonArchivedWithLatestSnippet(): List<ConversationWithSnippetOverride>
@Query("SELECT * FROM conversations WHERE archived = 1")
fun getAllArchived(): List<Conversation>
fun getNonArchived(): List<Conversation> {
return getNonArchivedWithLatestSnippet().map { it.toConversation() }
}
@Query("SELECT * FROM conversations WHERE (SELECT COUNT(*) FROM messages LEFT OUTER JOIN recycle_bin_messages ON messages.id = recycle_bin_messages.id WHERE recycle_bin_messages.id IS NOT NULL AND messages.thread_id = conversations.thread_id) > 0")
fun getAllWithMessagesInRecycleBin(): List<Conversation>
@Query("SELECT (SELECT body FROM messages LEFT OUTER JOIN recycle_bin_messages ON messages.id = recycle_bin_messages.id WHERE recycle_bin_messages.id IS NULL AND messages.thread_id = conversations.thread_id ORDER BY messages.date DESC LIMIT 1) as new_snippet, * FROM conversations WHERE archived = 1")
fun getAllArchivedWithLatestSnippet(): List<ConversationWithSnippetOverride>
fun getAllArchived(): List<Conversation> {
return getAllArchivedWithLatestSnippet().map { it.toConversation() }
}
@Query("SELECT (SELECT body FROM messages LEFT OUTER JOIN recycle_bin_messages ON messages.id = recycle_bin_messages.id WHERE recycle_bin_messages.id IS NOT NULL AND messages.thread_id = conversations.thread_id ORDER BY messages.date DESC LIMIT 1) as new_snippet, * FROM conversations WHERE (SELECT COUNT(*) FROM messages LEFT OUTER JOIN recycle_bin_messages ON messages.id = recycle_bin_messages.id WHERE recycle_bin_messages.id IS NOT NULL AND messages.thread_id = conversations.thread_id) > 0")
fun getAllWithMessagesInRecycleBinWithLatestSnippet(): List<ConversationWithSnippetOverride>
fun getAllWithMessagesInRecycleBin(): List<Conversation> {
return getAllWithMessagesInRecycleBinWithLatestSnippet().map { it.toConversation() }
}
@Query("SELECT * FROM conversations WHERE thread_id = :threadId")
fun getConversationWithThreadId(threadId: Long): Conversation?

View File

@ -0,0 +1,16 @@
package com.simplemobiletools.smsmessenger.models
import androidx.room.ColumnInfo
import androidx.room.Embedded
data class ConversationWithSnippetOverride(
@ColumnInfo(name = "new_snippet") val snippet: String?,
@Embedded val conversation: Conversation
) {
fun toConversation() =
if (snippet == null) {
conversation
} else {
conversation.copy(snippet = snippet)
}
}