mirror of
https://github.com/SchildiChat/SchildiChat-android.git
synced 2024-11-22 17:35:54 +03:00
Pills: cleanup and robustness
This commit is contained in:
parent
c412006f0e
commit
9f9c418085
1 changed files with 10 additions and 7 deletions
|
@ -66,27 +66,30 @@ class ComposerEditText @JvmOverloads constructor(context: Context, attrs: Attrib
|
|||
var spanToRemove: PillImageSpan? = null
|
||||
|
||||
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
|
||||
Timber.v("beforeTextChanged: start:$start count:$count after:$after")
|
||||
Timber.v("Pills: 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")
|
||||
Timber.v("Pills: beforeTextChanged: deleted char at $deleteCharPosition")
|
||||
|
||||
// Get span at this position
|
||||
val spans = editableText.getSpans(deleteCharPosition, deleteCharPosition, PillImageSpan::class.java)
|
||||
spanToRemove = spans.firstOrNull()
|
||||
// Get the first span at this position
|
||||
spanToRemove = editableText.getSpans(deleteCharPosition, deleteCharPosition, PillImageSpan::class.java)
|
||||
.also { Timber.v("Pills: beforeTextChanged: found ${it.size} span(s)") }
|
||||
.firstOrNull()
|
||||
}
|
||||
}
|
||||
|
||||
override fun afterTextChanged(s: Editable) {
|
||||
if (spanToRemove != null) {
|
||||
Timber.v("Removing the span")
|
||||
val start = editableText.getSpanStart(spanToRemove)
|
||||
val end = editableText.getSpanEnd(spanToRemove)
|
||||
Timber.v("Pills: afterTextChanged Removing the span start:$start end:$end")
|
||||
// Must be done before text replacement
|
||||
editableText.removeSpan(spanToRemove)
|
||||
editableText.replace(start, end, "")
|
||||
if (start != -1 && end != -1) {
|
||||
editableText.replace(start, end, "")
|
||||
}
|
||||
spanToRemove = null
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue