forked from upstream-mirrors/quik
Enter button will select first contact suggestion, back button closes activity
Fixes #861
This commit is contained in:
@@ -27,6 +27,7 @@ import android.widget.EditText
|
||||
import android.widget.RelativeLayout
|
||||
import com.google.android.flexbox.FlexboxLayoutManager
|
||||
import com.jakewharton.rxbinding2.view.keys
|
||||
import com.jakewharton.rxbinding2.widget.editorActions
|
||||
import com.jakewharton.rxbinding2.widget.textChanges
|
||||
import com.moez.QKSMS.R
|
||||
import common.util.Colors
|
||||
@@ -54,6 +55,7 @@ class ChipsAdapter @Inject constructor(private val context: Context, private val
|
||||
val chipDeleted: PublishSubject<Contact> = PublishSubject.create<Contact>()
|
||||
val textChanges = editText.textChanges()
|
||||
val keyEvents = editText.keys()
|
||||
val actions = editText.editorActions()
|
||||
|
||||
init {
|
||||
val wrap = ViewGroup.LayoutParams.WRAP_CONTENT
|
||||
|
||||
@@ -57,6 +57,7 @@ class ComposeActivity : QkActivity<ComposeViewModel>(), ComposeView {
|
||||
override val activityVisibleIntent: Subject<Boolean> = PublishSubject.create()
|
||||
override val queryChangedIntent: Observable<CharSequence> by lazy { chipsAdapter.textChanges }
|
||||
override val queryKeyEventIntent: Observable<KeyEvent> by lazy { chipsAdapter.keyEvents }
|
||||
override val queryEditorActionIntent: Observable<Int> by lazy { chipsAdapter.actions }
|
||||
override val chipSelectedIntent: Subject<Contact> by lazy { contactsAdapter.contactSelected }
|
||||
override val chipDeletedIntent: Subject<Contact> by lazy { chipsAdapter.chipDeleted }
|
||||
override val menuReadyIntent: Observable<Unit> = menu.map { Unit }
|
||||
|
||||
@@ -31,6 +31,7 @@ interface ComposeView : QkView<ComposeState> {
|
||||
val activityVisibleIntent: Observable<Boolean>
|
||||
val queryChangedIntent: Observable<CharSequence>
|
||||
val queryKeyEventIntent: Observable<KeyEvent>
|
||||
val queryEditorActionIntent: Observable<Int>
|
||||
val chipSelectedIntent: Subject<Contact>
|
||||
val chipDeletedIntent: Subject<Contact>
|
||||
val menuReadyIntent: Observable<Unit>
|
||||
|
||||
@@ -23,6 +23,7 @@ import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.telephony.PhoneNumberUtils
|
||||
import android.view.KeyEvent
|
||||
import android.view.inputmethod.EditorInfo
|
||||
import com.mlsdev.rximagepicker.RxImagePicker
|
||||
import com.mlsdev.rximagepicker.Sources
|
||||
import com.moez.QKSMS.R
|
||||
@@ -210,17 +211,37 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
|
||||
.subscribe { contacts -> newState { it.copy(contacts = contacts) } }
|
||||
|
||||
// Backspaces should delete the most recent contact if there's no text input
|
||||
// Close the activity if user presses back
|
||||
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) }
|
||||
.withLatestFrom(selectedContacts, view.queryChangedIntent, { event, contacts, query ->
|
||||
when (event.keyCode) {
|
||||
KeyEvent.KEYCODE_DEL -> {
|
||||
if (contacts.isNotEmpty() && query.isEmpty()) {
|
||||
contactsReducer.onNext { it.dropLast(1) }
|
||||
}
|
||||
}
|
||||
|
||||
KeyEvent.KEYCODE_BACK -> {
|
||||
newState { it.copy(hasError = true) }
|
||||
}
|
||||
}
|
||||
|
||||
})
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe()
|
||||
|
||||
// Enter the first contact suggestion if the enter button is pressed
|
||||
view.queryEditorActionIntent
|
||||
.filter { actionId -> actionId == EditorInfo.IME_ACTION_DONE }
|
||||
.withLatestFrom(state, { _, state -> state })
|
||||
.autoDisposable(view.scope())
|
||||
.subscribe { state ->
|
||||
state.contacts.firstOrNull()?.let { contact ->
|
||||
contactsReducer.onNext { contacts -> contacts + contact }
|
||||
}
|
||||
}
|
||||
|
||||
// Update the list of selected contacts when a new contact is selected or an existing one is deselected
|
||||
Observable.merge(
|
||||
view.chipDeletedIntent.doOnNext { contact ->
|
||||
|
||||
Reference in New Issue
Block a user