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
@@ -2,10 +2,7 @@ package com.moez.QKSMS
import android.app.Application
import com.bugsnag.android.Bugsnag
import com.moez.QKSMS.dagger.AppComponent
import com.moez.QKSMS.dagger.AppModule
import com.moez.QKSMS.dagger.ConversationsModule
import com.moez.QKSMS.dagger.DaggerAppComponent
import com.moez.QKSMS.dagger.*
import io.realm.Realm
import io.realm.RealmConfiguration
@@ -27,6 +24,7 @@ class QKApplication : Application() {
appComponent = DaggerAppComponent.builder()
.appModule(AppModule(this))
.contactModule(ContactModule())
.conversationsModule(ConversationsModule())
.build()
}
@@ -6,7 +6,7 @@ import dagger.Component
import javax.inject.Singleton
@Singleton
@Component(modules = arrayOf(AppModule::class, ConversationsModule::class))
@Component(modules = arrayOf(AppModule::class, ContactModule::class, ConversationsModule::class))
interface AppComponent {
fun inject(fragment: ConversationListFragment)
@@ -2,6 +2,7 @@ package com.moez.QKSMS.dagger
import android.app.Application
import android.content.Context
import com.moez.QKSMS.data.repository.ContactRepository
import com.moez.QKSMS.data.sync.SyncManager
import dagger.Module
import dagger.Provides
@@ -18,8 +19,8 @@ class AppModule(var application: Application) {
@Provides
@Singleton
fun provideSyncManager(context: Context): SyncManager {
return SyncManager(context)
fun provideSyncManager(context: Context, contacts: ContactRepository): SyncManager {
return SyncManager(context, contacts)
}
}
@@ -0,0 +1,18 @@
package com.moez.QKSMS.dagger
import android.content.Context
import com.moez.QKSMS.data.repository.ContactRepository
import dagger.Module
import dagger.Provides
import javax.inject.Singleton
@Module
class ContactModule {
@Provides
@Singleton
fun provideContactRepository(context: Context): ContactRepository {
return ContactRepository(context)
}
}
@@ -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 }
@@ -31,9 +31,9 @@ class ConversationAdapter(context: Context, data: OrderedRealmCollection<Convers
}
var title = ""
conversation?.recipients?.forEachIndexed { index, recipient ->
title += recipient.id
if (index < conversation.recipients.size - 1) {
conversation?.contacts?.forEachIndexed { index, recipient ->
title += recipient.address
if (index < conversation.contacts.size - 1) {
title += ", "
}
}