Use proper API to insert mention from timeline user

This commit is contained in:
jonnyandrew 2023-06-19 13:41:08 +01:00
parent cb64175c2b
commit 9d239bf94d
No known key found for this signature in database
GPG Key ID: 0D58D4EF33D27015
2 changed files with 38 additions and 23 deletions

View File

@ -101,6 +101,7 @@ import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.onEach
import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.content.ContentAttachmentData import org.matrix.android.sdk.api.session.content.ContentAttachmentData
import org.matrix.android.sdk.api.session.permalinks.PermalinkService
import org.matrix.android.sdk.api.util.MatrixItem import org.matrix.android.sdk.api.util.MatrixItem
import reactivecircus.flowbinding.android.view.focusChanges import reactivecircus.flowbinding.android.view.focusChanges
import reactivecircus.flowbinding.android.widget.textChanges import reactivecircus.flowbinding.android.widget.textChanges
@ -123,6 +124,9 @@ class MessageComposerFragment : VectorBaseFragment<FragmentComposerBinding>(), A
@Inject lateinit var session: Session @Inject lateinit var session: Session
@Inject lateinit var errorTracker: ErrorTracker @Inject lateinit var errorTracker: ErrorTracker
private val permalinkService: PermalinkService
get() = session.permalinkService()
private val roomId: String get() = withState(timelineViewModel) { it.roomId } private val roomId: String get() = withState(timelineViewModel) { it.roomId }
private val autoCompleters: MutableMap<EditText, AutoCompleter> = hashMapOf() private val autoCompleters: MutableMap<EditText, AutoCompleter> = hashMapOf()
@ -792,30 +796,37 @@ class MessageComposerFragment : VectorBaseFragment<FragmentComposerBinding>(), A
} else { } else {
val roomMember = timelineViewModel.getMember(userId) val roomMember = timelineViewModel.getMember(userId)
val displayName = sanitizeDisplayName(roomMember?.displayName ?: userId) val displayName = sanitizeDisplayName(roomMember?.displayName ?: userId)
val pill = buildSpannedString { if ((composer as? RichTextComposerLayout)?.isTextFormattingEnabled == true) {
append(displayName) // Rich text editor is enabled so we need to use its APIs
setSpan( permalinkService.createPermalink(userId)?.let { url ->
PillImageSpan( (composer as RichTextComposerLayout).insertMention(url, displayName)
glideRequests, }
avatarRenderer, } else {
requireContext(), val pill = buildSpannedString {
MatrixItem.UserItem(userId, displayName, roomMember?.avatarUrl) append(displayName)
) setSpan(
.also { it.bind(composer.editText) }, PillImageSpan(
0, glideRequests,
displayName.length, avatarRenderer,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE requireContext(),
) MatrixItem.UserItem(userId, displayName, roomMember?.avatarUrl),
append(if (startToCompose) ": " else " ") )
.also { it.bind(composer.editText) },
0,
displayName.length,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
append(if (startToCompose) ": " else " ")
}
if (startToCompose && displayName.startsWith("/")) {
// Ensure displayName will not be interpreted as a Slash command
composer.editText.append("\\")
}
// Always use EditText.getText().insert for adding pills as TextView.append doesn't appear
// to upgrade to BufferType.Spannable as hinted at in the docs:
// https://developer.android.com/reference/android/widget/TextView#append(java.lang.CharSequence)
composer.editText.text.insert(composer.editText.selectionStart, pill)
} }
if (startToCompose && displayName.startsWith("/")) {
// Ensure displayName will not be interpreted as a Slash command
composer.editText.append("\\")
}
// Always use EditText.getText().insert for adding pills as TextView.append doesn't appear
// to upgrade to BufferType.Spannable as hinted at in the docs:
// https://developer.android.com/reference/android/widget/TextView#append(java.lang.CharSequence)
composer.editText.text.insert(composer.editText.selectionStart, pill)
} }
focusComposerAndShowKeyboard() focusComposerAndShowKeyboard()
} }

View File

@ -305,6 +305,10 @@ internal class RichTextComposerLayout @JvmOverloads constructor(
fun removeLink() = fun removeLink() =
views.richTextComposerEditText.removeLink() views.richTextComposerEditText.removeLink()
// TODO: update the API to insertMention when available
fun insertMention(url: String, displayText: String) =
views.richTextComposerEditText.insertLink(url, displayText)
@SuppressLint("ClickableViewAccessibility") @SuppressLint("ClickableViewAccessibility")
private fun disallowParentInterceptTouchEvent(view: View) { private fun disallowParentInterceptTouchEvent(view: View) {
view.setOnTouchListener { v, event -> view.setOnTouchListener { v, event ->