From 7bcbd2181afc3335f58a73bc99d3c21ef4b5fbf8 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Mon, 4 Nov 2024 16:04:38 +0100 Subject: [PATCH] Improve last message formatter: add caption (or filename) --- .../impl/DefaultRoomLastMessageFormatter.kt | 16 ++++--- .../DefaultRoomLastMessageFormatterTest.kt | 44 ++++++++++++++----- 2 files changed, 41 insertions(+), 19 deletions(-) diff --git a/libraries/eventformatter/impl/src/main/kotlin/io/element/android/libraries/eventformatter/impl/DefaultRoomLastMessageFormatter.kt b/libraries/eventformatter/impl/src/main/kotlin/io/element/android/libraries/eventformatter/impl/DefaultRoomLastMessageFormatter.kt index 6b43fc3607..4031915445 100644 --- a/libraries/eventformatter/impl/src/main/kotlin/io/element/android/libraries/eventformatter/impl/DefaultRoomLastMessageFormatter.kt +++ b/libraries/eventformatter/impl/src/main/kotlin/io/element/android/libraries/eventformatter/impl/DefaultRoomLastMessageFormatter.kt @@ -110,25 +110,27 @@ class DefaultRoomLastMessageFormatter @Inject constructor( messageType.toPlainText(permalinkParser) } is VideoMessageType -> { - sp.getString(CommonStrings.common_video) + messageType.bestDescription.prefixWith(sp.getString(CommonStrings.common_video)) } is ImageMessageType -> { - sp.getString(CommonStrings.common_image) + messageType.bestDescription.prefixWith(sp.getString(CommonStrings.common_image)) } is StickerMessageType -> { - sp.getString(CommonStrings.common_sticker) + messageType.bestDescription.prefixWith(sp.getString(CommonStrings.common_sticker)) } is LocationMessageType -> { sp.getString(CommonStrings.common_shared_location) } is FileMessageType -> { - sp.getString(CommonStrings.common_file) + messageType.bestDescription.prefixWith(sp.getString(CommonStrings.common_file)) } is AudioMessageType -> { - sp.getString(CommonStrings.common_audio) + messageType.bestDescription.prefixWith(sp.getString(CommonStrings.common_audio)) } is VoiceMessageType -> { - sp.getString(CommonStrings.common_voice_message) + // In this case, do not use bestDescription, because the filename is useless, only use the caption if available. + messageType.caption?.prefixWith(sp.getString(CommonStrings.common_voice_message)) + ?: sp.getString(CommonStrings.common_voice_message) } is OtherMessageType -> { messageType.body @@ -140,7 +142,7 @@ class DefaultRoomLastMessageFormatter @Inject constructor( return message.prefixIfNeeded(senderDisambiguatedDisplayName, isDmRoom, isOutgoing) } - private fun String.prefixIfNeeded( + private fun CharSequence.prefixIfNeeded( senderDisambiguatedDisplayName: String, isDmRoom: Boolean, isOutgoing: Boolean, diff --git a/libraries/eventformatter/impl/src/test/kotlin/io/element/android/libraries/eventformatter/impl/DefaultRoomLastMessageFormatterTest.kt b/libraries/eventformatter/impl/src/test/kotlin/io/element/android/libraries/eventformatter/impl/DefaultRoomLastMessageFormatterTest.kt index efc748d10f..3c5038069a 100644 --- a/libraries/eventformatter/impl/src/test/kotlin/io/element/android/libraries/eventformatter/impl/DefaultRoomLastMessageFormatterTest.kt +++ b/libraries/eventformatter/impl/src/test/kotlin/io/element/android/libraries/eventformatter/impl/DefaultRoomLastMessageFormatterTest.kt @@ -208,32 +208,51 @@ class DefaultRoomLastMessageFormatterTest { // Verify results of DM mode for ((type, result) in resultsInDm) { + val string = result.toString() val expectedResult = when (type) { - is VideoMessageType -> "Video" - is AudioMessageType -> "Audio" + is VideoMessageType -> "Video: Shared body" + is AudioMessageType -> "Audio: Shared body" is VoiceMessageType -> "Voice message" - is ImageMessageType -> "Image" - is StickerMessageType -> "Sticker" - is FileMessageType -> "File" + is ImageMessageType -> "Image: Shared body" + is StickerMessageType -> "Sticker: Shared body" + is FileMessageType -> "File: Shared body" is LocationMessageType -> "Shared location" is EmoteMessageType -> "* $senderName ${type.body}" is TextMessageType, is NoticeMessageType, is OtherMessageType -> body } - assertWithMessage("$type was not properly handled for DM").that(result).isEqualTo(expectedResult) + val shouldCreateAnnotatedString = when (type) { + is VideoMessageType -> true + is AudioMessageType -> true + is VoiceMessageType -> false + is ImageMessageType -> true + is StickerMessageType -> true + is FileMessageType -> true + is LocationMessageType -> false + is EmoteMessageType -> false + is TextMessageType -> false + is NoticeMessageType -> false + is OtherMessageType -> false + } + if (shouldCreateAnnotatedString) { + assertWithMessage("$type doesn't produce an AnnotatedString") + .that(result) + .isInstanceOf(AnnotatedString::class.java) + } + assertWithMessage("$type was not properly handled for DM").that(string).isEqualTo(expectedResult) } // Verify results of Room mode for ((type, result) in resultsInRoom) { val string = result.toString() val expectedResult = when (type) { - is VideoMessageType -> "$expectedPrefix: Video" - is AudioMessageType -> "$expectedPrefix: Audio" + is VideoMessageType -> "$expectedPrefix: Video: Shared body" + is AudioMessageType -> "$expectedPrefix: Audio: Shared body" is VoiceMessageType -> "$expectedPrefix: Voice message" - is ImageMessageType -> "$expectedPrefix: Image" - is StickerMessageType -> "$expectedPrefix: Sticker" - is FileMessageType -> "$expectedPrefix: File" + is ImageMessageType -> "$expectedPrefix: Image: Shared body" + is StickerMessageType -> "$expectedPrefix: Sticker: Shared body" + is FileMessageType -> "$expectedPrefix: File: Shared body" is LocationMessageType -> "$expectedPrefix: Shared location" is TextMessageType, is NoticeMessageType, @@ -249,7 +268,8 @@ class DefaultRoomLastMessageFormatterTest { is FileMessageType -> true is LocationMessageType -> false is EmoteMessageType -> false - is TextMessageType, is NoticeMessageType -> true + is TextMessageType -> true + is NoticeMessageType -> true is OtherMessageType -> true } if (shouldCreateAnnotatedString) {