forked from upstream-mirrors/quik
Simplify mapping of cursor -> conversation and cursor -> message
This commit is contained in:
+5
-11
@@ -3,28 +3,22 @@ package com.moez.QKSMS.data.mapper
|
||||
import android.database.Cursor
|
||||
import android.net.Uri
|
||||
import android.provider.Telephony.Threads
|
||||
import com.moez.QKSMS.common.util.extensions.asFlowable
|
||||
import com.moez.QKSMS.data.model.Conversation
|
||||
import com.moez.QKSMS.data.repository.ContactRepository
|
||||
import io.reactivex.Flowable
|
||||
import io.reactivex.rxkotlin.toFlowable
|
||||
import javax.inject.Inject
|
||||
|
||||
class CursorToConversationFlowable @Inject constructor(private val contactsRepo: ContactRepository) : Mapper<Cursor, Flowable<Conversation>> {
|
||||
class CursorToConversation @Inject constructor(private val contactsRepo: ContactRepository) : Mapper<Cursor, Conversation> {
|
||||
|
||||
override fun map(from: Cursor): Flowable<Conversation> {
|
||||
return from.asFlowable().map { cursor ->
|
||||
val conversation = Conversation()
|
||||
override fun map(from: Cursor): Conversation {
|
||||
return Conversation().apply {
|
||||
id = from.getLong(ID)
|
||||
|
||||
conversation.id = cursor.getLong(ID)
|
||||
|
||||
conversation.contacts.addAll(cursor.getString(RECIPIENT_IDS).split(" ").toFlowable()
|
||||
contacts.addAll(from.getString(RECIPIENT_IDS).split(" ").toFlowable()
|
||||
.map { id -> id.toLong() }
|
||||
.flatMap { id -> contactsRepo.getContact(id) }
|
||||
.filter { contact -> contact.recipientId != 0L }
|
||||
.blockingIterable())
|
||||
|
||||
conversation
|
||||
}
|
||||
}
|
||||
|
||||
+62
-66
@@ -3,74 +3,72 @@ package com.moez.QKSMS.data.mapper
|
||||
import android.content.Context
|
||||
import android.database.Cursor
|
||||
import android.provider.Telephony.*
|
||||
import com.moez.QKSMS.common.util.extensions.asFlowable
|
||||
import com.moez.QKSMS.data.model.Message
|
||||
import com.moez.QKSMS.data.model.Message.DeliveryStatus
|
||||
import io.reactivex.Flowable
|
||||
import timber.log.Timber
|
||||
import javax.inject.Inject
|
||||
|
||||
class CursorToMessageFlowable @Inject constructor(val context: Context) : Mapper<Cursor, Flowable<Message>> {
|
||||
class CursorToMessage @Inject constructor(val context: Context) : Mapper<Pair<Cursor, CursorToMessage.MessageColumns>, Message> {
|
||||
|
||||
override fun map(from: Cursor): Flowable<Message> {
|
||||
val columnsMap = MessageColumns(from)
|
||||
return from.asFlowable().map { cursor ->
|
||||
Message().apply {
|
||||
type = when (cursor.getColumnIndex(MmsSms.TYPE_DISCRIMINATOR_COLUMN)) {
|
||||
-1 -> "sms"
|
||||
else -> cursor.getString(columnsMap.msgType)
|
||||
override fun map(from: Pair<Cursor, MessageColumns>): Message {
|
||||
val cursor = from.first
|
||||
val columnsMap = from.second
|
||||
|
||||
return Message().apply {
|
||||
type = when (cursor.getColumnIndex(MmsSms.TYPE_DISCRIMINATOR_COLUMN)) {
|
||||
-1 -> "sms"
|
||||
else -> cursor.getString(columnsMap.msgType)
|
||||
}
|
||||
|
||||
id = cursor.getLong(columnsMap.msgId)
|
||||
|
||||
when (type) {
|
||||
"sms" -> {
|
||||
threadId = cursor.getLong(columnsMap.smsThreadId)
|
||||
address = cursor.getString(columnsMap.smsAddress) ?: ""
|
||||
boxId = cursor.getInt(columnsMap.smsType)
|
||||
date = cursor.getLong(columnsMap.smsDate)
|
||||
dateSent = cursor.getLong(columnsMap.smsDateSent)
|
||||
read = cursor.getInt(columnsMap.smsRead) != 0
|
||||
seen = cursor.getInt(columnsMap.smsSeen) != 0
|
||||
locked = cursor.getInt(columnsMap.smsLocked) != 0
|
||||
|
||||
val status = cursor.getLong(columnsMap.smsStatus)
|
||||
deliveryStatus = when {
|
||||
status == Sms.STATUS_NONE.toLong() -> DeliveryStatus.NONE
|
||||
status >= Sms.STATUS_FAILED -> DeliveryStatus.FAILED
|
||||
status >= Sms.STATUS_PENDING -> DeliveryStatus.PENDING
|
||||
else -> DeliveryStatus.RECEIVED
|
||||
}
|
||||
|
||||
// SMS Specific
|
||||
body = cursor.getString(columnsMap.smsBody) ?: ""
|
||||
errorCode = cursor.getInt(columnsMap.smsErrorCode)
|
||||
}
|
||||
|
||||
id = cursor.getLong(columnsMap.msgId)
|
||||
"mms" -> {
|
||||
threadId = cursor.getLong(columnsMap.mmsThreadId)
|
||||
address = getMmsAddress(id)
|
||||
boxId = cursor.getInt(columnsMap.mmsMessageBox)
|
||||
date = cursor.getLong(columnsMap.mmsDate) * 1000L
|
||||
dateSent = cursor.getLong(columnsMap.mmsDateSent)
|
||||
read = cursor.getInt(columnsMap.mmsRead) != 0
|
||||
seen = cursor.getInt(columnsMap.mmsSeen) != 0
|
||||
locked = cursor.getInt(columnsMap.mmsLocked) != 0
|
||||
|
||||
when (type) {
|
||||
"sms" -> {
|
||||
threadId = cursor.getLong(columnsMap.smsThreadId)
|
||||
address = cursor.getString(columnsMap.smsAddress) ?: ""
|
||||
boxId = cursor.getInt(columnsMap.smsType)
|
||||
date = cursor.getLong(columnsMap.mmsDate)
|
||||
dateSent = cursor.getLong(columnsMap.mmsDateSent)
|
||||
read = cursor.getInt(columnsMap.mmsRead) != 0
|
||||
seen = cursor.getInt(columnsMap.mmsSeen) != 0
|
||||
locked = cursor.getInt(columnsMap.smsLocked) != 0
|
||||
|
||||
val status = cursor.getLong(columnsMap.smsStatus)
|
||||
deliveryStatus = when {
|
||||
status == Sms.STATUS_NONE.toLong() -> DeliveryStatus.NONE
|
||||
status >= Sms.STATUS_FAILED -> DeliveryStatus.FAILED
|
||||
status >= Sms.STATUS_PENDING -> DeliveryStatus.PENDING
|
||||
else -> DeliveryStatus.RECEIVED
|
||||
}
|
||||
|
||||
// SMS Specific
|
||||
body = cursor.getString(columnsMap.smsBody) ?: ""
|
||||
errorCode = cursor.getInt(columnsMap.smsErrorCode)
|
||||
}
|
||||
|
||||
"mms" -> {
|
||||
threadId = cursor.getLong(columnsMap.mmsThreadId)
|
||||
address = getMmsAddress(id)
|
||||
boxId = cursor.getInt(columnsMap.mmsMessageBox)
|
||||
date = cursor.getLong(columnsMap.mmsDate) * 1000L
|
||||
dateSent = cursor.getLong(columnsMap.mmsDateSent)
|
||||
read = cursor.getInt(columnsMap.mmsRead) != 0
|
||||
seen = cursor.getInt(columnsMap.mmsSeen) != 0
|
||||
locked = cursor.getInt(columnsMap.mmsLocked) != 0
|
||||
|
||||
// MMS Specific
|
||||
attachmentType = when (cursor.getInt(columnsMap.mmsTextOnly)) {
|
||||
0 -> Message.AttachmentType.NOT_LOADED
|
||||
else -> Message.AttachmentType.TEXT
|
||||
}
|
||||
mmsDeliveryStatusString = cursor.getString(columnsMap.mmsDeliveryReport) ?: ""
|
||||
errorType = cursor.getInt(columnsMap.mmsErrorType)
|
||||
messageSize = 0
|
||||
readReportString = cursor.getString(columnsMap.mmsReadReport) ?: ""
|
||||
messageType = cursor.getInt(columnsMap.mmsMessageType)
|
||||
mmsStatus = cursor.getInt(columnsMap.mmsStatus)
|
||||
subject = cursor.getString(columnsMap.mmsSubject) ?: ""
|
||||
textContentType = ""
|
||||
// MMS Specific
|
||||
attachmentType = when (cursor.getInt(columnsMap.mmsTextOnly)) {
|
||||
0 -> Message.AttachmentType.NOT_LOADED
|
||||
else -> Message.AttachmentType.TEXT
|
||||
}
|
||||
mmsDeliveryStatusString = cursor.getString(columnsMap.mmsDeliveryReport) ?: ""
|
||||
errorType = cursor.getInt(columnsMap.mmsErrorType)
|
||||
messageSize = 0
|
||||
readReportString = cursor.getString(columnsMap.mmsReadReport) ?: ""
|
||||
messageType = cursor.getInt(columnsMap.mmsMessageType)
|
||||
mmsStatus = cursor.getInt(columnsMap.mmsStatus)
|
||||
subject = cursor.getString(columnsMap.mmsSubject) ?: ""
|
||||
textContentType = ""
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -131,7 +129,7 @@ class CursorToMessageFlowable @Inject constructor(val context: Context) : Mapper
|
||||
Mms.TEXT_ONLY)
|
||||
}
|
||||
|
||||
private class MessageColumns(val cursor: Cursor) {
|
||||
class MessageColumns(private val cursor: Cursor) {
|
||||
|
||||
val msgType by lazy { getColumnIndex(MmsSms.TYPE_DISCRIMINATOR_COLUMN) }
|
||||
val msgId by lazy { getColumnIndex(MmsSms._ID) }
|
||||
@@ -165,13 +163,11 @@ class CursorToMessageFlowable @Inject constructor(val context: Context) : Mapper
|
||||
val mmsStatus by lazy { getColumnIndex(Mms.STATUS) }
|
||||
val mmsTextOnly by lazy { getColumnIndex(Mms.TEXT_ONLY) }
|
||||
|
||||
private fun getColumnIndex(columnsName: String): Int {
|
||||
return try {
|
||||
cursor.getColumnIndexOrThrow(columnsName)
|
||||
} catch (e: Exception) {
|
||||
Timber.w(e)
|
||||
-1
|
||||
}
|
||||
private fun getColumnIndex(columnsName: String) = try {
|
||||
cursor.getColumnIndexOrThrow(columnsName)
|
||||
} catch (e: Exception) {
|
||||
Timber.w(e)
|
||||
-1
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@ import android.provider.Telephony.Sms
|
||||
import android.provider.Telephony.TextBasedSmsColumns
|
||||
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.mapper.CursorToMessage
|
||||
import com.moez.QKSMS.data.model.Conversation
|
||||
import com.moez.QKSMS.data.model.ConversationMessagePair
|
||||
import com.moez.QKSMS.data.model.Message
|
||||
@@ -25,7 +25,7 @@ import javax.inject.Singleton
|
||||
@Singleton
|
||||
class MessageRepository @Inject constructor(
|
||||
private val context: Context,
|
||||
private val cursorToMessageFlowable: CursorToMessageFlowable) {
|
||||
private val cursorToMessageFlowable: CursorToMessage) {
|
||||
|
||||
fun getConversations(archived: Boolean = false): Flowable<List<ConversationMessagePair>> {
|
||||
val realm = Realm.getDefaultInstance()
|
||||
@@ -201,8 +201,8 @@ class MessageRepository @Inject constructor(
|
||||
|
||||
fun addMessageFromUri(uri: Uri) {
|
||||
val cursor = context.contentResolver.query(uri, null, null, null, "date DESC")
|
||||
cursorToMessageFlowable.map(cursor)
|
||||
.subscribe { message -> message.insertOrUpdate() }
|
||||
val columnsMap = CursorToMessage.MessageColumns(cursor)
|
||||
cursorToMessageFlowable.map(Pair(cursor, columnsMap)).insertOrUpdate()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,20 +3,22 @@ package com.moez.QKSMS.domain.interactor
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import android.provider.Telephony
|
||||
import com.moez.QKSMS.data.mapper.CursorToConversationFlowable
|
||||
import com.moez.QKSMS.data.mapper.CursorToMessageFlowable
|
||||
import com.moez.QKSMS.common.util.extensions.asFlowable
|
||||
import com.moez.QKSMS.data.mapper.CursorToConversation
|
||||
import com.moez.QKSMS.data.mapper.CursorToMessage
|
||||
import io.reactivex.Flowable
|
||||
import io.realm.Realm
|
||||
import javax.inject.Inject
|
||||
|
||||
class FullSync @Inject constructor(
|
||||
private val context: Context,
|
||||
private val cursorToConversationFlowable: CursorToConversationFlowable,
|
||||
private val cursorToMessageFlowable: CursorToMessageFlowable) : Interactor<Long, Unit>() {
|
||||
private val cursorToConversation: CursorToConversation,
|
||||
private val cursorToMessage: CursorToMessage)
|
||||
: Interactor<Long, Unit>() {
|
||||
|
||||
override fun buildObservable(params: Unit): Flowable<Long> {
|
||||
val contentResolver = context.contentResolver
|
||||
val conversationsCursor = contentResolver.query(CursorToConversationFlowable.URI, CursorToConversationFlowable.PROJECTION, null, null, "date desc")
|
||||
val conversationsCursor = contentResolver.query(CursorToConversation.URI, CursorToConversation.PROJECTION, null, null, "date desc")
|
||||
|
||||
var realm: Realm? = null
|
||||
|
||||
@@ -27,13 +29,16 @@ class FullSync @Inject constructor(
|
||||
realm?.beginTransaction()
|
||||
realm?.deleteAll()
|
||||
}
|
||||
.flatMap { cursor -> cursorToConversationFlowable.map(cursor) }
|
||||
.flatMap { cursor -> cursor.asFlowable().map { cursorToConversation.map(cursor) } }
|
||||
.distinct { conversation -> conversation.id }
|
||||
.doOnNext { conversation -> realm?.insertOrUpdate(conversation) }
|
||||
.map { conversation -> conversation.id }
|
||||
.map { threadId -> Uri.withAppendedPath(Telephony.MmsSms.CONTENT_CONVERSATIONS_URI, threadId.toString()) }
|
||||
.map { threadUri -> contentResolver.query(threadUri, CursorToMessageFlowable.CURSOR_PROJECTION, null, null, "date desc") }
|
||||
.flatMap { threadCursor -> cursorToMessageFlowable.map(threadCursor) }
|
||||
.map { threadUri -> contentResolver.query(threadUri, CursorToMessage.CURSOR_PROJECTION, null, null, "date desc") }
|
||||
.flatMap { cursor ->
|
||||
val columnsMap = CursorToMessage.MessageColumns(cursor)
|
||||
cursor.asFlowable().map { cursorToMessage.map(Pair(cursor, columnsMap)) }
|
||||
}
|
||||
.filter { message -> message.type == "sms" || message.type == "mms" }
|
||||
.distinct { message -> message.id }
|
||||
.doOnNext { message -> realm?.insertOrUpdate(message) }
|
||||
|
||||
Reference in New Issue
Block a user