forked from upstream-mirrors/quik
Sync starred contacts and groups
This commit is contained in:
@@ -27,10 +27,13 @@ import com.moez.QKSMS.extensions.insertOrUpdate
|
||||
import com.moez.QKSMS.extensions.map
|
||||
import com.moez.QKSMS.manager.KeyManager
|
||||
import com.moez.QKSMS.mapper.CursorToContact
|
||||
import com.moez.QKSMS.mapper.CursorToContactGroup
|
||||
import com.moez.QKSMS.mapper.CursorToContactGroupMember
|
||||
import com.moez.QKSMS.mapper.CursorToConversation
|
||||
import com.moez.QKSMS.mapper.CursorToMessage
|
||||
import com.moez.QKSMS.mapper.CursorToRecipient
|
||||
import com.moez.QKSMS.model.Contact
|
||||
import com.moez.QKSMS.model.ContactGroup
|
||||
import com.moez.QKSMS.model.Conversation
|
||||
import com.moez.QKSMS.model.Message
|
||||
import com.moez.QKSMS.model.MmsPart
|
||||
@@ -53,6 +56,8 @@ class SyncRepositoryImpl @Inject constructor(
|
||||
private val cursorToMessage: CursorToMessage,
|
||||
private val cursorToRecipient: CursorToRecipient,
|
||||
private val cursorToContact: CursorToContact,
|
||||
private val cursorToContactGroup: CursorToContactGroup,
|
||||
private val cursorToContactGroupMember: CursorToContactGroupMember,
|
||||
private val keys: KeyManager,
|
||||
private val phoneNumberUtils: PhoneNumberUtils,
|
||||
private val rxPrefs: RxSharedPreferences
|
||||
@@ -89,6 +94,7 @@ class SyncRepositoryImpl @Inject constructor(
|
||||
.toMutableMap()
|
||||
|
||||
realm.delete(Contact::class.java)
|
||||
realm.delete(ContactGroup::class.java)
|
||||
realm.delete(Conversation::class.java)
|
||||
realm.delete(Message::class.java)
|
||||
realm.delete(MmsPart::class.java)
|
||||
@@ -234,19 +240,19 @@ class SyncRepositoryImpl @Inject constructor(
|
||||
|
||||
realm.executeTransaction {
|
||||
realm.delete(Contact::class.java)
|
||||
realm.delete(ContactGroup::class.java)
|
||||
|
||||
contacts = realm.copyToRealm(contacts)
|
||||
realm.insertOrUpdate(getContactGroups(contacts))
|
||||
|
||||
// Update all the recipients with the new contacts
|
||||
val updatedRecipients = recipients.map { recipient ->
|
||||
recipient.apply {
|
||||
contact = contacts.firstOrNull {
|
||||
it.numbers.any { phoneNumberUtils.compare(recipient.address, it.address) }
|
||||
}
|
||||
recipients.forEach { recipient ->
|
||||
recipient.contact = contacts.find { contact ->
|
||||
contact.numbers.any { phoneNumberUtils.compare(recipient.address, it.address) }
|
||||
}
|
||||
}
|
||||
|
||||
realm.insertOrUpdate(updatedRecipients)
|
||||
realm.insertOrUpdate(recipients)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -254,26 +260,24 @@ class SyncRepositoryImpl @Inject constructor(
|
||||
|
||||
override fun syncContact(address: String): Boolean {
|
||||
// See if there's a contact that matches this phone number
|
||||
var contact = getContacts().firstOrNull {
|
||||
it.numbers.any { number -> phoneNumberUtils.compare(number.address, address) }
|
||||
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()
|
||||
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
|
||||
val updatedRecipients = recipients
|
||||
.filter { recipient ->
|
||||
contact.numbers.any { number ->
|
||||
phoneNumberUtils.compare(recipient.address, number.address)
|
||||
}
|
||||
}
|
||||
.map { recipient -> recipient.apply { this.contact = contact } }
|
||||
recipients.forEach { recipient -> recipient.contact = contact }
|
||||
|
||||
realm.insertOrUpdate(updatedRecipients)
|
||||
realm.insertOrUpdate(recipients)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -293,4 +297,22 @@ class SyncRepositoryImpl @Inject constructor(
|
||||
} ?: listOf()
|
||||
}
|
||||
|
||||
}
|
||||
private fun getContactGroups(contacts: List<Contact>): List<ContactGroup> {
|
||||
val groupMembers = cursorToContactGroupMember.getGroupMembersCursor()
|
||||
?.map(cursorToContactGroupMember::map)
|
||||
.orEmpty()
|
||||
|
||||
val groups = cursorToContactGroup.getContactGroupsCursor()
|
||||
?.map(cursorToContactGroup::map)
|
||||
.orEmpty()
|
||||
|
||||
groups.forEach { group ->
|
||||
group.contacts.addAll(groupMembers
|
||||
.filter { member -> member.groupId == group.id }
|
||||
.mapNotNull { member -> contacts.find { contact -> contact.lookupKey == member.lookupKey } })
|
||||
}
|
||||
|
||||
return groups
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user