Skip to content

Commit

Permalink
Insert spaces around share spans when textifying
Browse files Browse the repository at this point in the history
When inserting images & other media into the chat input, we add spaces
around it to make sure the text doesn't touch the resulting URL. But if the
user edits the text after, the text might touch the image again. This
sometimes is hard to see, especially if the thumbnail and the text are on
separate lines.

To overcome this, also insert spaces around the URL when textifying.
  • Loading branch information
oakkitten committed Dec 25, 2023
1 parent 0a5726e commit 306496f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ class MediaAcceptingEditText : ActionEditText {
text?.let {
for (span in it.getSpans(0, it.length, ShareSpan::class.java)) {
span.suri.httpUri?.let { httpUri ->
it.replace(it.getSpanStart(span), it.getSpanEnd(span), httpUri)
val pos = it.getSpanStart(span)
it.replace(pos, it.getSpanEnd(span), "")
it.removeSpan(span)
insertAddingSpacesAsNeeded(pos, httpUri)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,12 @@ val NO_BITMAP: Bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ALPHA_8)

////////////////////////////////////////////////////////////////////////////////////////////////////


@MainThread fun EditText.insertAddingSpacesAsNeeded(insertAt: InsertAt, word: CharSequence) {
val pos = if (insertAt == InsertAt.CURRENT_POSITION) selectionEnd else text.length
insertAddingSpacesAsNeeded(pos, word)
}

@MainThread fun EditText.insertAddingSpacesAsNeeded(pos: Int, word: CharSequence) {
val wordStartsWithSpace = word.firstOrNull() == ' '
val wordEndsWithSpace = word.lastOrNull() == ' '
val spaceBeforeInsertLocation = pos > 0 && text[pos - 1] == ' '
Expand Down

0 comments on commit 306496f

Please sign in to comment.