Create conversation when message is received from new sender

This commit is contained in:
Moez Bhatti
2018-01-10 20:17:43 -05:00
parent 665e1442c2
commit f1e4d3df42
2 changed files with 12 additions and 15 deletions
@@ -39,7 +39,6 @@ import io.reactivex.Flowable
import io.reactivex.Maybe import io.reactivex.Maybe
import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.rxkotlin.Flowables import io.reactivex.rxkotlin.Flowables
import io.reactivex.rxkotlin.toFlowable
import io.reactivex.schedulers.Schedulers import io.reactivex.schedulers.Schedulers
import io.realm.Realm import io.realm.Realm
import io.realm.RealmList import io.realm.RealmList
@@ -98,8 +97,8 @@ class MessageRepository @Inject constructor(
.findFirst() .findFirst()
} }
fun getOrCreateConversation(threadId: Long) { fun getOrCreateConversation(threadId: Long): Conversation? {
getConversation(threadId) ?: getConversationFromCp(threadId) return getConversation(threadId) ?: getConversationFromCp(threadId)
} }
fun getOrCreateConversation(address: String): Maybe<Conversation> { fun getOrCreateConversation(address: String): Maybe<Conversation> {
@@ -468,23 +467,21 @@ class MessageRepository @Inject constructor(
private fun getConversationFromCp(threadId: Long): Conversation? { private fun getConversationFromCp(threadId: Long): Conversation? {
var conversation: Conversation? = null var conversation: Conversation? = null
val cursor = context.contentResolver.query( val cursor = context.contentResolver.query(CursorToConversation.URI, CursorToConversation.PROJECTION,
CursorToConversation.URI, "_id = ?", arrayOf(threadId.toString()), null)
CursorToConversation.PROJECTION,
"_id = ?",
arrayOf(threadId.toString()),
null)
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {
conversation = cursorToConversation.map(cursor) conversation = cursorToConversation.map(cursor)
conversation.insertOrUpdate()
conversation.recipients.toFlowable() val recipients = conversation.recipients
.map { recipient -> recipient.id.toString() } .map { recipient -> recipient.id.toString() }
.map { id -> context.contentResolver.query(CursorToRecipient.URI, null, "_id = ?", arrayOf(id), null) } .map { id -> context.contentResolver.query(CursorToRecipient.URI, null, "_id = ?", arrayOf(id), null) }
.flatMap { recipientCursor -> recipientCursor.asFlowable().map { cursor -> cursorToRecipient.map(cursor) } } .map { recipientCursor -> recipientCursor.map { cursorToRecipient.map(recipientCursor) } }
.doOnNext { recipient -> recipient.insertOrUpdate() } .flatten()
.blockingSubscribe()
conversation.recipients.clear()
conversation.recipients.addAll(recipients)
conversation.insertOrUpdate()
} }
cursor.close() cursor.close()
@@ -34,7 +34,7 @@ class ReceiveMessage @Inject constructor(
override fun buildObservable(params: Params): Flowable<Unit> { override fun buildObservable(params: Params): Flowable<Unit> {
return Flowable.just(params) return Flowable.just(params)
.map { messageRepo.insertReceivedSms(params.address, params.body, params.sentTime) } // Add the message to the db .map { messageRepo.insertReceivedSms(params.address, params.body, params.sentTime) } // Add the message to the db
.mapNotNull { message -> messageRepo.getConversation(message.threadId) } // Map message to conversation .mapNotNull { message -> messageRepo.getOrCreateConversation(message.threadId) } // Map message to conversation
.doOnNext { conversation -> if (conversation.archived) messageRepo.markUnarchived(conversation.id) } // Unarchive conversation if necessary .doOnNext { conversation -> if (conversation.archived) messageRepo.markUnarchived(conversation.id) } // Unarchive conversation if necessary
.doOnNext { conversation -> notificationManager.update(conversation.id) } // Update the notification .doOnNext { conversation -> notificationManager.update(conversation.id) } // Update the notification
.map { } .map { }