Store lastMessage instead of it's fields in conversation

This commit is contained in:
moezbhatti
2019-11-09 03:43:15 -05:00
parent e720ee9560
commit 335c5757c4
11 changed files with 74 additions and 71 deletions
@@ -19,13 +19,11 @@
package com.moez.QKSMS.interactor
import com.moez.QKSMS.manager.NotificationManager
import com.moez.QKSMS.repository.ConversationRepository
import com.moez.QKSMS.repository.MessageRepository
import io.reactivex.Flowable
import javax.inject.Inject
class MarkRead @Inject constructor(
private val conversationRepo: ConversationRepository,
private val messageRepo: MessageRepository,
private val notificationManager: NotificationManager,
private val updateBadge: UpdateBadge
@@ -34,7 +32,6 @@ class MarkRead @Inject constructor(
override fun buildObservable(params: List<Long>): Flowable<*> {
return Flowable.just(params.toLongArray())
.doOnNext { threadIds -> messageRepo.markRead(*threadIds) }
.doOnNext { threadIds -> conversationRepo.updateConversations(*threadIds) } // Update the conversation
.doOnNext { threadIds -> threadIds.forEach(notificationManager::update) }
.flatMap { updateBadge.buildObservable(Unit) } // Update the badge
}
@@ -29,11 +29,7 @@ open class Conversation(
@Index var blocked: Boolean = false,
@Index var pinned: Boolean = false,
var recipients: RealmList<Recipient> = RealmList(),
var count: Int = 0,
var date: Long = 0,
var snippet: String = "",
var read: Boolean = true,
var me: Boolean = false,
var lastMessage: Message? = null,
var draft: String = "",
var blockingClient: Int? = null,
@@ -42,6 +38,11 @@ open class Conversation(
var name: String = "" // For group chats, the user is allowed to set a custom title for the conversation
) : RealmObject() {
val date: Long get() = lastMessage?.date ?: 0
val snippet: String? get() = lastMessage?.getSummary()
val unread: Boolean get() = lastMessage?.read == false
val me: Boolean get() = lastMessage?.isMe() == true
fun getTitle(): String {
return name.takeIf { it.isNotBlank() } ?: recipients.joinToString { recipient -> recipient.getDisplayName() }
}