mirror of
https://github.com/quik-sms/quik.git
synced 2026-11-17 19:38:52 +01:00
Handle edittext backspaces for Compose Activity in ViewModel instead of ChipsAdapter
This commit is contained in:
@@ -20,7 +20,6 @@ package presentation.feature.compose
|
||||
|
||||
import android.content.Context
|
||||
import android.support.v7.widget.RecyclerView
|
||||
import android.view.KeyEvent
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
@@ -44,8 +43,8 @@ import javax.inject.Inject
|
||||
class ChipsAdapter @Inject constructor(private val context: Context, private val colors: Colors) : QkAdapter<Contact>() {
|
||||
|
||||
companion object {
|
||||
private val TYPE_EDIT_TEXT = 0
|
||||
private val TYPE_ITEM = 1
|
||||
private const val TYPE_EDIT_TEXT = 0
|
||||
private const val TYPE_ITEM = 1
|
||||
}
|
||||
|
||||
private val hint: String = context.getString(R.string.title_compose)
|
||||
@@ -54,6 +53,7 @@ class ChipsAdapter @Inject constructor(private val context: Context, private val
|
||||
var view: RecyclerView? = null
|
||||
val chipDeleted: PublishSubject<Contact> = PublishSubject.create<Contact>()
|
||||
val textChanges = editText.textChanges()
|
||||
val keyEvents = editText.keys()
|
||||
|
||||
init {
|
||||
val wrap = ViewGroup.LayoutParams.WRAP_CONTENT
|
||||
@@ -63,14 +63,6 @@ class ChipsAdapter @Inject constructor(private val context: Context, private val
|
||||
}
|
||||
|
||||
editText.hint = hint
|
||||
editText.keys()
|
||||
.filter { event -> event.action == KeyEvent.ACTION_DOWN }
|
||||
.filter { event -> event.keyCode == KeyEvent.KEYCODE_DEL }
|
||||
.subscribe {
|
||||
if (itemCount > 1 && editText.text.isEmpty()) {
|
||||
chipDeleted.onNext(getItem(itemCount - 2))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDatasetChanged() {
|
||||
|
||||
@@ -25,6 +25,7 @@ import android.net.Uri
|
||||
import android.os.Bundle
|
||||
import android.support.v7.widget.LinearLayoutManager
|
||||
import android.support.v7.widget.RecyclerView
|
||||
import android.view.KeyEvent
|
||||
import android.view.Menu
|
||||
import android.view.MenuItem
|
||||
import com.google.android.flexbox.FlexboxLayoutManager
|
||||
@@ -55,6 +56,7 @@ class ComposeActivity : QkActivity<ComposeViewModel>(), ComposeView {
|
||||
override val viewModelClass = ComposeViewModel::class
|
||||
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 chipSelectedIntent: Subject<Contact> by lazy { contactsAdapter.contactSelected }
|
||||
override val chipDeletedIntent: Subject<Contact> by lazy { chipsAdapter.chipDeleted }
|
||||
override val menuReadyIntent: Observable<Unit> = menu.map { Unit }
|
||||
|
||||
@@ -18,8 +18,8 @@
|
||||
*/
|
||||
package presentation.feature.compose
|
||||
|
||||
import android.graphics.Bitmap
|
||||
import android.net.Uri
|
||||
import android.view.KeyEvent
|
||||
import data.model.Contact
|
||||
import data.model.Message
|
||||
import io.reactivex.Observable
|
||||
@@ -30,6 +30,7 @@ interface ComposeView : QkView<ComposeState> {
|
||||
|
||||
val activityVisibleIntent: Observable<Boolean>
|
||||
val queryChangedIntent: Observable<CharSequence>
|
||||
val queryKeyEventIntent: Observable<KeyEvent>
|
||||
val chipSelectedIntent: Subject<Contact>
|
||||
val chipDeletedIntent: Subject<Contact>
|
||||
val menuReadyIntent: Observable<Unit>
|
||||
|
||||
@@ -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 ->
|
||||
|
||||
Reference in New Issue
Block a user