Skip to content

Commit 9d239bf

Browse files
committed
Use proper API to insert mention from timeline user
1 parent cb64175 commit 9d239bf

File tree

2 files changed

+38
-23
lines changed

2 files changed

+38
-23
lines changed

vector/src/main/java/im/vector/app/features/home/room/detail/composer/MessageComposerFragment.kt

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ import kotlinx.coroutines.flow.map
101101
import kotlinx.coroutines.flow.onEach
102102
import org.matrix.android.sdk.api.session.Session
103103
import org.matrix.android.sdk.api.session.content.ContentAttachmentData
104+
import org.matrix.android.sdk.api.session.permalinks.PermalinkService
104105
import org.matrix.android.sdk.api.util.MatrixItem
105106
import reactivecircus.flowbinding.android.view.focusChanges
106107
import reactivecircus.flowbinding.android.widget.textChanges
@@ -123,6 +124,9 @@ class MessageComposerFragment : VectorBaseFragment<FragmentComposerBinding>(), A
123124
@Inject lateinit var session: Session
124125
@Inject lateinit var errorTracker: ErrorTracker
125126

127+
private val permalinkService: PermalinkService
128+
get() = session.permalinkService()
129+
126130
private val roomId: String get() = withState(timelineViewModel) { it.roomId }
127131

128132
private val autoCompleters: MutableMap<EditText, AutoCompleter> = hashMapOf()
@@ -792,30 +796,37 @@ class MessageComposerFragment : VectorBaseFragment<FragmentComposerBinding>(), A
792796
} else {
793797
val roomMember = timelineViewModel.getMember(userId)
794798
val displayName = sanitizeDisplayName(roomMember?.displayName ?: userId)
795-
val pill = buildSpannedString {
796-
append(displayName)
797-
setSpan(
798-
PillImageSpan(
799-
glideRequests,
800-
avatarRenderer,
801-
requireContext(),
802-
MatrixItem.UserItem(userId, displayName, roomMember?.avatarUrl)
803-
)
804-
.also { it.bind(composer.editText) },
805-
0,
806-
displayName.length,
807-
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
808-
)
809-
append(if (startToCompose) ": " else " ")
810-
}
811-
if (startToCompose && displayName.startsWith("/")) {
812-
// Ensure displayName will not be interpreted as a Slash command
813-
composer.editText.append("\\")
799+
if ((composer as? RichTextComposerLayout)?.isTextFormattingEnabled == true) {
800+
// Rich text editor is enabled so we need to use its APIs
801+
permalinkService.createPermalink(userId)?.let { url ->
802+
(composer as RichTextComposerLayout).insertMention(url, displayName)
803+
}
804+
} else {
805+
val pill = buildSpannedString {
806+
append(displayName)
807+
setSpan(
808+
PillImageSpan(
809+
glideRequests,
810+
avatarRenderer,
811+
requireContext(),
812+
MatrixItem.UserItem(userId, displayName, roomMember?.avatarUrl),
813+
)
814+
.also { it.bind(composer.editText) },
815+
0,
816+
displayName.length,
817+
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
818+
)
819+
append(if (startToCompose) ": " else " ")
820+
}
821+
if (startToCompose && displayName.startsWith("/")) {
822+
// Ensure displayName will not be interpreted as a Slash command
823+
composer.editText.append("\\")
824+
}
825+
// Always use EditText.getText().insert for adding pills as TextView.append doesn't appear
826+
// to upgrade to BufferType.Spannable as hinted at in the docs:
827+
// https://developer.android.com/reference/android/widget/TextView#append(java.lang.CharSequence)
828+
composer.editText.text.insert(composer.editText.selectionStart, pill)
814829
}
815-
// Always use EditText.getText().insert for adding pills as TextView.append doesn't appear
816-
// to upgrade to BufferType.Spannable as hinted at in the docs:
817-
// https://developer.android.com/reference/android/widget/TextView#append(java.lang.CharSequence)
818-
composer.editText.text.insert(composer.editText.selectionStart, pill)
819830
}
820831
focusComposerAndShowKeyboard()
821832
}

vector/src/main/java/im/vector/app/features/home/room/detail/composer/RichTextComposerLayout.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,10 @@ internal class RichTextComposerLayout @JvmOverloads constructor(
305305
fun removeLink() =
306306
views.richTextComposerEditText.removeLink()
307307

308+
// TODO: update the API to insertMention when available
309+
fun insertMention(url: String, displayText: String) =
310+
views.richTextComposerEditText.insertLink(url, displayText)
311+
308312
@SuppressLint("ClickableViewAccessibility")
309313
private fun disallowParentInterceptTouchEvent(view: View) {
310314
view.setOnTouchListener { v, event ->

0 commit comments

Comments
 (0)