mirror of
https://github.com/quik-sms/quik.git
synced 2026-11-17 19:38:52 +01:00
Avoid duplicate contacts, update recipient contact on sync
This commit is contained in:
@@ -20,11 +20,11 @@ package data.mapper
|
||||
|
||||
import android.database.Cursor
|
||||
import android.net.Uri
|
||||
import android.telephony.PhoneNumberUtils
|
||||
import data.model.Recipient
|
||||
import data.repository.ContactRepository
|
||||
import javax.inject.Inject
|
||||
|
||||
class CursorToRecipient @Inject constructor(private val contactRepository: ContactRepository) : Mapper<Cursor, Recipient> {
|
||||
class CursorToRecipient @Inject constructor() : Mapper<Cursor, Recipient> {
|
||||
|
||||
companion object {
|
||||
val URI = Uri.parse("content://mms-sms/canonical-addresses")
|
||||
@@ -35,8 +35,7 @@ class CursorToRecipient @Inject constructor(private val contactRepository: Conta
|
||||
|
||||
override fun map(from: Cursor) = Recipient().apply {
|
||||
id = from.getLong(COLUMN_ID)
|
||||
address = from.getString(COLUMN_ADDRESS)
|
||||
contact = contactRepository.getContactBlocking(address) // TODO do this lazily for faster syncs
|
||||
address = PhoneNumberUtils.stripSeparators(from.getString(COLUMN_ADDRESS))
|
||||
lastUpdate = System.currentTimeMillis()
|
||||
}
|
||||
|
||||
|
||||
@@ -20,17 +20,14 @@ package data.repository
|
||||
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import android.provider.BaseColumns
|
||||
import android.provider.ContactsContract
|
||||
import common.util.extensions.asFlowable
|
||||
import data.model.Contact
|
||||
import data.model.PhoneNumber
|
||||
import io.reactivex.Flowable
|
||||
import io.reactivex.Single
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||
import io.reactivex.schedulers.Schedulers
|
||||
import io.realm.Realm
|
||||
import timber.log.Timber
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@@ -59,38 +56,4 @@ class ContactRepository @Inject constructor(val context: Context) {
|
||||
.observeOn(Schedulers.io())
|
||||
}
|
||||
|
||||
fun getContactBlocking(address: String): Contact? {
|
||||
if (address.isEmpty()) return null
|
||||
|
||||
val realm = Realm.getDefaultInstance()
|
||||
var contact = realm.where(Contact::class.java)
|
||||
.equalTo("numbers.address", address)
|
||||
.findFirst()
|
||||
|
||||
if (contact == null) {
|
||||
val contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address))
|
||||
val projection = arrayOf(BaseColumns._ID, ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup.LOOKUP_KEY)
|
||||
try {
|
||||
context.contentResolver.query(contactUri, projection, null, null, null).use { contactCursor ->
|
||||
if (contactCursor.moveToFirst()) {
|
||||
contact = Contact().apply {
|
||||
numbers.add(PhoneNumber().apply { this.address = address })
|
||||
name = contactCursor.getString(contactCursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME)).orEmpty()
|
||||
lookupKey = contactCursor.getString(contactCursor.getColumnIndex(ContactsContract.PhoneLookup.LOOKUP_KEY)).orEmpty()
|
||||
lastUpdate = System.currentTimeMillis()
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Timber.w(e)
|
||||
}
|
||||
} else {
|
||||
contact = realm.copyFromRealm(contact)
|
||||
}
|
||||
|
||||
realm.close()
|
||||
|
||||
return contact
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user