mirror of
https://github.com/SchildiChat/SchildiChat-android.git
synced 2025-03-17 19:58:57 +03:00
Pills: remove pills when a char is deleted
This commit is contained in:
parent
a3f8f138a6
commit
5d3c376267
1 changed files with 38 additions and 0 deletions
|
@ -20,12 +20,16 @@ package im.vector.riotx.features.home.room.detail.composer
|
|||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.text.Editable
|
||||
import android.util.AttributeSet
|
||||
import android.view.inputmethod.EditorInfo
|
||||
import android.view.inputmethod.InputConnection
|
||||
import androidx.appcompat.widget.AppCompatEditText
|
||||
import androidx.core.view.inputmethod.EditorInfoCompat
|
||||
import androidx.core.view.inputmethod.InputConnectionCompat
|
||||
import im.vector.riotx.core.platform.SimpleTextWatcher
|
||||
import im.vector.riotx.features.html.PillImageSpan
|
||||
import timber.log.Timber
|
||||
|
||||
class ComposerEditText @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = android.R.attr.editTextStyle)
|
||||
: AppCompatEditText(context, attrs, defStyleAttr) {
|
||||
|
@ -55,4 +59,38 @@ class ComposerEditText @JvmOverloads constructor(context: Context, attrs: Attrib
|
|||
}
|
||||
return InputConnectionCompat.createWrapper(ic, editorInfo, callback)
|
||||
}
|
||||
|
||||
init {
|
||||
addTextChangedListener(
|
||||
object : SimpleTextWatcher() {
|
||||
var spanToRemove: PillImageSpan? = null
|
||||
|
||||
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
|
||||
Timber.v("beforeTextChanged: start:$start count:$count after:$after")
|
||||
|
||||
if (count > after) {
|
||||
// A char has been deleted
|
||||
val deleteCharPosition = start + count
|
||||
Timber.v("beforeTextChanged: deleted char at $deleteCharPosition")
|
||||
|
||||
// Get span at this position
|
||||
val spans = editableText.getSpans(deleteCharPosition, deleteCharPosition, PillImageSpan::class.java)
|
||||
spanToRemove = spans.firstOrNull()
|
||||
}
|
||||
}
|
||||
|
||||
override fun afterTextChanged(s: Editable) {
|
||||
if (spanToRemove != null) {
|
||||
Timber.v("Removing the span")
|
||||
val start = editableText.getSpanStart(spanToRemove)
|
||||
val end = editableText.getSpanEnd(spanToRemove)
|
||||
// Must be done before text replacement
|
||||
editableText.removeSpan(spanToRemove)
|
||||
editableText.replace(start, end, "")
|
||||
spanToRemove = null
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue