mirror of
https://github.com/SchildiChat/SchildiChat-android.git
synced 2024-11-26 19:36:08 +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
|
var spanToRemove: PillImageSpan? = null
|
||||||
|
|
||||||
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
|
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) {
|
if (count > after) {
|
||||||
// A char has been deleted
|
// A char has been deleted
|
||||||
val deleteCharPosition = start + count
|
val deleteCharPosition = start + count
|
||||||
Timber.v("beforeTextChanged: deleted char at $deleteCharPosition")
|
Timber.v("Pills: beforeTextChanged: deleted char at $deleteCharPosition")
|
||||||
|
|
||||||
// Get span at this position
|
// Get the first span at this position
|
||||||
val spans = editableText.getSpans(deleteCharPosition, deleteCharPosition, PillImageSpan::class.java)
|
spanToRemove = editableText.getSpans(deleteCharPosition, deleteCharPosition, PillImageSpan::class.java)
|
||||||
spanToRemove = spans.firstOrNull()
|
.also { Timber.v("Pills: beforeTextChanged: found ${it.size} span(s)") }
|
||||||
|
.firstOrNull()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun afterTextChanged(s: Editable) {
|
override fun afterTextChanged(s: Editable) {
|
||||||
if (spanToRemove != null) {
|
if (spanToRemove != null) {
|
||||||
Timber.v("Removing the span")
|
|
||||||
val start = editableText.getSpanStart(spanToRemove)
|
val start = editableText.getSpanStart(spanToRemove)
|
||||||
val end = editableText.getSpanEnd(spanToRemove)
|
val end = editableText.getSpanEnd(spanToRemove)
|
||||||
|
Timber.v("Pills: afterTextChanged Removing the span start:$start end:$end")
|
||||||
// Must be done before text replacement
|
// Must be done before text replacement
|
||||||
editableText.removeSpan(spanToRemove)
|
editableText.removeSpan(spanToRemove)
|
||||||
|
if (start != -1 && end != -1) {
|
||||||
editableText.replace(start, end, "")
|
editableText.replace(start, end, "")
|
||||||
|
}
|
||||||
spanToRemove = null
|
spanToRemove = null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue