forked from upstream-mirrors/quik
Sync contacts when composing a new message
This commit is contained in:
@@ -0,0 +1,30 @@
|
|||||||
|
package com.moez.QKSMS.data.mapper
|
||||||
|
|
||||||
|
import android.database.Cursor
|
||||||
|
import android.provider.ContactsContract
|
||||||
|
import com.moez.QKSMS.data.model.Contact
|
||||||
|
import com.moez.QKSMS.data.model.PhoneNumber
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
class CursorToContact @Inject constructor() : Mapper<Cursor, Contact> {
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
val URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI
|
||||||
|
val PROJECTION = arrayOf(
|
||||||
|
ContactsContract.CommonDataKinds.Phone.LOOKUP_KEY,
|
||||||
|
ContactsContract.CommonDataKinds.Phone.NUMBER,
|
||||||
|
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)
|
||||||
|
|
||||||
|
val LOOKUP_KEY = 0
|
||||||
|
val NUMBER = 1
|
||||||
|
val DISPLAY_NAME = 2
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun map(from: Cursor) = Contact().apply {
|
||||||
|
lookupKey = from.getString(LOOKUP_KEY)
|
||||||
|
name = from.getString(DISPLAY_NAME)
|
||||||
|
numbers.add(PhoneNumber().apply { address = from.getString(NUMBER) })
|
||||||
|
lastUpdate = System.currentTimeMillis()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -2,10 +2,11 @@ package com.moez.QKSMS.data.model
|
|||||||
|
|
||||||
import io.realm.RealmList
|
import io.realm.RealmList
|
||||||
import io.realm.RealmObject
|
import io.realm.RealmObject
|
||||||
|
import io.realm.annotations.PrimaryKey
|
||||||
|
|
||||||
open class Contact : RealmObject() {
|
open class Contact : RealmObject() {
|
||||||
|
|
||||||
var lookupKey: String = ""
|
@PrimaryKey var lookupKey: String = ""
|
||||||
var numbers: RealmList<PhoneNumber> = RealmList()
|
var numbers: RealmList<PhoneNumber> = RealmList()
|
||||||
var name: String = ""
|
var name: String = ""
|
||||||
var lastUpdate: Long = 0
|
var lastUpdate: Long = 0
|
||||||
|
|||||||
@@ -0,0 +1,56 @@
|
|||||||
|
package com.moez.QKSMS.domain.interactor
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import com.moez.QKSMS.common.util.extensions.asFlowable
|
||||||
|
import com.moez.QKSMS.data.mapper.CursorToContact
|
||||||
|
import io.reactivex.Flowable
|
||||||
|
import io.realm.Realm
|
||||||
|
import timber.log.Timber
|
||||||
|
import java.util.concurrent.TimeUnit
|
||||||
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
open class ContactSync @Inject constructor(
|
||||||
|
private val context: Context,
|
||||||
|
private val cursorToContact: CursorToContact)
|
||||||
|
: Interactor<Unit, Long>() {
|
||||||
|
|
||||||
|
override fun buildObservable(params: Unit): Flowable<Long> {
|
||||||
|
val contentResolver = context.contentResolver
|
||||||
|
var realm: Realm? = null
|
||||||
|
|
||||||
|
var startTime = 0L
|
||||||
|
|
||||||
|
return Flowable.just(params)
|
||||||
|
.doOnNext {
|
||||||
|
Timber.v("Starting contact sync")
|
||||||
|
startTime = System.currentTimeMillis()
|
||||||
|
|
||||||
|
// We need to set up realm on the io thread, and doOnSubscribe doesn't support setting a custom Scheduler
|
||||||
|
realm = Realm.getDefaultInstance()
|
||||||
|
realm?.beginTransaction()
|
||||||
|
}
|
||||||
|
.flatMap { contentResolver.query(CursorToContact.URI, CursorToContact.PROJECTION, null, null, null).asFlowable() }
|
||||||
|
.map { cursor -> cursorToContact.map(cursor) }
|
||||||
|
.groupBy { contact -> contact.lookupKey }
|
||||||
|
.flatMap { group -> group.toList().toFlowable() }
|
||||||
|
.map { contacts ->
|
||||||
|
val numbers = contacts.map { it.numbers }.flatten()
|
||||||
|
val contact = contacts.first()
|
||||||
|
contact.numbers.clear()
|
||||||
|
contact.numbers.addAll(numbers)
|
||||||
|
contact
|
||||||
|
}
|
||||||
|
.toList().toFlowable()
|
||||||
|
.doOnNext { contacts ->
|
||||||
|
Timber.v("${contacts.size} contacts")
|
||||||
|
realm?.insertOrUpdate(contacts)
|
||||||
|
}
|
||||||
|
.doOnNext {
|
||||||
|
realm?.commitTransaction()
|
||||||
|
realm?.close()
|
||||||
|
Timber.v("Synced contacts in ${TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - startTime)} seconds")
|
||||||
|
}
|
||||||
|
.map { 0L }
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -31,6 +31,7 @@ class ComposeViewModel(threadId: Long, body: String)
|
|||||||
@Inject lateinit var contactsRepo: ContactRepository
|
@Inject lateinit var contactsRepo: ContactRepository
|
||||||
@Inject lateinit var messageRepo: MessageRepository
|
@Inject lateinit var messageRepo: MessageRepository
|
||||||
@Inject lateinit var navigator: Navigator
|
@Inject lateinit var navigator: Navigator
|
||||||
|
@Inject lateinit var syncContacts: ContactSync
|
||||||
@Inject lateinit var markArchived: MarkArchived
|
@Inject lateinit var markArchived: MarkArchived
|
||||||
@Inject lateinit var markUnarchived: MarkUnarchived
|
@Inject lateinit var markUnarchived: MarkUnarchived
|
||||||
@Inject lateinit var deleteConversation: DeleteConversation
|
@Inject lateinit var deleteConversation: DeleteConversation
|
||||||
@@ -96,6 +97,10 @@ class ComposeViewModel(threadId: Long, body: String)
|
|||||||
messages
|
messages
|
||||||
})
|
})
|
||||||
.subscribe()
|
.subscribe()
|
||||||
|
|
||||||
|
if (threadId == 0L) {
|
||||||
|
syncContacts.execute(Unit)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun bindView(view: ComposeView) {
|
override fun bindView(view: ComposeView) {
|
||||||
|
|||||||
Reference in New Issue
Block a user