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(" "))
|
||||
.map { id -> id.toLong() }
|
||||
.map { id -> contacts.getContact(id) }
|
||||
.map { id -> contacts.getContactBlocking(id) }
|
||||
.filter { contact -> contact.recipientId != 0L }
|
||||
.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
|
||||
* execute off of the main thread. At the time of writing
|
||||
*/
|
||||
fun getContact(recipientId: Long): Contact {
|
||||
var contact: Contact? = getContactFromRealm(recipientId)
|
||||
if (contact == null) {
|
||||
contact = getContactFromContentProvider(recipientId)
|
||||
}
|
||||
|
||||
return contact ?: Contact()
|
||||
}
|
||||
|
||||
private fun getContactFromRealm(recipientId: Long): Contact? {
|
||||
fun getContactBlocking(recipientId: Long): Contact {
|
||||
// First, try loading the conversation from Realm
|
||||
val realm = Realm.getDefaultInstance()
|
||||
val results = realm.where(Contact::class.java)
|
||||
.equalTo("recipientId", recipientId)
|
||||
.findAll()
|
||||
|
||||
if (results.size > 0) {
|
||||
realm.close()
|
||||
return results[0]
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
private fun getContactFromContentProvider(recipientId: Long): Contact? {
|
||||
// If it's not found, then load from the Contacts ContentProvider instead
|
||||
var contact: Contact? = 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()
|
||||
}
|
||||
|
||||
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)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.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?.beginTransaction()
|
||||
realm?.deleteAll()
|
||||
|
||||
Reference in New Issue
Block a user