Swipe to archive (#6)

* Added support for moving conversations to archived inbox

* Optimized query for loading conversations

* Upgrade RxKotlin

* Explicitly define type for archivedConversationIntent

* Ensure that swipe functionality is only for Inbox
This commit is contained in:
Moez Bhatti
2017-11-15 14:55:18 -05:00
committed by GitHub
parent bc68cf74f7
commit 67e778102e
10 changed files with 164 additions and 41 deletions
@@ -0,0 +1,6 @@
package com.moez.QKSMS.data.model
/**
* Groups a conversation with its latest message into a single model
*/
data class ConversationMessagePair(val conversation: Conversation, val message: Message)
@@ -10,8 +10,10 @@ import com.moez.QKSMS.common.util.extensions.asFlowable
import com.moez.QKSMS.common.util.extensions.insertOrUpdate
import com.moez.QKSMS.data.mapper.CursorToMessageFlowable
import com.moez.QKSMS.data.model.Conversation
import com.moez.QKSMS.data.model.ConversationMessagePair
import com.moez.QKSMS.data.model.Message
import io.reactivex.Flowable
import io.reactivex.functions.BiFunction
import io.reactivex.schedulers.Schedulers
import io.realm.Realm
import io.realm.RealmResults
@@ -25,12 +27,29 @@ class MessageRepository @Inject constructor(
private val context: Context,
private val cursorToMessageFlowable: CursorToMessageFlowable) {
fun getConversationMessagesAsync(): RealmResults<Message> {
return Realm.getDefaultInstance()
.where(Message::class.java)
fun getConversations(archived: Boolean = false): Flowable<List<ConversationMessagePair>> {
val realm = Realm.getDefaultInstance()
val conversations = realm.where(Conversation::class.java)
.equalTo("archived", archived)
.findAllAsync()
.asFlowable()
.filter { it.isLoaded }
.map { conversations -> conversations.associateBy { conversation -> conversation.id } }
val messages = realm.where(Message::class.java)
.findAllSortedAsync("date", Sort.DESCENDING)
.where()
.distinctAsync("threadId")
.asFlowable()
.filter { it.isLoaded }
return Flowable.combineLatest(conversations, messages, BiFunction { conversations, messages ->
messages.mapNotNull { message ->
val conversation = conversations[message.threadId]
if (conversation == null) null else ConversationMessagePair(conversation, message)
}
})
}
fun getConversationAsync(threadId: Long): Conversation {
@@ -69,7 +88,8 @@ class MessageRepository @Inject constructor(
.equalTo("archived", false)
.findFirst()
conversation?.archived = true
realm.executeTransaction { conversation?.archived = true }
realm.close()
}