forked from upstream-mirrors/quik
use single function for synchronously obtaining contact
This commit is contained in:
@@ -30,7 +30,7 @@ open class Conversation() : RealmObject() {
|
|||||||
|
|
||||||
Flowable.fromIterable(cursor.getString(ConversationColumns.RECIPIENT_IDS).split(" "))
|
Flowable.fromIterable(cursor.getString(ConversationColumns.RECIPIENT_IDS).split(" "))
|
||||||
.map { id -> id.toLong() }
|
.map { id -> id.toLong() }
|
||||||
.map { id -> contacts.getContact(id) }
|
.map { id -> contacts.getContactBlocking(id) }
|
||||||
.filter { contact -> contact.recipientId != 0L }
|
.filter { contact -> contact.recipientId != 0L }
|
||||||
.blockingSubscribe { contact -> this.contacts.add(contact) }
|
.blockingSubscribe { contact -> this.contacts.add(contact) }
|
||||||
|
|
||||||
|
|||||||
@@ -21,29 +21,19 @@ class ContactRepository(val context: Context) {
|
|||||||
* This function is not asynchronous, and potentially very slow. Make sure to
|
* This function is not asynchronous, and potentially very slow. Make sure to
|
||||||
* execute off of the main thread. At the time of writing
|
* execute off of the main thread. At the time of writing
|
||||||
*/
|
*/
|
||||||
fun getContact(recipientId: Long): Contact {
|
fun getContactBlocking(recipientId: Long): Contact {
|
||||||
var contact: Contact? = getContactFromRealm(recipientId)
|
// First, try loading the conversation from Realm
|
||||||
if (contact == null) {
|
|
||||||
contact = getContactFromContentProvider(recipientId)
|
|
||||||
}
|
|
||||||
|
|
||||||
return contact ?: Contact()
|
|
||||||
}
|
|
||||||
|
|
||||||
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) {
|
if (results.size > 0) {
|
||||||
|
realm.close()
|
||||||
return results[0]
|
return results[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
return null
|
// If it's not found, then load from the Contacts ContentProvider instead
|
||||||
}
|
|
||||||
|
|
||||||
private fun getContactFromContentProvider(recipientId: Long): Contact? {
|
|
||||||
var contact: Contact? = null
|
var contact: Contact? = null
|
||||||
|
|
||||||
val recipientCursor = context.contentResolver.query(ContentUris.withAppendedId(URI, recipientId), null, null, null, null)
|
val recipientCursor = context.contentResolver.query(ContentUris.withAppendedId(URI, recipientId), null, null, null, null)
|
||||||
@@ -64,7 +54,13 @@ class ContactRepository(val context: Context) {
|
|||||||
contactCursor?.close()
|
contactCursor?.close()
|
||||||
}
|
}
|
||||||
|
|
||||||
return contact
|
if (contact != null) {
|
||||||
|
realm.close()
|
||||||
|
return contact
|
||||||
|
}
|
||||||
|
|
||||||
|
// If it's still not found then return an empty Contact object
|
||||||
|
return Contact()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -24,7 +24,7 @@ class SyncManager(val context: Context, val contacts: ContactRepository) {
|
|||||||
Flowable.just(conversationsCursor)
|
Flowable.just(conversationsCursor)
|
||||||
.subscribeOn(Schedulers.io())
|
.subscribeOn(Schedulers.io())
|
||||||
.doOnNext {
|
.doOnNext {
|
||||||
// We need to setup realm on the io thread, and doOnSubscribe doesn't support setting a custom Scheduler
|
// We need to set up realm on the io thread, and doOnSubscribe doesn't support setting a custom Scheduler
|
||||||
realm = Realm.getDefaultInstance()
|
realm = Realm.getDefaultInstance()
|
||||||
realm?.beginTransaction()
|
realm?.beginTransaction()
|
||||||
realm?.deleteAll()
|
realm?.deleteAll()
|
||||||
|
|||||||
Reference in New Issue
Block a user