forked from upstream-mirrors/quik
Make contact retrieval by recipientId async
This commit is contained in:
@@ -7,21 +7,24 @@ import com.moez.QKSMS.common.util.extensions.asFlowable
|
|||||||
import com.moez.QKSMS.data.model.Conversation
|
import com.moez.QKSMS.data.model.Conversation
|
||||||
import com.moez.QKSMS.data.repository.ContactRepository
|
import com.moez.QKSMS.data.repository.ContactRepository
|
||||||
import io.reactivex.Flowable
|
import io.reactivex.Flowable
|
||||||
|
import io.reactivex.rxkotlin.toFlowable
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class CursorToConversationFlowable @Inject constructor(val contactsRepo: ContactRepository) : Mapper<Cursor, Flowable<Conversation>> {
|
class CursorToConversationFlowable @Inject constructor(private val contactsRepo: ContactRepository) : Mapper<Cursor, Flowable<Conversation>> {
|
||||||
|
|
||||||
override fun map(from: Cursor): Flowable<Conversation> {
|
override fun map(from: Cursor): Flowable<Conversation> {
|
||||||
return from.asFlowable().map { cursor ->
|
return from.asFlowable().map { cursor ->
|
||||||
Conversation().apply {
|
val conversation = Conversation()
|
||||||
id = cursor.getLong(ID)
|
|
||||||
|
|
||||||
cursor.getString(RECIPIENT_IDS).split(" ")
|
conversation.id = cursor.getLong(ID)
|
||||||
.map { id -> id.toLong() }
|
|
||||||
.map { id -> contactsRepo.getContactBlocking(id) }
|
conversation.contacts.addAll(cursor.getString(RECIPIENT_IDS).split(" ").toFlowable()
|
||||||
.filter { contact -> contact.recipientId != 0L }
|
.map { id -> id.toLong() }
|
||||||
.forEach { contact -> contacts.add(contact) }
|
.flatMap { id -> contactsRepo.getContact(id) }
|
||||||
}
|
.filter { contact -> contact.recipientId != 0L }
|
||||||
|
.blockingIterable())
|
||||||
|
|
||||||
|
conversation
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import android.net.Uri
|
|||||||
import android.provider.BaseColumns
|
import android.provider.BaseColumns
|
||||||
import android.provider.ContactsContract
|
import android.provider.ContactsContract
|
||||||
import com.moez.QKSMS.data.model.Contact
|
import com.moez.QKSMS.data.model.Contact
|
||||||
|
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.schedulers.Schedulers
|
import io.reactivex.schedulers.Schedulers
|
||||||
@@ -23,23 +24,25 @@ class ContactRepository @Inject constructor(val context: Context) {
|
|||||||
*/
|
*/
|
||||||
private val URI = Uri.parse("content://mms-sms/canonical-address")
|
private val URI = Uri.parse("content://mms-sms/canonical-address")
|
||||||
|
|
||||||
/**
|
fun getContact(recipientId: Long): Flowable<Contact> {
|
||||||
* This function is not asynchronous, and potentially very slow. Make sure to
|
return Flowable.fromPublisher<Contact> { emitter ->
|
||||||
* execute off of the main thread. At the time of writing
|
val contact = getContactFromRealm(recipientId) ?: getContactFromDb(recipientId)
|
||||||
*/
|
contact?.let { emitter.onNext(it) }
|
||||||
fun getContactBlocking(recipientId: Long): Contact {
|
emitter.onComplete()
|
||||||
// First, try loading the conversation from Realm
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getContactFromRealm(recipientId: Long): Contact? {
|
||||||
val realm = Realm.getDefaultInstance()
|
val realm = Realm.getDefaultInstance()
|
||||||
val results = realm.where(Contact::class.java)
|
val results = realm.where(Contact::class.java)
|
||||||
.equalTo("recipientId", recipientId)
|
.equalTo("recipientId", recipientId)
|
||||||
.findAll()
|
.findAll()
|
||||||
|
|
||||||
if (results.size > 0) {
|
realm.close()
|
||||||
realm.close()
|
return results.getOrNull(0)
|
||||||
return results[0]
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// If it's not found, then load from the Contacts ContentProvider instead
|
private fun getContactFromDb(recipientId: Long): Contact? {
|
||||||
var contact: Contact? = null
|
var contact: Contact? = null
|
||||||
|
|
||||||
val recipientCursor = context.contentResolver.query(Uri.withAppendedPath(URI, recipientId.toString()), null, null, null, null)
|
val recipientCursor = context.contentResolver.query(Uri.withAppendedPath(URI, recipientId.toString()), null, null, null, null)
|
||||||
@@ -51,7 +54,8 @@ class ContactRepository @Inject constructor(val context: Context) {
|
|||||||
val contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(contact?.address))
|
val contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(contact?.address))
|
||||||
var contactCursor: Cursor? = null
|
var contactCursor: Cursor? = null
|
||||||
try {
|
try {
|
||||||
contactCursor = context.contentResolver.query(contactUri, arrayOf(BaseColumns._ID, ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.Data.PHOTO_THUMBNAIL_URI), null, null, null)
|
val projection = arrayOf(BaseColumns._ID, ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.Data.PHOTO_THUMBNAIL_URI)
|
||||||
|
contactCursor = context.contentResolver.query(contactUri, projection, null, null, null)
|
||||||
if (contactCursor.moveToFirst()) {
|
if (contactCursor.moveToFirst()) {
|
||||||
contact?.name = contactCursor.getString(contactCursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME))
|
contact?.name = contactCursor.getString(contactCursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME))
|
||||||
contact?.photoUri = contactCursor.getString(contactCursor.getColumnIndex(ContactsContract.Data.PHOTO_THUMBNAIL_URI))
|
contact?.photoUri = contactCursor.getString(contactCursor.getColumnIndex(ContactsContract.Data.PHOTO_THUMBNAIL_URI))
|
||||||
@@ -61,13 +65,7 @@ class ContactRepository @Inject constructor(val context: Context) {
|
|||||||
contactCursor?.close()
|
contactCursor?.close()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (contact != null) {
|
return contact
|
||||||
realm.close()
|
|
||||||
return contact
|
|
||||||
}
|
|
||||||
|
|
||||||
// If it's still not found then return an empty Contact object
|
|
||||||
return Contact()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getContactFromCache(address: String): Contact? {
|
fun getContactFromCache(address: String): Contact? {
|
||||||
|
|||||||
Reference in New Issue
Block a user