Keep contacts in sync

Fixes #329
This commit is contained in:
Moez Bhatti
2019-11-24 15:30:25 -05:00
parent 25cff7e346
commit 13c8ba3e78
10 changed files with 27 additions and 75 deletions
@@ -65,7 +65,7 @@ class SyncRepositoryImpl @Inject constructor(
) : SyncRepository {
override val syncProgress: Subject<SyncRepository.SyncProgress> =
BehaviorSubject.createDefault(SyncRepository.SyncProgress.Idle())
BehaviorSubject.createDefault(SyncRepository.SyncProgress.Idle)
override fun syncMessages() {
@@ -185,7 +185,7 @@ class SyncRepositoryImpl @Inject constructor(
// Only delete this after the sync has successfully completed
oldBlockedSenders.delete()
syncProgress.onNext(SyncRepository.SyncProgress.Idle())
syncProgress.onNext(SyncRepository.SyncProgress.Idle)
}
override fun syncMessage(uri: Uri): Message? {
@@ -259,32 +259,6 @@ class SyncRepositoryImpl @Inject constructor(
}
}
override fun syncContact(address: String): Boolean {
// See if there's a contact that matches this phone number
var contact = getContacts().find { contact ->
contact.numbers.any { number -> phoneNumberUtils.compare(number.address, address) }
} ?: return false
Realm.getDefaultInstance().use { realm ->
val recipients = realm.where(Recipient::class.java).findAll().filter { recipient ->
contact.numbers.any { number ->
phoneNumberUtils.compare(recipient.address, number.address)
}
}
realm.executeTransaction {
contact = realm.copyToRealmOrUpdate(contact)
// Update all the matching recipients with the new contact
recipients.forEach { recipient -> recipient.contact = contact }
realm.insertOrUpdate(recipients)
}
}
return true
}
private fun getContacts(): List<Contact> {
val defaultNumberIds = Realm.getDefaultInstance().use { realm ->
realm.where(PhoneNumber::class.java)