Sync contacts during message sync instead of after

This commit is contained in:
Moez Bhatti
2018-05-18 13:26:28 -04:00
parent 1ad764e87f
commit 43ef8bd0f6
@@ -81,6 +81,7 @@ class SyncRepositoryImpl @Inject constructor(
.findAll()
.map { PersistedData(it.id, it.archived, it.blocked) }
realm.delete(Contact::class.java)
realm.delete(Conversation::class.java)
realm.delete(Message::class.java)
realm.delete(MmsPart::class.java)
@@ -120,7 +121,16 @@ class SyncRepositoryImpl @Inject constructor(
// Sync recipients
cursorToRecipient.getRecipientCursor()?.use { recipientCursor ->
val recipients = recipientCursor.map { cursor -> cursorToRecipient.map(cursor) }
val contacts = realm.copyToRealm(getContacts())
val recipients = recipientCursor
.map { cursor -> cursorToRecipient.map(cursor) }
.map { recipient ->
recipient.apply {
contact = contacts.firstOrNull { contact ->
contact.numbers.any { PhoneNumberUtils.compare(recipient.address, it.address) }
}
}
}
realm.insertOrUpdate(recipients)
}
@@ -132,8 +142,6 @@ class SyncRepositoryImpl @Inject constructor(
// Only delete this after the sync has successfully completed
oldBlockedSenders.delete()
syncContacts()
syncProgress.onNext(SyncRepository.SyncProgress.Idle())
}
@@ -183,16 +191,7 @@ class SyncRepositoryImpl @Inject constructor(
override fun syncContacts() {
// Load all the contacts
var contacts = cursorToContact.getContactsCursor()
?.map { cursor -> cursorToContact.map(cursor) }
?.groupBy { contact -> contact.lookupKey }
?.map { contacts ->
val allNumbers = contacts.value.map { it.numbers }.flatten()
contacts.value.first().apply {
numbers.clear()
numbers.addAll(allNumbers)
}
} ?: listOf()
var contacts = getContacts()
Realm.getDefaultInstance()?.use { realm ->
val recipients = realm.where(Recipient::class.java).findAll()
@@ -217,4 +216,17 @@ class SyncRepositoryImpl @Inject constructor(
}
}
private fun getContacts(): List<Contact> {
return cursorToContact.getContactsCursor()
?.map { cursor -> cursorToContact.map(cursor) }
?.groupBy { contact -> contact.lookupKey }
?.map { contacts ->
val allNumbers = contacts.value.map { it.numbers }.flatten()
contacts.value.first().apply {
numbers.clear()
numbers.addAll(allNumbers)
}
} ?: listOf()
}
}