From 06699eaefce909d5ddd6dd24bcf62292d9b85a81 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Wed, 10 Jul 2019 14:40:08 +0200 Subject: [PATCH] Cleaner code --- .../room/send/LocalEchoEventFactory.kt | 46 ++++--------------- .../internal/session/room/send/TextContent.kt | 13 ++++++ 2 files changed, 22 insertions(+), 37 deletions(-) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/LocalEchoEventFactory.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/LocalEchoEventFactory.kt index 5f060284c3..cc449018ef 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/LocalEchoEventFactory.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/LocalEchoEventFactory.kt @@ -82,52 +82,24 @@ internal class LocalEchoEventFactory @Inject constructor(private val credentials text != htmlText && htmlText != "

${text.trim()}

\n" fun createFormattedTextEvent(roomId: String, textContent: TextContent): Event { - val content = MessageTextContent( - type = MessageType.MSGTYPE_TEXT, - format = if (textContent.formattedText == null) MessageType.FORMAT_MATRIX_HTML else null, - body = textContent.text, - formattedBody = textContent.formattedText - ) - return createEvent(roomId, content) + return createEvent(roomId, textContent.toMessageTextContent()) } - fun createReplaceTextEvent(roomId: String, targetEventId: String, newBodyText: String, newBodyAutoMarkdown: Boolean, msgType: String, compatibilityText: String): Event { - val newContent = if (newBodyAutoMarkdown) { - val document = parser.parse(newBodyText) - val htmlText = renderer.render(document) - if (isFormattedTextPertinent(newBodyText, htmlText)) { + return createEvent(roomId, MessageTextContent( - type = MessageType.MSGTYPE_TEXT, - format = MessageType.FORMAT_MATRIX_HTML, - body = newBodyText, - formattedBody = htmlText - ) - } else { - MessageTextContent( - type = MessageType.MSGTYPE_TEXT, - body = newBodyText - ) - } - } else { - MessageTextContent( - type = MessageType.MSGTYPE_TEXT, - body = newBodyText - ) - } - - val content = MessageTextContent( - type = msgType, - body = compatibilityText, - relatesTo = RelationDefaultContent(RelationType.REPLACE, targetEventId), - newContent = newContent.toContent() - ) - return createEvent(roomId, content) + type = msgType, + body = compatibilityText, + relatesTo = RelationDefaultContent(RelationType.REPLACE, targetEventId), + newContent = createTextContent(newBodyText, newBodyAutoMarkdown) + .toMessageTextContent() + .toContent() + )) } fun createMediaEvent(roomId: String, attachment: ContentAttachmentData): Event { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/TextContent.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/TextContent.kt index 554d84b38b..f1f29c20e8 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/TextContent.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/TextContent.kt @@ -16,6 +16,9 @@ package im.vector.matrix.android.internal.session.room.send +import im.vector.matrix.android.api.session.room.model.message.MessageTextContent +import im.vector.matrix.android.api.session.room.model.message.MessageType + /** * Contains a text and eventually a formatted text */ @@ -26,3 +29,13 @@ data class TextContent( ) { fun takeFormatted() = formattedText ?: text } + + +fun TextContent.toMessageTextContent(): MessageTextContent { + return MessageTextContent( + type = MessageType.MSGTYPE_TEXT, + format = MessageType.FORMAT_MATRIX_HTML.takeIf { formattedText != null }, + body = text, + formattedBody = formattedText + ) +}