forked from upstream-mirrors/quik
Code quality improvements for Sync
This commit is contained in:
@@ -2,6 +2,7 @@ package com.moez.QKSMS.data.mapper
|
||||
|
||||
import android.content.Context
|
||||
import android.database.Cursor
|
||||
import android.net.Uri
|
||||
import android.provider.Telephony.*
|
||||
import com.moez.QKSMS.data.model.Message
|
||||
import com.moez.QKSMS.data.model.Message.DeliveryStatus
|
||||
@@ -95,7 +96,8 @@ class CursorToMessage @Inject constructor(val context: Context) : Mapper<Pair<Cu
|
||||
}
|
||||
|
||||
companion object {
|
||||
val CURSOR_PROJECTION = arrayOf(
|
||||
val URI = Uri.parse("content://mms-sms/complete-conversations")
|
||||
val PROJECTION = arrayOf(
|
||||
MmsSms.TYPE_DISCRIMINATOR_COLUMN,
|
||||
MmsSms._ID,
|
||||
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.moez.QKSMS.data.mapper
|
||||
|
||||
import android.database.Cursor
|
||||
import android.net.Uri
|
||||
import com.moez.QKSMS.data.model.Recipient
|
||||
import javax.inject.Inject
|
||||
|
||||
class CursorToRecipient @Inject constructor() : Mapper<Cursor, Recipient> {
|
||||
|
||||
companion object {
|
||||
val URI = Uri.parse("content://mms-sms/canonical-addresses")
|
||||
|
||||
val COLUMN_ID = 0
|
||||
val COLUMN_ADDRESS = 1
|
||||
}
|
||||
|
||||
override fun map(from: Cursor) = Recipient().apply {
|
||||
id = from.getLong(COLUMN_ID)
|
||||
address = from.getString(COLUMN_ADDRESS)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package com.moez.QKSMS.domain.interactor
|
||||
import android.content.Context
|
||||
import com.moez.QKSMS.data.mapper.CursorToConversation
|
||||
import com.moez.QKSMS.data.mapper.CursorToMessage
|
||||
import com.moez.QKSMS.data.mapper.CursorToRecipient
|
||||
import io.reactivex.Flowable
|
||||
import io.realm.Realm
|
||||
import javax.inject.Inject
|
||||
@@ -10,8 +11,9 @@ import javax.inject.Inject
|
||||
class FullSync @Inject constructor(
|
||||
context: Context,
|
||||
cursorToConversation: CursorToConversation,
|
||||
cursorToMessage: CursorToMessage)
|
||||
: PartialSync(context, cursorToConversation, cursorToMessage) {
|
||||
cursorToMessage: CursorToMessage,
|
||||
cursorToRecipient: CursorToRecipient)
|
||||
: PartialSync(context, cursorToConversation, cursorToMessage, cursorToRecipient) {
|
||||
|
||||
override fun buildObservable(params: Unit): Flowable<Long> {
|
||||
val realm = Realm.getDefaultInstance()
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
package com.moez.QKSMS.domain.interactor
|
||||
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import android.provider.Telephony
|
||||
import com.moez.QKSMS.common.util.extensions.asFlowable
|
||||
import com.moez.QKSMS.data.mapper.CursorToConversation
|
||||
import com.moez.QKSMS.data.mapper.CursorToMessage
|
||||
import com.moez.QKSMS.data.model.Recipient
|
||||
import com.moez.QKSMS.data.mapper.CursorToRecipient
|
||||
import com.moez.QKSMS.data.model.SyncLog
|
||||
import io.reactivex.Flowable
|
||||
import io.realm.Realm
|
||||
@@ -18,7 +16,8 @@ import javax.inject.Inject
|
||||
open class PartialSync @Inject constructor(
|
||||
private val context: Context,
|
||||
private val cursorToConversation: CursorToConversation,
|
||||
private val cursorToMessage: CursorToMessage)
|
||||
private val cursorToMessage: CursorToMessage,
|
||||
private val cursorToRecipient: CursorToRecipient)
|
||||
: Interactor<Long, Unit>() {
|
||||
|
||||
override fun buildObservable(params: Unit): Flowable<Long> {
|
||||
@@ -41,38 +40,39 @@ open class PartialSync @Inject constructor(
|
||||
// Add a log entry for this sync
|
||||
realm?.insert(SyncLog())
|
||||
}
|
||||
.map { contentResolver.query(CursorToConversation.URI, CursorToConversation.PROJECTION, "date >= ?", arrayOf(lastSync.toString()), "date desc") }
|
||||
.flatMap { cursor -> cursor.asFlowable().map { cursorToConversation.map(it) } }
|
||||
.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, CursorToMessage.CURSOR_PROJECTION, null, null, "date desc") }
|
||||
.flatMap { cursor ->
|
||||
val columnsMap = CursorToMessage.MessageColumns(cursor)
|
||||
cursor.asFlowable()
|
||||
.map { cursorToMessage.map(Pair(cursor, columnsMap)) }
|
||||
.flatMap {
|
||||
// Sync conversations
|
||||
contentResolver.query(CursorToConversation.URI, CursorToConversation.PROJECTION,
|
||||
"date >= ?", arrayOf(lastSync.toString()), "date desc")
|
||||
.asFlowable()
|
||||
.map { cursor -> cursorToConversation.map(cursor) }
|
||||
.toList().toFlowable()
|
||||
.doOnNext { conversations -> realm?.insertOrUpdate(conversations) }
|
||||
}
|
||||
.flatMap {
|
||||
// Sync messages
|
||||
val messageCursor = contentResolver.query(CursorToMessage.URI, CursorToMessage.PROJECTION, null, null, "normalized_date desc")
|
||||
val messageColumns = CursorToMessage.MessageColumns(messageCursor)
|
||||
messageCursor.asFlowable()
|
||||
.map { cursor -> cursorToMessage.map(Pair(cursor, messageColumns)) }
|
||||
.takeWhile { message -> message.date >= lastSync }
|
||||
.filter { message -> message.type == "sms" || message.type == "mms" }
|
||||
.toList().toFlowable()
|
||||
.doOnNext { messages -> realm?.insertOrUpdate(messages) }
|
||||
}
|
||||
.filter { message -> message.type == "sms" || message.type == "mms" }
|
||||
.doOnNext { message -> realm?.insertOrUpdate(message) }
|
||||
.count()
|
||||
.toFlowable()
|
||||
.map { contentResolver.query(Uri.parse("content://mms-sms/canonical-addresses"), null, null, null, null) }
|
||||
.flatMap { cursor -> cursor.asFlowable() }
|
||||
.map { cursor ->
|
||||
Recipient().apply {
|
||||
id = cursor.getLong(0)
|
||||
address = cursor.getString(1)
|
||||
}
|
||||
.flatMap {
|
||||
// Sync recipients
|
||||
contentResolver.query(CursorToRecipient.URI, null, null, null, null).asFlowable()
|
||||
.map { cursor -> cursorToRecipient.map(cursor) }
|
||||
.toList().toFlowable()
|
||||
.doOnNext { recipients -> realm?.insertOrUpdate(recipients) }
|
||||
}
|
||||
.doOnNext { recipient -> realm?.insertOrUpdate(recipient)}
|
||||
.count()
|
||||
.toFlowable()
|
||||
.doOnNext {
|
||||
realm?.commitTransaction()
|
||||
realm?.close()
|
||||
Timber.v("Synced $it messages in ${TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - startTime)} seconds")
|
||||
Timber.v("Performed sync in ${TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - startTime)} seconds")
|
||||
}
|
||||
.map { 0L }
|
||||
}
|
||||
|
||||
}
|
||||
@@ -117,6 +117,9 @@ class MainActivity : QkActivity<MainViewModel>(), MainView {
|
||||
})
|
||||
.show()
|
||||
}
|
||||
|
||||
/*val cursor = contentResolver.query(Uri.parse("content://mms-sms/complete-conversations"), CursorToConversation.PROJECTION, null, null, null)
|
||||
Timber.d("Total message count: ${cursor.count}")*/
|
||||
}
|
||||
|
||||
override fun onNewIntent(intent: Intent?) {
|
||||
|
||||
Reference in New Issue
Block a user