From 6112082d072e8e647b8e2627ad9b98233ffb84bc Mon Sep 17 00:00:00 2001 From: jonnyandrew Date: Fri, 18 Aug 2023 10:52:03 +0100 Subject: [PATCH] Update to new mentions API --- .../detail/composer/RichTextComposerLayout.kt | 16 ++++------ .../composer/mentions/PillDisplayHandler.kt | 27 ++++++---------- .../mentions/PillDisplayHandlerTest.kt | 31 +++++++------------ 3 files changed, 27 insertions(+), 47 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/composer/RichTextComposerLayout.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/composer/RichTextComposerLayout.kt index 48163a43bf..1b999b65c8 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/composer/RichTextComposerLayout.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/composer/RichTextComposerLayout.kt @@ -51,8 +51,7 @@ import im.vector.app.databinding.ViewRichTextMenuButtonBinding import im.vector.app.features.home.room.detail.composer.images.UriContentListener import im.vector.app.features.home.room.detail.composer.mentions.PillDisplayHandler import io.element.android.wysiwyg.EditorEditText -import io.element.android.wysiwyg.display.KeywordDisplayHandler -import io.element.android.wysiwyg.display.LinkDisplayHandler +import io.element.android.wysiwyg.display.MentionDisplayHandler import io.element.android.wysiwyg.display.TextDisplay import io.element.android.wysiwyg.utils.RustErrorCollector import io.element.android.wysiwyg.view.models.InlineFormat @@ -238,15 +237,12 @@ internal class RichTextComposerLayout @JvmOverloads constructor( views.composerEditTextOuterBorder.background = borderShapeDrawable setupRichTextMenu() - views.richTextComposerEditText.linkDisplayHandler = LinkDisplayHandler { text, url -> - pillDisplayHandler?.resolveLinkDisplay(text, url) ?: TextDisplay.Plain - } - views.richTextComposerEditText.keywordDisplayHandler = object : KeywordDisplayHandler { - override val keywords: List - get() = pillDisplayHandler?.keywords.orEmpty() + views.richTextComposerEditText.mentionDisplayHandler = object : MentionDisplayHandler { + override fun resolveMentionDisplay(text: String, url: String): TextDisplay = + pillDisplayHandler?.resolveMentionDisplay(text, url) ?: TextDisplay.Plain - override fun resolveKeywordDisplay(text: String): TextDisplay = - pillDisplayHandler?.resolveKeywordDisplay(text) ?: TextDisplay.Plain + override fun resolveAtRoomMentionDisplay(): TextDisplay = + pillDisplayHandler?.resolveAtRoomMentionDisplay() ?: TextDisplay.Plain } updateTextFieldBorder(isFullScreen) diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/composer/mentions/PillDisplayHandler.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/composer/mentions/PillDisplayHandler.kt index c2b71ea15b..e5e6aa347d 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/composer/mentions/PillDisplayHandler.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/composer/mentions/PillDisplayHandler.kt @@ -17,8 +17,7 @@ package im.vector.app.features.home.room.detail.composer.mentions import android.text.style.ReplacementSpan -import io.element.android.wysiwyg.display.KeywordDisplayHandler -import io.element.android.wysiwyg.display.LinkDisplayHandler +import io.element.android.wysiwyg.display.MentionDisplayHandler import io.element.android.wysiwyg.display.TextDisplay import org.matrix.android.sdk.api.session.permalinks.PermalinkData import org.matrix.android.sdk.api.session.permalinks.PermalinkParser @@ -30,16 +29,15 @@ import org.matrix.android.sdk.api.util.toMatrixItem import org.matrix.android.sdk.api.util.toRoomAliasMatrixItem /** - * A rich text editor [LinkDisplayHandler] and [KeywordDisplayHandler] - * that helps with replacing user and room links with pills. + * A rich text editor [MentionDisplayHandler] that helps with replacing user and room links with pills. */ internal class PillDisplayHandler( private val roomId: String, private val getRoom: (roomId: String) -> RoomSummary?, private val getMember: (userId: String) -> RoomMemberSummary?, private val replacementSpanFactory: (matrixItem: MatrixItem) -> ReplacementSpan, -) : LinkDisplayHandler, KeywordDisplayHandler { - override fun resolveLinkDisplay(text: String, url: String): TextDisplay { +) : MentionDisplayHandler { + override fun resolveMentionDisplay(text: String, url: String): TextDisplay { val matrixItem = when (val permalink = PermalinkParser.parse(url)) { is PermalinkData.UserLink -> { val userId = permalink.userId @@ -65,16 +63,9 @@ internal class PillDisplayHandler( return TextDisplay.Custom(customSpan = replacement) } - override val keywords: List - get() = listOf(MatrixItem.NOTIFY_EVERYONE) - - override fun resolveKeywordDisplay(text: String): TextDisplay = - when (text) { - MatrixItem.NOTIFY_EVERYONE -> { - val matrixItem = getRoom(roomId)?.toEveryoneInRoomMatrixItem() - ?: MatrixItem.EveryoneInRoomItem(roomId) - TextDisplay.Custom(replacementSpanFactory.invoke(matrixItem)) - } - else -> TextDisplay.Plain - } + override fun resolveAtRoomMentionDisplay(): TextDisplay { + val matrixItem = getRoom(roomId)?.toEveryoneInRoomMatrixItem() + ?: MatrixItem.EveryoneInRoomItem(roomId) + return TextDisplay.Custom(replacementSpanFactory.invoke(matrixItem)) + } } diff --git a/vector/src/test/java/im/vector/app/features/home/room/detail/composer/mentions/PillDisplayHandlerTest.kt b/vector/src/test/java/im/vector/app/features/home/room/detail/composer/mentions/PillDisplayHandlerTest.kt index 57c7aee420..be5a6a93ee 100644 --- a/vector/src/test/java/im/vector/app/features/home/room/detail/composer/mentions/PillDisplayHandlerTest.kt +++ b/vector/src/test/java/im/vector/app/features/home/room/detail/composer/mentions/PillDisplayHandlerTest.kt @@ -80,7 +80,7 @@ internal class PillDisplayHandlerTest { fun `when resolve non-matrix link, then it returns plain text`() { val subject = createSubject() - val result = subject.resolveLinkDisplay("text", NON_MATRIX_URL) + val result = subject.resolveMentionDisplay("text", NON_MATRIX_URL) assertEquals(TextDisplay.Plain, result) } @@ -89,7 +89,7 @@ internal class PillDisplayHandlerTest { fun `when resolve unknown user link, then it returns generic custom pill`() { val subject = createSubject() - val matrixItem = subject.resolveLinkDisplay("text", UNKNOWN_MATRIX_USER_URL) + val matrixItem = subject.resolveMentionDisplay("text", UNKNOWN_MATRIX_USER_URL) .getMatrixItem() assertEquals(MatrixItem.UserItem(UNKNOWN_MATRIX_USER_ID, UNKNOWN_MATRIX_USER_ID, null), matrixItem) @@ -99,7 +99,7 @@ internal class PillDisplayHandlerTest { fun `when resolve known user link, then it returns named custom pill`() { val subject = createSubject() - val matrixItem = subject.resolveLinkDisplay("text", KNOWN_MATRIX_USER_URL) + val matrixItem = subject.resolveMentionDisplay("text", KNOWN_MATRIX_USER_URL) .getMatrixItem() assertEquals(MatrixItem.UserItem(KNOWN_MATRIX_USER_ID, KNOWN_MATRIX_USER_NAME, KNOWN_MATRIX_USER_AVATAR), matrixItem) @@ -109,7 +109,7 @@ internal class PillDisplayHandlerTest { fun `when resolve unknown room link, then it returns generic custom pill`() { val subject = createSubject() - val matrixItem = subject.resolveLinkDisplay("text", UNKNOWN_MATRIX_ROOM_URL) + val matrixItem = subject.resolveMentionDisplay("text", UNKNOWN_MATRIX_ROOM_URL) .getMatrixItem() assertEquals(MatrixItem.RoomItem(UNKNOWN_MATRIX_ROOM_ID, UNKNOWN_MATRIX_ROOM_ID, null), matrixItem) @@ -119,7 +119,7 @@ internal class PillDisplayHandlerTest { fun `when resolve known room link, then it returns named custom pill`() { val subject = createSubject() - val matrixItem = subject.resolveLinkDisplay("text", KNOWN_MATRIX_ROOM_URL) + val matrixItem = subject.resolveMentionDisplay("text", KNOWN_MATRIX_ROOM_URL) .getMatrixItem() assertEquals(MatrixItem.RoomItem(KNOWN_MATRIX_ROOM_ID, KNOWN_MATRIX_ROOM_NAME, KNOWN_MATRIX_ROOM_AVATAR), matrixItem) @@ -129,7 +129,7 @@ internal class PillDisplayHandlerTest { fun `when resolve @room link, then it returns room notification custom pill`() { val subject = createSubject() - val matrixItem = subject.resolveLinkDisplay("@room", KNOWN_MATRIX_ROOM_URL) + val matrixItem = subject.resolveMentionDisplay("@room", KNOWN_MATRIX_ROOM_URL) .getMatrixItem() assertEquals(MatrixItem.EveryoneInRoomItem(KNOWN_MATRIX_ROOM_ID, NOTIFY_EVERYONE, KNOWN_MATRIX_ROOM_AVATAR, KNOWN_MATRIX_ROOM_NAME), matrixItem) @@ -139,7 +139,7 @@ internal class PillDisplayHandlerTest { fun `when resolve @room keyword, then it returns room notification custom pill`() { val subject = createSubject() - val matrixItem = subject.resolveKeywordDisplay("@room") + val matrixItem = subject.resolveAtRoomMentionDisplay() .getMatrixItem() assertEquals(MatrixItem.EveryoneInRoomItem(ROOM_ID, NOTIFY_EVERYONE, KNOWN_MATRIX_ROOM_AVATAR, KNOWN_MATRIX_ROOM_NAME), matrixItem) @@ -150,24 +150,17 @@ internal class PillDisplayHandlerTest { val subject = createSubject() every { mockGetRoom(ROOM_ID) } returns null - val matrixItem = subject.resolveKeywordDisplay("@room") + val matrixItem = subject.resolveAtRoomMentionDisplay() .getMatrixItem() assertEquals(MatrixItem.EveryoneInRoomItem(ROOM_ID, NOTIFY_EVERYONE, null, null), matrixItem) } - @Test - fun `when get keywords, then it returns @room`() { - val subject = createSubject() - - assertEquals(listOf("@room"), subject.keywords) - } - @Test fun `when resolve known user for custom domain link, then it returns named custom pill`() { val subject = createSubject() - val matrixItem = subject.resolveLinkDisplay("text", CUSTOM_DOMAIN_MATRIX_USER_URL) + val matrixItem = subject.resolveMentionDisplay("text", CUSTOM_DOMAIN_MATRIX_USER_URL) .getMatrixItem() assertEquals(MatrixItem.UserItem(KNOWN_MATRIX_USER_ID, KNOWN_MATRIX_USER_NAME, KNOWN_MATRIX_USER_AVATAR), matrixItem) @@ -177,7 +170,7 @@ internal class PillDisplayHandlerTest { fun `when resolve known room for custom domain link, then it returns named custom pill`() { val subject = createSubject() - val matrixItem = subject.resolveLinkDisplay("text", CUSTOM_DOMAIN_MATRIX_ROOM_URL) + val matrixItem = subject.resolveMentionDisplay("text", CUSTOM_DOMAIN_MATRIX_ROOM_URL) .getMatrixItem() assertEquals(MatrixItem.RoomItem(KNOWN_MATRIX_ROOM_ID, KNOWN_MATRIX_ROOM_NAME, KNOWN_MATRIX_ROOM_AVATAR), matrixItem) @@ -187,13 +180,13 @@ internal class PillDisplayHandlerTest { fun `when resolve known room with alias link, then it returns named custom pill`() { val subject = createSubject() - val matrixItem = subject.resolveLinkDisplay("text", KNOWN_MATRIX_ROOM_ALIAS_URL) + val matrixItem = subject.resolveMentionDisplay("text", KNOWN_MATRIX_ROOM_ALIAS_URL) .getMatrixItem() assertEquals(MatrixItem.RoomAliasItem(KNOWN_MATRIX_ROOM_ALIAS, KNOWN_MATRIX_ROOM_NAME, KNOWN_MATRIX_ROOM_AVATAR), matrixItem) } - private fun TextDisplay.getMatrixItem(): MatrixItem? { + private fun TextDisplay.getMatrixItem(): MatrixItem { val customSpan = this as? TextDisplay.Custom assertNotNull("The URL did not resolve to a custom link display method", customSpan)