Allow for nullable cursor when syncing message

This commit is contained in:
Moez Bhatti
2018-04-06 17:49:27 -04:00
parent 2a638d29a3
commit 7779ed219d
4 changed files with 22 additions and 17 deletions
@@ -23,7 +23,6 @@ import android.content.ContentUris
import android.net.Uri
import android.provider.Telephony
import android.telephony.PhoneNumberUtils
import io.reactivex.Flowable
import io.realm.Realm
import mapper.CursorToContact
import mapper.CursorToConversation
@@ -35,7 +34,6 @@ import model.Message
import model.MmsPart
import model.Recipient
import model.SyncLog
import util.extensions.asFlowable
import util.extensions.insertOrUpdate
import util.extensions.map
import util.extensions.mapWhile
@@ -138,14 +136,17 @@ class SyncRepositoryImpl @Inject constructor(
status = Status.Idle()
}
override fun syncMessage(uri: Uri): Flowable<Message> {
override fun syncMessage(uri: Uri): Message? {
// If we don't have a valid type, return null
val type = when {
uri.toString().contains("mms") -> "mms"
uri.toString().contains("sms") -> "sms"
else -> return Flowable.empty()
else -> return null
}
val id = tryOrNull { ContentUris.parseId(uri) } ?: return Flowable.empty()
// If we don't have a valid id, return null
val id = tryOrNull { ContentUris.parseId(uri) } ?: return null
// Check if the message already exists, so we can reuse the id
val existingId = Realm.getDefaultInstance().use { realm ->
@@ -164,15 +165,19 @@ class SyncRepositoryImpl @Inject constructor(
else -> ContentUris.withAppendedId(Telephony.Sms.CONTENT_URI, id)
}
val cursor = contentResolver.query(stableUri, null, null, null, null)
val columnsMap = CursorToMessage.MessageColumns(cursor)
return contentResolver.query(stableUri, null, null, null, null)?.use { cursor ->
// Map the cursor to a message
return cursor.asFlowable()
.map { cursorToMessage.map(Pair(it, columnsMap)) }
.doOnNext { message -> existingId?.let { message.id = it } }
.doOnNext { message -> messageRepo.getOrCreateConversation(message.threadId) }
.doOnNext { message -> message.insertOrUpdate() }
// If there are no rows, return null. Otherwise, we've moved to the first row
if (!cursor.moveToFirst()) return null
val columnsMap = CursorToMessage.MessageColumns(cursor)
cursorToMessage.map(Pair(cursor, columnsMap)).apply {
existingId?.let { this.id = it }
messageRepo.getOrCreateConversation(threadId)
insertOrUpdate()
}
}
}
override fun syncContacts() {
@@ -38,7 +38,7 @@ class ReceiveMms @Inject constructor(
override fun buildObservable(params: Uri): Flowable<*> {
return Flowable.just(params)
.flatMap { uri -> syncManager.syncMessage(uri) } // Sync the message
.mapNotNull { uri -> syncManager.syncMessage(uri) } // Sync the message
.filter { message ->
// Because we use the smsmms library for receiving and storing MMS, we'll need
// to check if it should be blocked after we've pulled it into realm. If it
@@ -22,13 +22,14 @@ import android.net.Uri
import io.reactivex.Flowable
import model.Message
import repository.SyncRepository
import util.extensions.mapNotNull
import javax.inject.Inject
class SyncMessage @Inject constructor(private val syncManager: SyncRepository) : Interactor<Uri>() {
override fun buildObservable(params: Uri): Flowable<Message> {
return Flowable.just(params)
.flatMap { uri -> syncManager.syncMessage(uri) }
.mapNotNull { uri -> syncManager.syncMessage(uri) }
}
}
@@ -19,14 +19,13 @@
package repository
import android.net.Uri
import io.reactivex.Flowable
import model.Message
interface SyncRepository {
fun syncMessages(fullSync: Boolean = false)
fun syncMessage(uri: Uri): Flowable<Message>
fun syncMessage(uri: Uri): Message?
fun syncContacts()