Use live-unmanaged list of contacts in ComposeViewModel

Fixes #839
This commit is contained in:
Moez Bhatti
2018-01-12 23:35:37 -05:00
parent 634c7c707b
commit 8bb835c2f5
2 changed files with 14 additions and 16 deletions
@@ -27,6 +27,8 @@ 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
@@ -44,21 +46,17 @@ class ContactRepository @Inject constructor(val context: Context) {
.map { id -> Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, id) }
}
fun getContacts(): List<Contact> {
fun getUnmanagedContacts(): Flowable<List<Contact>> {
val realm = Realm.getDefaultInstance()
val results = realm.copyFromRealm(realm
.where(Contact::class.java)
.findAllSorted("name"))
realm.close()
return results
}
fun getContact(address: String): Flowable<Contact> {
return Flowable.fromPublisher<Contact> { emitter ->
getContactBlocking(address)?.let { emitter.onNext(it) }
emitter.onComplete()
}
return realm.where(Contact::class.java)
.sort("name")
.findAllAsync()
.asFlowable()
.filter { it.isLoaded }
.filter { it.isValid }
.map { realm.copyFromRealm(it) }
.subscribeOn(AndroidSchedulers.mainThread())
.observeOn(Schedulers.io())
}
fun getContactBlocking(address: String): Contact? {
@@ -67,7 +67,7 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
private var draft: String = ""
private val attachments: Subject<List<Bitmap>> = BehaviorSubject.createDefault(ArrayList())
private val contacts: List<Contact> by lazy { contactsRepo.getContacts() }
private val contacts: Observable<List<Contact>> by lazy { contactsRepo.getUnmanagedContacts().toObservable() }
private val contactsReducer: Subject<(List<Contact>) -> List<Contact>> = PublishSubject.create()
private val selectedContacts: Observable<List<Contact>>
private val conversation: Observable<Conversation>
@@ -184,7 +184,7 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
// Update the list of contact suggestions based on the query input, while also filtering out any contacts
// that have already been selected
intents += Observables
.combineLatest(view.queryChangedIntent, selectedContacts, { query, selectedContacts ->
.combineLatest(view.queryChangedIntent, contacts, selectedContacts, { query, contacts, selectedContacts ->
contacts
.filterNot { contact -> selectedContacts.contains(contact) }
.filter { contact -> contactFilter.filter(contact, query) }