From 6bd8480e4ec5c0e946cbd48ed525d13c042132f1 Mon Sep 17 00:00:00 2001 From: Moez Bhatti Date: Thu, 28 Nov 2019 02:53:53 -0500 Subject: [PATCH] Include archived conversations in search results --- .../repository/ConversationRepositoryImpl.kt | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/data/src/main/java/com/moez/QKSMS/repository/ConversationRepositoryImpl.kt b/data/src/main/java/com/moez/QKSMS/repository/ConversationRepositoryImpl.kt index 224b7e33..6bfa6cc5 100644 --- a/data/src/main/java/com/moez/QKSMS/repository/ConversationRepositoryImpl.kt +++ b/data/src/main/java/com/moez/QKSMS/repository/ConversationRepositoryImpl.kt @@ -112,17 +112,26 @@ class ConversationRepositoryImpl @Inject constructor( } override fun searchConversations(query: CharSequence): List { - val normalizedQuery = query.removeAccents() - val conversations = getConversationsSnapshot() + val realm = Realm.getDefaultInstance() - val messagesByConversation = Realm.getDefaultInstance() + val normalizedQuery = query.removeAccents() + val conversations = realm.copyFromRealm(realm + .where(Conversation::class.java) + .notEqualTo("id", 0L) + .isNotNull("lastMessage") + .equalTo("blocked", false) + .isNotEmpty("recipients") + .sort("pinned", Sort.DESCENDING, "lastMessage.date", Sort.DESCENDING) + .findAll()) + + val messagesByConversation = realm.copyFromRealm(realm .where(Message::class.java) .beginGroup() .contains("body", normalizedQuery, Case.INSENSITIVE) .or() .contains("parts.text", normalizedQuery, Case.INSENSITIVE) .endGroup() - .findAll() + .findAll()) .asSequence() .groupBy { message -> message.threadId } .filter { (threadId, _) -> conversations.firstOrNull { it.id == threadId } != null } @@ -131,10 +140,11 @@ class ConversationRepositoryImpl @Inject constructor( .sortedByDescending { result -> result.messages } .toList() + realm.close() + return conversations .filter { conversation -> conversationFilter.filter(conversation, normalizedQuery) } - .map { conversation -> SearchResult(normalizedQuery, conversation, 0) } - .plus(messagesByConversation) + .map { conversation -> SearchResult(normalizedQuery, conversation, 0) } + messagesByConversation } override fun getBlockedConversations(): RealmResults {