Fix duplicated pills when pills contain other spans

Fixes following issues:
- Duplicated pills if the mention contains an image: https://github.com/SchildiChat/SchildiChat-android/issues/148
- Duplicated pills if these contain underscores: https://github.com/SchildiChat/SchildiChat-android/issues/156
This commit is contained in:
SpiritCroc 2022-11-01 11:21:27 +01:00
parent 76ead3f344
commit 411c8c9096
1 changed files with 14 additions and 0 deletions

View File

@ -83,6 +83,20 @@ class PillsPostProcessor @AssistedInject constructor(
val pillSpan = linkSpan.createPillSpan(roomId) ?: return@forEach
val startSpan = renderedText.getSpanStart(linkSpan)
val endSpan = renderedText.getSpanEnd(linkSpan)
// GlideImagesPlugin causes duplicated pills if we have a nested spans in the pill span,
// such as images or italic text.
// Accordingly, it's better to remove all spans that are contained in this span before rendering.
renderedText.getSpans(startSpan, endSpan, Any::class.java).forEach remove@{
if (it !is LinkSpan) {
// Make sure to only remove spans that are contained in this link, and not are bigger than this link, e.g. like reply-blocks
val start = renderedText.getSpanStart(it)
if (start < startSpan) return@remove
val end = renderedText.getSpanEnd(it)
if (end > endSpan) return@remove
renderedText.removeSpan(it)
}
}
addPillSpan(renderedText, pillSpan, startSpan, endSpan)
}
}