forked from upstream-mirrors/quik
@@ -23,33 +23,28 @@ import android.database.ContentObserver
|
||||
import android.net.Uri
|
||||
import android.os.Handler
|
||||
import android.provider.ContactsContract
|
||||
import com.moez.QKSMS.repository.SyncRepository
|
||||
import io.reactivex.Observable
|
||||
import io.reactivex.subjects.PublishSubject
|
||||
import io.reactivex.subjects.BehaviorSubject
|
||||
import javax.inject.Inject
|
||||
|
||||
/**
|
||||
* Listens for a contact being added, and then syncs it to Realm
|
||||
*
|
||||
* TODO: Stop listening automatically. Currently, this will only happen if the contact is added
|
||||
*/
|
||||
class ContactAddedListenerImpl @Inject constructor(
|
||||
private val context: Context,
|
||||
private val syncRepo: SyncRepository
|
||||
private val context: Context
|
||||
) : ContactAddedListener {
|
||||
|
||||
companion object {
|
||||
private val URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI
|
||||
}
|
||||
|
||||
override fun listen(address: String): Observable<*> {
|
||||
override fun listen(): Observable<*> {
|
||||
return ContactContentObserver(context).observable
|
||||
.filter { syncRepo.syncContact(address) }
|
||||
}
|
||||
|
||||
private class ContactContentObserver(context: Context) : ContentObserver(Handler()) {
|
||||
|
||||
private val subject = PublishSubject.create<Unit>()
|
||||
private val subject = BehaviorSubject.createDefault<Unit>(Unit)
|
||||
|
||||
val observable: Observable<Unit> = subject
|
||||
.doOnSubscribe { context.contentResolver.registerContentObserver(URI, true, this) }
|
||||
@@ -66,4 +61,4 @@ class ContactAddedListenerImpl @Inject constructor(
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user