diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/interfaces/ConversationsDao.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/interfaces/ConversationsDao.kt index 271cc83b..e5e1259a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/interfaces/ConversationsDao.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/interfaces/ConversationsDao.kt @@ -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 + @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 - @Query("SELECT * FROM conversations WHERE archived = 1") - fun getAllArchived(): List + fun getNonArchived(): List { + 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 + @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 + + fun getAllArchived(): List { + 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 + + fun getAllWithMessagesInRecycleBin(): List { + return getAllWithMessagesInRecycleBinWithLatestSnippet().map { it.toConversation() } + } @Query("SELECT * FROM conversations WHERE thread_id = :threadId") fun getConversationWithThreadId(threadId: Long): Conversation? diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/ConversationWithSnippetOverride.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/ConversationWithSnippetOverride.kt new file mode 100644 index 00000000..ede5e4e5 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/models/ConversationWithSnippetOverride.kt @@ -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) + } +}