forked from upstream-mirrors/quik
#1028 - Fix backspace not working when searching for contacts
This commit is contained in:
@@ -21,6 +21,10 @@ package common.widget
|
|||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.graphics.Typeface
|
import android.graphics.Typeface
|
||||||
import android.util.AttributeSet
|
import android.util.AttributeSet
|
||||||
|
import android.view.KeyEvent
|
||||||
|
import android.view.inputmethod.EditorInfo
|
||||||
|
import android.view.inputmethod.InputConnection
|
||||||
|
import android.view.inputmethod.InputConnectionWrapper
|
||||||
import android.widget.EditText
|
import android.widget.EditText
|
||||||
import com.moez.QKSMS.R
|
import com.moez.QKSMS.R
|
||||||
import com.uber.autodispose.android.scope
|
import com.uber.autodispose.android.scope
|
||||||
@@ -35,6 +39,7 @@ import injection.appComponent
|
|||||||
import io.reactivex.Observable
|
import io.reactivex.Observable
|
||||||
import io.reactivex.rxkotlin.Observables
|
import io.reactivex.rxkotlin.Observables
|
||||||
import io.reactivex.subjects.BehaviorSubject
|
import io.reactivex.subjects.BehaviorSubject
|
||||||
|
import io.reactivex.subjects.PublishSubject
|
||||||
import io.reactivex.subjects.Subject
|
import io.reactivex.subjects.Subject
|
||||||
import util.Preferences
|
import util.Preferences
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
@@ -65,6 +70,8 @@ class QkEditText @JvmOverloads constructor(context: Context, attrs: AttributeSet
|
|||||||
@Inject lateinit var fontProvider: FontProvider
|
@Inject lateinit var fontProvider: FontProvider
|
||||||
@Inject lateinit var prefs: Preferences
|
@Inject lateinit var prefs: Preferences
|
||||||
|
|
||||||
|
val backspaces: Subject<Unit> = PublishSubject.create()
|
||||||
|
|
||||||
private var textColorObservable: Observable<Int>? = null
|
private var textColorObservable: Observable<Int>? = null
|
||||||
private var textColorHintObservable: Observable<Int>? = null
|
private var textColorHintObservable: Observable<Int>? = null
|
||||||
|
|
||||||
@@ -169,4 +176,26 @@ class QkEditText @JvmOverloads constructor(context: Context, attrs: AttributeSet
|
|||||||
.subscribe()
|
.subscribe()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onCreateInputConnection(outAttrs: EditorInfo): InputConnection {
|
||||||
|
return object : InputConnectionWrapper(super.onCreateInputConnection(outAttrs), true) {
|
||||||
|
|
||||||
|
override fun sendKeyEvent(event: KeyEvent): Boolean {
|
||||||
|
if (event.action == KeyEvent.ACTION_DOWN && event.keyCode == KeyEvent.KEYCODE_DEL) {
|
||||||
|
backspaces.onNext(Unit)
|
||||||
|
}
|
||||||
|
return super.sendKeyEvent(event)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
override fun deleteSurroundingText(beforeLength: Int, afterLength: Int): Boolean {
|
||||||
|
// magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspace
|
||||||
|
return if (beforeLength == 1 && afterLength == 0) {
|
||||||
|
sendKeyEvent(KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL)) && sendKeyEvent(KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL))
|
||||||
|
} else super.deleteSurroundingText(beforeLength, afterLength)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -20,14 +20,11 @@ package feature.compose
|
|||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.support.v7.widget.RecyclerView
|
import android.support.v7.widget.RecyclerView
|
||||||
import android.view.KeyEvent
|
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import android.widget.EditText
|
|
||||||
import android.widget.RelativeLayout
|
import android.widget.RelativeLayout
|
||||||
import com.google.android.flexbox.FlexboxLayoutManager
|
import com.google.android.flexbox.FlexboxLayoutManager
|
||||||
import com.jakewharton.rxbinding2.view.keys
|
|
||||||
import com.jakewharton.rxbinding2.widget.editorActions
|
import com.jakewharton.rxbinding2.widget.editorActions
|
||||||
import com.jakewharton.rxbinding2.widget.textChanges
|
import com.jakewharton.rxbinding2.widget.textChanges
|
||||||
import com.moez.QKSMS.R
|
import com.moez.QKSMS.R
|
||||||
@@ -37,7 +34,7 @@ import common.util.Colors
|
|||||||
import common.util.extensions.dpToPx
|
import common.util.extensions.dpToPx
|
||||||
import common.util.extensions.setBackgroundTint
|
import common.util.extensions.setBackgroundTint
|
||||||
import common.util.extensions.showKeyboard
|
import common.util.extensions.showKeyboard
|
||||||
import io.reactivex.functions.Predicate
|
import common.widget.QkEditText
|
||||||
import io.reactivex.subjects.PublishSubject
|
import io.reactivex.subjects.PublishSubject
|
||||||
import kotlinx.android.synthetic.main.contact_chip.view.*
|
import kotlinx.android.synthetic.main.contact_chip.view.*
|
||||||
import model.Contact
|
import model.Contact
|
||||||
@@ -51,21 +48,13 @@ class ChipsAdapter @Inject constructor(private val context: Context, private val
|
|||||||
}
|
}
|
||||||
|
|
||||||
private val hint: String = context.getString(R.string.title_compose)
|
private val hint: String = context.getString(R.string.title_compose)
|
||||||
private val editText = View.inflate(context, R.layout.chip_input_list_item, null) as EditText
|
private val editText = View.inflate(context, R.layout.chip_input_list_item, null) as QkEditText
|
||||||
|
|
||||||
var view: RecyclerView? = null
|
var view: RecyclerView? = null
|
||||||
val chipDeleted: PublishSubject<Contact> = PublishSubject.create<Contact>()
|
val chipDeleted: PublishSubject<Contact> = PublishSubject.create<Contact>()
|
||||||
val textChanges = editText.textChanges()
|
val textChanges = editText.textChanges()
|
||||||
val actions = editText.editorActions()
|
val actions = editText.editorActions()
|
||||||
|
val backspaces = editText.backspaces
|
||||||
/**
|
|
||||||
* On certain devices, not returning false in this predicate for number entry will result in
|
|
||||||
* the user not being able to enter numbers into the text field. As a result, we have to only
|
|
||||||
* pass the events that we're looking for
|
|
||||||
*/
|
|
||||||
val keyEvents = editText.keys(Predicate { event ->
|
|
||||||
event.keyCode == KeyEvent.KEYCODE_BACK || event.keyCode == KeyEvent.KEYCODE_DEL
|
|
||||||
})
|
|
||||||
|
|
||||||
init {
|
init {
|
||||||
val wrap = ViewGroup.LayoutParams.WRAP_CONTENT
|
val wrap = ViewGroup.LayoutParams.WRAP_CONTENT
|
||||||
|
|||||||
@@ -23,7 +23,6 @@ import android.graphics.PorterDuff
|
|||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.support.v7.widget.LinearLayoutManager
|
import android.support.v7.widget.LinearLayoutManager
|
||||||
import android.view.KeyEvent
|
|
||||||
import android.view.Menu
|
import android.view.Menu
|
||||||
import android.view.MenuItem
|
import android.view.MenuItem
|
||||||
import com.google.android.flexbox.FlexboxLayoutManager
|
import com.google.android.flexbox.FlexboxLayoutManager
|
||||||
@@ -54,7 +53,7 @@ class ComposeActivity : QkThemedActivity<ComposeViewModel>(), ComposeView {
|
|||||||
override val viewModelClass = ComposeViewModel::class
|
override val viewModelClass = ComposeViewModel::class
|
||||||
override val activityVisibleIntent: Subject<Boolean> = PublishSubject.create()
|
override val activityVisibleIntent: Subject<Boolean> = PublishSubject.create()
|
||||||
override val queryChangedIntent: Observable<CharSequence> by lazy { chipsAdapter.textChanges }
|
override val queryChangedIntent: Observable<CharSequence> by lazy { chipsAdapter.textChanges }
|
||||||
override val queryKeyEventIntent: Observable<KeyEvent> by lazy { chipsAdapter.keyEvents }
|
override val queryBackspaceIntent: Observable<*> by lazy { chipsAdapter.backspaces }
|
||||||
override val queryEditorActionIntent: Observable<Int> by lazy { chipsAdapter.actions }
|
override val queryEditorActionIntent: Observable<Int> by lazy { chipsAdapter.actions }
|
||||||
override val chipSelectedIntent: Subject<Contact> by lazy { contactsAdapter.contactSelected }
|
override val chipSelectedIntent: Subject<Contact> by lazy { contactsAdapter.contactSelected }
|
||||||
override val chipDeletedIntent: Subject<Contact> by lazy { chipsAdapter.chipDeleted }
|
override val chipDeletedIntent: Subject<Contact> by lazy { chipsAdapter.chipDeleted }
|
||||||
|
|||||||
@@ -19,7 +19,6 @@
|
|||||||
package feature.compose
|
package feature.compose
|
||||||
|
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.view.KeyEvent
|
|
||||||
import common.MenuItem
|
import common.MenuItem
|
||||||
import common.base.QkView
|
import common.base.QkView
|
||||||
import io.reactivex.Observable
|
import io.reactivex.Observable
|
||||||
@@ -31,7 +30,7 @@ interface ComposeView : QkView<ComposeState> {
|
|||||||
|
|
||||||
val activityVisibleIntent: Observable<Boolean>
|
val activityVisibleIntent: Observable<Boolean>
|
||||||
val queryChangedIntent: Observable<CharSequence>
|
val queryChangedIntent: Observable<CharSequence>
|
||||||
val queryKeyEventIntent: Observable<KeyEvent>
|
val queryBackspaceIntent: Observable<*>
|
||||||
val queryEditorActionIntent: Observable<Int>
|
val queryEditorActionIntent: Observable<Int>
|
||||||
val chipSelectedIntent: Subject<Contact>
|
val chipSelectedIntent: Subject<Contact>
|
||||||
val chipDeletedIntent: Subject<Contact>
|
val chipDeletedIntent: Subject<Contact>
|
||||||
|
|||||||
@@ -23,7 +23,6 @@ import android.content.Intent
|
|||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.telephony.PhoneNumberUtils
|
import android.telephony.PhoneNumberUtils
|
||||||
import android.telephony.SmsMessage
|
import android.telephony.SmsMessage
|
||||||
import android.view.KeyEvent
|
|
||||||
import android.view.inputmethod.EditorInfo
|
import android.view.inputmethod.EditorInfo
|
||||||
import com.mlsdev.rximagepicker.RxImagePicker
|
import com.mlsdev.rximagepicker.RxImagePicker
|
||||||
import com.mlsdev.rximagepicker.Sources
|
import com.mlsdev.rximagepicker.Sources
|
||||||
@@ -241,21 +240,11 @@ class ComposeViewModel(intent: Intent) : QkViewModel<ComposeView, ComposeState>(
|
|||||||
|
|
||||||
// Backspaces should delete the most recent contact if there's no text input
|
// Backspaces should delete the most recent contact if there's no text input
|
||||||
// Close the activity if user presses back
|
// Close the activity if user presses back
|
||||||
view.queryKeyEventIntent
|
view.queryBackspaceIntent
|
||||||
.filter { event -> event.action == KeyEvent.ACTION_DOWN }
|
|
||||||
.withLatestFrom(selectedContacts, view.queryChangedIntent, { event, contacts, query ->
|
.withLatestFrom(selectedContacts, view.queryChangedIntent, { event, contacts, query ->
|
||||||
when (event.keyCode) {
|
if (contacts.isNotEmpty() && query.isEmpty()) {
|
||||||
KeyEvent.KEYCODE_DEL -> {
|
contactsReducer.onNext { it.dropLast(1) }
|
||||||
if (contacts.isNotEmpty() && query.isEmpty()) {
|
|
||||||
contactsReducer.onNext { it.dropLast(1) }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
KeyEvent.KEYCODE_BACK -> {
|
|
||||||
newState { it.copy(hasError = true) }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
})
|
})
|
||||||
.autoDisposable(view.scope())
|
.autoDisposable(view.scope())
|
||||||
.subscribe()
|
.subscribe()
|
||||||
|
|||||||
Reference in New Issue
Block a user