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 data.model.PhoneNumber
import io.reactivex.Flowable import io.reactivex.Flowable
import io.reactivex.Single import io.reactivex.Single
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import io.realm.Realm import io.realm.Realm
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
@@ -44,21 +46,17 @@ class ContactRepository @Inject constructor(val context: Context) {
.map { id -> Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, id) } .map { id -> Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, id) }
} }
fun getContacts(): List<Contact> { fun getUnmanagedContacts(): Flowable<List<Contact>> {
val realm = Realm.getDefaultInstance() val realm = Realm.getDefaultInstance()
val results = realm.copyFromRealm(realm return realm.where(Contact::class.java)
.where(Contact::class.java) .sort("name")
.findAllSorted("name")) .findAllAsync()
realm.close() .asFlowable()
.filter { it.isLoaded }
return results .filter { it.isValid }
} .map { realm.copyFromRealm(it) }
.subscribeOn(AndroidSchedulers.mainThread())
fun getContact(address: String): Flowable<Contact> { .observeOn(Schedulers.io())
return Flowable.fromPublisher<Contact> { emitter ->
getContactBlocking(address)?.let { emitter.onNext(it) }
emitter.onComplete()
}
} }
fun getContactBlocking(address: String): Contact? { fun getContactBlocking(address: String): Contact? {
@@ -67,7 +67,7 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
private var draft: String = "" private var draft: String = ""
private val attachments: Subject<List<Bitmap>> = BehaviorSubject.createDefault(ArrayList()) 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 contactsReducer: Subject<(List<Contact>) -> List<Contact>> = PublishSubject.create()
private val selectedContacts: Observable<List<Contact>> private val selectedContacts: Observable<List<Contact>>
private val conversation: Observable<Conversation> 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 // Update the list of contact suggestions based on the query input, while also filtering out any contacts
// that have already been selected // that have already been selected
intents += Observables intents += Observables
.combineLatest(view.queryChangedIntent, selectedContacts, { query, selectedContacts -> .combineLatest(view.queryChangedIntent, contacts, selectedContacts, { query, contacts, selectedContacts ->
contacts contacts
.filterNot { contact -> selectedContacts.contains(contact) } .filterNot { contact -> selectedContacts.contains(contact) }
.filter { contact -> contactFilter.filter(contact, query) } .filter { contact -> contactFilter.filter(contact, query) }