Handle edittext backspaces for Compose Activity in ViewModel instead of ChipsAdapter

This commit is contained in:
Moez Bhatti
2018-01-22 12:36:52 -05:00
parent dc73c636b9
commit ab0e9a0f26
4 changed files with 20 additions and 12 deletions
@@ -22,6 +22,7 @@ import android.content.Context
import android.content.Intent
import android.net.Uri
import android.telephony.PhoneNumberUtils
import android.view.KeyEvent
import com.mlsdev.rximagepicker.RxImagePicker
import com.mlsdev.rximagepicker.Sources
import com.moez.QKSMS.R
@@ -208,6 +209,18 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
.autoDisposable(view.scope())
.subscribe { contacts -> newState { it.copy(contacts = contacts) } }
// Backspaces should delete the most recent contact if there's no text input
view.queryKeyEventIntent
.filter { event -> event.action == KeyEvent.ACTION_DOWN }
.filter { event -> event.keyCode == KeyEvent.KEYCODE_DEL }
.withLatestFrom(selectedContacts, view.queryChangedIntent, { _, contacts, query ->
if (contacts.isNotEmpty() && query.isEmpty()) {
contactsReducer.onNext { it.dropLast(1) }
}
})
.autoDisposable(view.scope())
.subscribe()
// Update the list of selected contacts when a new contact is selected or an existing one is deselected
Observable.merge(
view.chipDeletedIntent.doOnNext { contact ->