mirror of
https://github.com/quik-sms/quik.git
synced 2026-11-17 11:38:51 +01:00
Create Mapper for Conversation -> Flowable<Conversation> and Interactor for syncing conversations
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
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 javax.inject.Inject
|
||||
|
||||
class CursorToConversationFlowable @Inject constructor(val contactsRepo: ContactRepository) : Mapper<Cursor, Flowable<Conversation>> {
|
||||
|
||||
override fun map(from: Cursor): Flowable<Conversation> {
|
||||
return from.asFlowable().map { cursor ->
|
||||
Conversation().apply {
|
||||
id = cursor.getLong(ID)
|
||||
|
||||
cursor.getString(RECIPIENT_IDS).split(" ")
|
||||
.map { id -> id.toLong() }
|
||||
.map { id -> contactsRepo.getContactBlocking(id) }
|
||||
.filter { contact -> contact.recipientId != 0L }
|
||||
.forEach { contact -> contacts.add(contact) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
val URI: Uri = Uri.parse("content://mms-sms/conversations?simple=true")
|
||||
val PROJECTION = arrayOf(
|
||||
Threads._ID,
|
||||
Threads.RECIPIENT_IDS)
|
||||
|
||||
val ID = 0
|
||||
val RECIPIENT_IDS = 1
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,117 +1,118 @@
|
||||
package com.moez.QKSMS.data.mapper
|
||||
|
||||
import android.database.Cursor
|
||||
import android.provider.Telephony
|
||||
import android.provider.Telephony.*
|
||||
import com.moez.QKSMS.common.util.extensions.asFlowable
|
||||
import com.moez.QKSMS.data.model.Message
|
||||
import io.reactivex.Flowable
|
||||
import timber.log.Timber
|
||||
import javax.inject.Inject
|
||||
|
||||
object CursorToMessageFlowable {
|
||||
class CursorToMessageFlowable @Inject constructor() : Mapper<Cursor, Flowable<Message>> {
|
||||
|
||||
fun map(cursor: Cursor): Flowable<Message> {
|
||||
val messageColumns = MessageColumns(cursor)
|
||||
return cursor.asFlowable().map { cursor -> cursorToMessage(cursor, messageColumns) }
|
||||
}
|
||||
|
||||
private fun cursorToMessage(cursor: Cursor, columnsMap: MessageColumns): Message {
|
||||
return Message().apply {
|
||||
type = when (cursor.getColumnIndex(Telephony.MmsSms.TYPE_DISCRIMINATOR_COLUMN)) {
|
||||
-1 -> "sms"
|
||||
else -> cursor.getString(columnsMap.msgType)
|
||||
}
|
||||
|
||||
id = cursor.getLong(columnsMap.msgId)
|
||||
body = cursor.getString(columnsMap.smsBody) ?: ""
|
||||
|
||||
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)
|
||||
seen = cursor.getInt(columnsMap.mmsSeen) != 0
|
||||
read = cursor.getInt(columnsMap.mmsRead) != 0
|
||||
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)
|
||||
}
|
||||
|
||||
"mms" -> {
|
||||
threadId = cursor.getLong(columnsMap.mmsThreadId)
|
||||
boxId = cursor.getInt(columnsMap.mmsMessageBox)
|
||||
date = cursor.getLong(columnsMap.smsDate)
|
||||
dateSent = cursor.getLong(columnsMap.smsDateSent)
|
||||
seen = cursor.getInt(columnsMap.smsSeen) != 0
|
||||
read = cursor.getInt(columnsMap.smsRead) != 0
|
||||
errorType = cursor.getInt(columnsMap.mmsErrorType)
|
||||
id = cursor.getLong(columnsMap.msgId)
|
||||
body = cursor.getString(columnsMap.smsBody) ?: ""
|
||||
|
||||
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)
|
||||
seen = cursor.getInt(columnsMap.mmsSeen) != 0
|
||||
read = cursor.getInt(columnsMap.mmsRead) != 0
|
||||
}
|
||||
|
||||
"mms" -> {
|
||||
threadId = cursor.getLong(columnsMap.mmsThreadId)
|
||||
boxId = cursor.getInt(columnsMap.mmsMessageBox)
|
||||
date = cursor.getLong(columnsMap.smsDate)
|
||||
dateSent = cursor.getLong(columnsMap.smsDateSent)
|
||||
seen = cursor.getInt(columnsMap.smsSeen) != 0
|
||||
read = cursor.getInt(columnsMap.smsRead) != 0
|
||||
errorType = cursor.getInt(columnsMap.mmsErrorType)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val CURSOR_PROJECTION = arrayOf(
|
||||
Telephony.MmsSms.TYPE_DISCRIMINATOR_COLUMN,
|
||||
Telephony.MmsSms._ID,
|
||||
companion object {
|
||||
val CURSOR_PROJECTION = arrayOf(
|
||||
MmsSms.TYPE_DISCRIMINATOR_COLUMN,
|
||||
MmsSms._ID,
|
||||
|
||||
Telephony.Sms.THREAD_ID,
|
||||
Telephony.Sms.ADDRESS,
|
||||
Telephony.Sms.BODY,
|
||||
Telephony.Sms.DATE,
|
||||
Telephony.Sms.DATE_SENT,
|
||||
Telephony.Sms.SEEN,
|
||||
Telephony.Sms.READ,
|
||||
Telephony.Sms.TYPE,
|
||||
Telephony.Sms.STATUS,
|
||||
Telephony.Sms.LOCKED,
|
||||
Telephony.Sms.ERROR_CODE,
|
||||
Sms.THREAD_ID,
|
||||
Sms.ADDRESS,
|
||||
Sms.BODY,
|
||||
Sms.DATE,
|
||||
Sms.DATE_SENT,
|
||||
Sms.SEEN,
|
||||
Sms.READ,
|
||||
Sms.TYPE,
|
||||
Sms.STATUS,
|
||||
Sms.LOCKED,
|
||||
Sms.ERROR_CODE,
|
||||
|
||||
Telephony.Mms.THREAD_ID,
|
||||
Telephony.Mms.SUBJECT,
|
||||
Telephony.Mms.SUBJECT_CHARSET,
|
||||
Telephony.Mms.DATE,
|
||||
Telephony.Mms.DATE_SENT,
|
||||
Telephony.Mms.SEEN,
|
||||
Telephony.Mms.READ,
|
||||
Telephony.Mms.MESSAGE_TYPE,
|
||||
Telephony.Mms.MESSAGE_BOX,
|
||||
Telephony.Mms.DELIVERY_REPORT,
|
||||
Telephony.Mms.READ_REPORT,
|
||||
Telephony.MmsSms.PendingMessages.ERROR_TYPE,
|
||||
Telephony.Mms.LOCKED,
|
||||
Telephony.Mms.STATUS,
|
||||
Telephony.Mms.TEXT_ONLY)
|
||||
Mms.THREAD_ID,
|
||||
Mms.SUBJECT,
|
||||
Mms.SUBJECT_CHARSET,
|
||||
Mms.DATE,
|
||||
Mms.DATE_SENT,
|
||||
Mms.SEEN,
|
||||
Mms.READ,
|
||||
Mms.MESSAGE_TYPE,
|
||||
Mms.MESSAGE_BOX,
|
||||
Mms.DELIVERY_REPORT,
|
||||
Mms.READ_REPORT,
|
||||
MmsSms.PendingMessages.ERROR_TYPE,
|
||||
Mms.LOCKED,
|
||||
Mms.STATUS,
|
||||
Mms.TEXT_ONLY)
|
||||
}
|
||||
|
||||
private class MessageColumns(val cursor: Cursor) {
|
||||
|
||||
val msgType by lazy { getColumnIndex(Telephony.MmsSms.TYPE_DISCRIMINATOR_COLUMN) }
|
||||
val msgId by lazy { getColumnIndex(Telephony.MmsSms._ID) }
|
||||
val msgType by lazy { getColumnIndex(MmsSms.TYPE_DISCRIMINATOR_COLUMN) }
|
||||
val msgId by lazy { getColumnIndex(MmsSms._ID) }
|
||||
|
||||
val smsThreadId by lazy { getColumnIndex(Telephony.Sms.THREAD_ID) }
|
||||
val smsAddress by lazy { getColumnIndex(Telephony.Sms.ADDRESS) }
|
||||
val smsBody by lazy { getColumnIndex(Telephony.Sms.BODY) }
|
||||
val smsDate by lazy { getColumnIndex(Telephony.Sms.DATE) }
|
||||
val smsDateSent by lazy { getColumnIndex(Telephony.Sms.DATE_SENT) }
|
||||
val smsSeen by lazy { getColumnIndex(Telephony.Sms.SEEN) }
|
||||
val smsRead by lazy { getColumnIndex(Telephony.Sms.READ) }
|
||||
val smsType by lazy { getColumnIndex(Telephony.Sms.TYPE) }
|
||||
val smsStatus by lazy { getColumnIndex(Telephony.Sms.STATUS) }
|
||||
val smsLocked by lazy { getColumnIndex(Telephony.Sms.LOCKED) }
|
||||
val smsErrorCode by lazy { getColumnIndex(Telephony.Sms.ERROR_CODE) }
|
||||
val smsThreadId by lazy { getColumnIndex(Sms.THREAD_ID) }
|
||||
val smsAddress by lazy { getColumnIndex(Sms.ADDRESS) }
|
||||
val smsBody by lazy { getColumnIndex(Sms.BODY) }
|
||||
val smsDate by lazy { getColumnIndex(Sms.DATE) }
|
||||
val smsDateSent by lazy { getColumnIndex(Sms.DATE_SENT) }
|
||||
val smsSeen by lazy { getColumnIndex(Sms.SEEN) }
|
||||
val smsRead by lazy { getColumnIndex(Sms.READ) }
|
||||
val smsType by lazy { getColumnIndex(Sms.TYPE) }
|
||||
val smsStatus by lazy { getColumnIndex(Sms.STATUS) }
|
||||
val smsLocked by lazy { getColumnIndex(Sms.LOCKED) }
|
||||
val smsErrorCode by lazy { getColumnIndex(Sms.ERROR_CODE) }
|
||||
|
||||
val mmsThreadId by lazy { getColumnIndex(Telephony.Mms.THREAD_ID) }
|
||||
val mmsSubject by lazy { getColumnIndex(Telephony.Mms.SUBJECT) }
|
||||
val mmsSubjectCharset by lazy { getColumnIndex(Telephony.Mms.SUBJECT_CHARSET) }
|
||||
val mmsDate by lazy { getColumnIndex(Telephony.Mms.DATE) }
|
||||
val mmsDateSent by lazy { getColumnIndex(Telephony.Mms.DATE_SENT) }
|
||||
val mmsSeen by lazy { getColumnIndex(Telephony.Mms.SEEN) }
|
||||
val mmsRead by lazy { getColumnIndex(Telephony.Mms.READ) }
|
||||
val mmsMessageType by lazy { getColumnIndex(Telephony.Mms.MESSAGE_TYPE) }
|
||||
val mmsMessageBox by lazy { getColumnIndex(Telephony.Mms.MESSAGE_BOX) }
|
||||
val mmsDeliveryReport by lazy { getColumnIndex(Telephony.Mms.DELIVERY_REPORT) }
|
||||
val mmsReadReport by lazy { getColumnIndex(Telephony.Mms.READ_REPORT) }
|
||||
val mmsErrorType by lazy { getColumnIndex(Telephony.MmsSms.PendingMessages.ERROR_TYPE) }
|
||||
val mmsLocked by lazy { getColumnIndex(Telephony.Mms.LOCKED) }
|
||||
val mmsStatus by lazy { getColumnIndex(Telephony.Mms.STATUS) }
|
||||
val mmsTextOnly by lazy { getColumnIndex(Telephony.Mms.TEXT_ONLY) }
|
||||
val mmsThreadId by lazy { getColumnIndex(Mms.THREAD_ID) }
|
||||
val mmsSubject by lazy { getColumnIndex(Mms.SUBJECT) }
|
||||
val mmsSubjectCharset by lazy { getColumnIndex(Mms.SUBJECT_CHARSET) }
|
||||
val mmsDate by lazy { getColumnIndex(Mms.DATE) }
|
||||
val mmsDateSent by lazy { getColumnIndex(Mms.DATE_SENT) }
|
||||
val mmsSeen by lazy { getColumnIndex(Mms.SEEN) }
|
||||
val mmsRead by lazy { getColumnIndex(Mms.READ) }
|
||||
val mmsMessageType by lazy { getColumnIndex(Mms.MESSAGE_TYPE) }
|
||||
val mmsMessageBox by lazy { getColumnIndex(Mms.MESSAGE_BOX) }
|
||||
val mmsDeliveryReport by lazy { getColumnIndex(Mms.DELIVERY_REPORT) }
|
||||
val mmsReadReport by lazy { getColumnIndex(Mms.READ_REPORT) }
|
||||
val mmsErrorType by lazy { getColumnIndex(MmsSms.PendingMessages.ERROR_TYPE) }
|
||||
val mmsLocked by lazy { getColumnIndex(Mms.LOCKED) }
|
||||
val mmsStatus by lazy { getColumnIndex(Mms.STATUS) }
|
||||
val mmsTextOnly by lazy { getColumnIndex(Mms.TEXT_ONLY) }
|
||||
|
||||
private fun getColumnIndex(columnsName: String): Int {
|
||||
return try {
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.moez.QKSMS.data.mapper
|
||||
|
||||
interface Mapper<in From, out To> {
|
||||
|
||||
fun map(from: From): To
|
||||
|
||||
}
|
||||
@@ -126,7 +126,7 @@ class MessageRepository @Inject constructor(private val context: Context) {
|
||||
|
||||
fun addMessageFromUri(uri: Uri) {
|
||||
val cursor = context.contentResolver.query(uri, null, null, null, "date DESC")
|
||||
CursorToMessageFlowable.map(cursor)
|
||||
CursorToMessageFlowable().map(cursor)
|
||||
.subscribe { message -> message.insertOrUpdate() }
|
||||
}
|
||||
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
package com.moez.QKSMS.data.sync
|
||||
|
||||
import android.net.Uri
|
||||
|
||||
internal object ConversationColumns {
|
||||
|
||||
val URI: Uri = Uri.parse("content://mms-sms/conversations?simple=true")
|
||||
val PROJECTION = arrayOf(
|
||||
android.provider.Telephony.Threads._ID,
|
||||
android.provider.Telephony.Threads.RECIPIENT_IDS)
|
||||
|
||||
val ID = 0
|
||||
val RECIPIENT_IDS = 1
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package com.moez.QKSMS.data.sync
|
||||
|
||||
import android.net.Uri
|
||||
|
||||
class RecipientColumns {
|
||||
|
||||
companion object {
|
||||
val URI = Uri.parse("content://mms-sms/canonical-address/")
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
package com.moez.QKSMS.data.sync
|
||||
|
||||
import android.content.Context
|
||||
import android.database.Cursor
|
||||
import android.net.Uri
|
||||
import android.provider.Telephony
|
||||
import com.moez.QKSMS.common.util.extensions.asFlowable
|
||||
import com.moez.QKSMS.data.mapper.CursorToMessageFlowable
|
||||
import com.moez.QKSMS.data.model.Conversation
|
||||
import com.moez.QKSMS.data.repository.ContactRepository
|
||||
import io.reactivex.Flowable
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||
import io.reactivex.schedulers.Schedulers
|
||||
import io.realm.Realm
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class SyncManager @Inject constructor(val context: Context, private val contactsRepo: ContactRepository) {
|
||||
|
||||
fun copyToRealm(completionListener: () -> Unit) {
|
||||
val contentResolver = context.contentResolver
|
||||
val conversationsCursor = contentResolver.query(ConversationColumns.URI, ConversationColumns.PROJECTION, null, null, "date desc")
|
||||
|
||||
var realm: Realm? = null
|
||||
|
||||
Flowable.just(conversationsCursor)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.doOnNext {
|
||||
// We need to set up realm on the io thread, and doOnSubscribe doesn't support setting a custom Scheduler
|
||||
realm = Realm.getDefaultInstance()
|
||||
realm?.beginTransaction()
|
||||
realm?.deleteAll()
|
||||
}
|
||||
.flatMap { cursor -> cursor.asFlowable() }
|
||||
.map { cursor -> conversationFromCursor(cursor) }
|
||||
.distinct { conversation -> conversation.id }
|
||||
.doOnNext { conversation -> realm?.insertOrUpdate(conversation) }
|
||||
.map { conversation -> conversation.id }
|
||||
.flatMap { threadId ->
|
||||
val uri = Uri.withAppendedPath(Telephony.MmsSms.CONTENT_CONVERSATIONS_URI, threadId.toString())
|
||||
val messagesCursor = contentResolver.query(uri, CursorToMessageFlowable.CURSOR_PROJECTION, null, null, "date desc")
|
||||
CursorToMessageFlowable.map(messagesCursor)
|
||||
}
|
||||
.filter { message -> message.type == "sms" || message.type == "mms" }
|
||||
.distinct { message -> message.id }
|
||||
.doOnNext { message -> realm?.insertOrUpdate(message) }
|
||||
.count()
|
||||
.toFlowable()
|
||||
.doOnNext {
|
||||
realm?.commitTransaction()
|
||||
realm?.close()
|
||||
}
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe { completionListener.invoke() }
|
||||
}
|
||||
|
||||
private fun conversationFromCursor(cursor: Cursor): Conversation {
|
||||
return Conversation().apply {
|
||||
id = cursor.getLong(ConversationColumns.ID)
|
||||
|
||||
cursor.getString(ConversationColumns.RECIPIENT_IDS).split(" ")
|
||||
.map { id -> id.toLong() }
|
||||
.map { id -> contactsRepo.getContactBlocking(id) }
|
||||
.filter { contact -> contact.recipientId != 0L }
|
||||
.forEach { contact -> contacts.add(contact) }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
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 io.reactivex.Flowable
|
||||
import io.realm.Realm
|
||||
import javax.inject.Inject
|
||||
|
||||
class SyncConversations @Inject constructor(
|
||||
private val context: Context,
|
||||
private val cursorToConversationFlowable: CursorToConversationFlowable,
|
||||
private val cursorToMessageFlowable: CursorToMessageFlowable) : 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")
|
||||
|
||||
var realm: Realm? = null
|
||||
|
||||
return Flowable.just(conversationsCursor)
|
||||
.doOnNext {
|
||||
// We need to set up realm on the io thread, and doOnSubscribe doesn't support setting a custom Scheduler
|
||||
realm = Realm.getDefaultInstance()
|
||||
realm?.beginTransaction()
|
||||
realm?.deleteAll()
|
||||
}
|
||||
.flatMap { cursor -> cursorToConversationFlowable.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) }
|
||||
.filter { message -> message.type == "sms" || message.type == "mms" }
|
||||
.distinct { message -> message.id }
|
||||
.doOnNext { message -> realm?.insertOrUpdate(message) }
|
||||
.count()
|
||||
.toFlowable()
|
||||
.doOnNext {
|
||||
realm?.commitTransaction()
|
||||
realm?.close()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+4
-2
@@ -7,14 +7,15 @@ import com.moez.QKSMS.data.model.Message
|
||||
import com.moez.QKSMS.data.repository.MessageRepository
|
||||
import com.moez.QKSMS.data.sync.SyncManager
|
||||
import com.moez.QKSMS.domain.interactor.MarkAllSeen
|
||||
import com.moez.QKSMS.domain.interactor.SyncConversations
|
||||
import io.reactivex.subjects.PublishSubject
|
||||
import io.realm.RealmResults
|
||||
import javax.inject.Inject
|
||||
|
||||
class ConversationListViewModel : ViewModel() {
|
||||
|
||||
@Inject lateinit var syncManager: SyncManager
|
||||
@Inject lateinit var messageRepo: MessageRepository
|
||||
@Inject lateinit var syncConversations: SyncConversations
|
||||
@Inject lateinit var markAllSeen: MarkAllSeen
|
||||
|
||||
val state: MutableLiveData<ConversationListViewState> = MutableLiveData()
|
||||
@@ -37,11 +38,12 @@ class ConversationListViewModel : ViewModel() {
|
||||
|
||||
fun onRefresh() {
|
||||
partialStates.onNext(PartialState.Refreshing(true))
|
||||
syncManager.copyToRealm { partialStates.onNext(PartialState.Refreshing(false)) }
|
||||
syncConversations.execute({ partialStates.onNext(PartialState.Refreshing(false)) }, Unit)
|
||||
}
|
||||
|
||||
override fun onCleared() {
|
||||
super.onCleared()
|
||||
syncConversations.dispose()
|
||||
markAllSeen.dispose()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user