Remove link between messages and conversations

This commit is contained in:
Moez Bhatti
2018-03-29 00:35:27 -04:00
parent d26adad7bf
commit ca548d97fb
19 changed files with 112 additions and 96 deletions
@@ -23,13 +23,18 @@ import repository.MessageRepository
import javax.inject.Inject
class DeleteMessage @Inject constructor(
private val messageRepository: MessageRepository,
private val messageRepo: MessageRepository,
private val updateBadge: UpdateBadge
) : Interactor<Long>() {
override fun buildObservable(params: Long): Flowable<*> {
return Flowable.just(params)
.doOnNext { messageId -> messageRepository.deleteMessage(messageId) }
.doOnNext { messageId ->
val threadId = messageRepo.getMessage(messageId)?.threadId
messageRepo.deleteMessage(messageId)
threadId?.let { messageRepo.updateConversation(it) }
}
.doOnNext { messageRepo.updateConversation(params) } // Update the conversation
.flatMap { updateBadge.buildObservable(Unit) } // Update the badge
}
@@ -32,6 +32,7 @@ class MarkRead @Inject constructor(
override fun buildObservable(params: Long): Flowable<*> {
return Flowable.just(Unit)
.doOnNext { messageRepo.markRead(params) }
.doOnNext { messageRepo.updateConversation(params) } // Update the conversation
.doOnNext { notificationManager.update(params) }
.flatMap { updateBadge.buildObservable(Unit) } // Update the badge
}
@@ -24,6 +24,7 @@ import manager.NotificationManager
import repository.MessageRepository
import repository.SyncRepository
import util.extensions.mapNotNull
import java.util.concurrent.TimeUnit
import javax.inject.Inject
class ReceiveMms @Inject constructor(
@@ -36,10 +37,13 @@ class ReceiveMms @Inject constructor(
override fun buildObservable(params: Uri): Flowable<*> {
return Flowable.just(params)
.flatMap { uri -> syncManager.syncMessage(uri) } // Sync the message
.mapNotNull { message -> message.conversation } // Map message to conversation
.doOnNext { message -> messageRepo.updateConversation(message.threadId) } // Update the conversation
.mapNotNull { message -> messageRepo.getOrCreateConversation(message.threadId) } // Map message to conversation
.filter { conversation -> !conversation.blocked } // Don't notify for blocked conversations
.doOnNext { conversation -> if (conversation.archived) messageRepo.markUnarchived(conversation.id) } // Unarchive conversation if necessary
.doOnNext { conversation -> notificationManager.update(conversation.id) } // Update the notification
.map { conversation -> conversation.id } // Map to the id because [delay] will put us on the wrong thread
.delay(1, TimeUnit.SECONDS) // Wait one second before trying to notify, in case the foreground app marks it as read first
.doOnNext { threadId -> notificationManager.update(threadId) } // Update the notification
.flatMap { updateBadge.buildObservable(Unit) } // Update the badge
}
@@ -44,11 +44,13 @@ class ReceiveSms @Inject constructor(
messageRepo.insertReceivedSms(address, body, time) // Add the message to the db
}
.mapNotNull { message -> message.conversation } // Map message to conversation
.doOnNext { message -> messageRepo.updateConversation(message.threadId) } // Update the conversation
.mapNotNull { message -> messageRepo.getOrCreateConversation(message.threadId) } // Map message to conversation
.filter { conversation -> !conversation.blocked } // Don't notify for blocked conversations
.doOnNext { conversation -> if (conversation.archived) messageRepo.markUnarchived(conversation.id) } // Unarchive conversation if necessary
.map { conversation -> conversation.id } // Map to the id because [delay] will put us on the wrong thread
.delay(1, TimeUnit.SECONDS) // Wait one second before trying to notify, in case the foreground app marks it as read first
.doOnNext { conversation -> notificationManager.update(conversation.id) } // Update the notification
.doOnNext { threadId -> notificationManager.update(threadId) } // Update the notification
.flatMap { updateBadge.buildObservable(Unit) } // Update the badge
}
@@ -30,7 +30,7 @@ class UpdateBadge @Inject constructor(
override fun buildObservable(params: Unit): Flowable<*> {
return Flowable.just(params)
.map { messageRepo.getUnreadMessageCount() }
.map { messageRepo.getUnreadCount() }
.map { count -> count.toInt() }
.doOnNext { count -> widgetManager.updateUnreadCount(count) }
}
+5 -1
View File
@@ -28,9 +28,13 @@ import java.util.*
open class Conversation : RealmObject() {
@PrimaryKey var id: Long = 0
var recipients: RealmList<Recipient> = RealmList()
@Index var archived: Boolean = false
@Index var blocked: Boolean = false
var recipients: RealmList<Recipient> = RealmList()
var date: Long = 0
var snippet: String = ""
var read: Boolean = true
var me: Boolean = false
var draft: String = ""
fun getTitle() = recipients.joinToString { recipient ->
+2 -15
View File
@@ -32,19 +32,7 @@ open class Message : RealmObject() {
@PrimaryKey var id: Long = 0
var conversation: Conversation? = null
set(value) {
field = value
threadId = value?.id ?: 0
}
/**
* This is a hack so that we can index by the threadId, and return distinct threadIds when
* querying for all conversations for the MainActivity
*/
@Index
@Deprecated("This is only here for Realm to access - it should be ignored in our codebase")
var threadId: Long = 0
@Index var threadId: Long = 0
// The MMS-SMS content provider returns messages where duplicate ids can exist. This is because
// SMS and MMS are stored in separate tables. We can't use these ids as our realm message id
@@ -136,8 +124,7 @@ open class Message : RealmObject() {
.mapNotNull { it.text }
.joinToString("\n") { text -> text }
.takeIf { it.isNotEmpty() }
?: getCleansedSubject()
.takeIf { it.isNotEmpty() }
?: getCleansedSubject().takeIf { it.isNotEmpty() }
?: "An MMS message"
}
}
@@ -28,9 +28,9 @@ import model.MmsPart
interface MessageRepository {
fun getConversations(archived: Boolean = false): RealmResults<Message>
fun getConversations(archived: Boolean = false): RealmResults<Conversation>
fun getConversationsSnapshot(): List<Message>
fun getConversationsSnapshot(): List<Conversation>
fun getBlockedConversations(): Flowable<List<Conversation>>
@@ -52,7 +52,7 @@ interface MessageRepository {
fun getMessageForPart(id: Long): Message?
fun getUnreadMessageCount(): Long
fun getUnreadCount(): Long
fun getPart(id: Long): MmsPart?
@@ -70,6 +70,11 @@ interface MessageRepository {
*/
fun getUnreadMessages(threadId: Long): RealmResults<Message>
/**
* Updates message-related fields in the conversation, like the date and snippet
*/
fun updateConversation(threadId: Long)
fun markArchived(threadId: Long)
fun markUnarchived(threadId: Long)