set up contactrepository to inflate phone number in contact

This commit is contained in:
Moez Bhatti
2017-08-28 15:13:27 -07:00
parent 7996048658
commit 22489af4c3
10 changed files with 80 additions and 33 deletions
@@ -0,0 +1,19 @@
package com.moez.QKSMS.data.model
import io.realm.RealmObject
import io.realm.annotations.PrimaryKey
open class Contact() : RealmObject() {
@PrimaryKey var id: Long = 0
var address: String = ""
/**
* For inflating the contact using the cursor
*/
constructor(id: Long, address: String) : this() {
this.id = id
this.address = address
}
}
@@ -1,6 +1,7 @@
package com.moez.QKSMS.data.model
import android.database.Cursor
import com.moez.QKSMS.data.repository.ContactRepository
import com.moez.QKSMS.data.sync.ConversationColumns
import io.reactivex.Flowable
import io.realm.RealmList
@@ -12,7 +13,7 @@ open class Conversation() : RealmObject() {
@PrimaryKey var id: Long = 0
var date: Long = 0
var messageCount: Int = 0
var recipients: RealmList<Recipient> = RealmList()
var contacts: RealmList<Contact> = RealmList()
var snippet: String = ""
var snippetCs: String = ""
var read: Int = 0
@@ -22,15 +23,16 @@ open class Conversation() : RealmObject() {
/**
* Instantiates a Conversation from the Android SMS ContentProvider
*/
constructor(cursor: Cursor) : this() {
constructor(cursor: Cursor, contacts: ContactRepository) : this() {
id = cursor.getLong(ConversationColumns.ID)
date = cursor.getLong(ConversationColumns.DATE)
messageCount = cursor.getInt(ConversationColumns.MESSAGE_COUNT)
Flowable.fromIterable(cursor.getString(ConversationColumns.RECIPIENT_IDS).split(" "))
.map { id -> id.toLong() }
.map { id -> Recipient(id) }
.blockingSubscribe { recipient -> recipients.add(recipient) }
.map { id -> contacts.getContactBlocking(id) }
.filter { contact -> contact.id != 0L }
.blockingSubscribe { contact -> this.contacts.add(contact) }
snippet = cursor.getString(ConversationColumns.SNIPPET) ?: ""
snippetCs = cursor.getString(ConversationColumns.SNIPPET_CS) ?: ""
@@ -1,17 +0,0 @@
package com.moez.QKSMS.data.model
import io.realm.RealmObject
import io.realm.annotations.PrimaryKey
open class Recipient() : RealmObject() {
@PrimaryKey var id: Long = 0
/**
* For inflating the recipient using just the id
*/
constructor(id: Long) : this() {
this.id = id
}
}
@@ -0,0 +1,25 @@
package com.moez.QKSMS.data.repository
import android.content.ContentUris
import android.content.Context
import android.net.Uri
import com.moez.QKSMS.data.model.Contact
class ContactRepository(val context: Context) {
/**
* Uri for the MMS-SMS recipients table, where there exists only one column (address)
*/
private val URI = Uri.parse("content://mms-sms/canonical-address")
fun getContactBlocking(recipientId: Long): Contact {
val cursor = context.contentResolver.query(ContentUris.withAppendedId(URI, recipientId), null, null, null, null)
if (cursor.moveToFirst()) {
return Contact(recipientId, cursor.getString(0))
}
cursor.close()
return Contact()
}
}
@@ -4,13 +4,14 @@ import android.content.Context
import android.net.Uri
import com.moez.QKSMS.data.model.Conversation
import com.moez.QKSMS.data.model.Message
import com.moez.QKSMS.data.repository.ContactRepository
import com.moez.QKSMS.util.asFlowable
import io.reactivex.Flowable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import io.realm.Realm
class SyncManager(val context: Context) {
class SyncManager(val context: Context, val contacts: ContactRepository) {
private val TAG = "SyncManager"
fun copyToRealm(completionListener: () -> Unit) {
@@ -30,7 +31,7 @@ class SyncManager(val context: Context) {
realm?.delete(Message::class.java)
}
.flatMap { cursor -> cursor.asFlowable() }
.map { cursor -> Conversation(cursor) }
.map { cursor -> Conversation(cursor, contacts) }
.distinct { conversation -> conversation.id }
.doOnNext { conversation -> realm?.insertOrUpdate(conversation) }
.map { conversation -> conversation.id }