Cleaner code

This commit is contained in:
Benoit Marty 2019-07-10 14:40:08 +02:00
parent e5082f662c
commit 06699eaefc
2 changed files with 22 additions and 37 deletions

View file

@ -82,52 +82,24 @@ internal class LocalEchoEventFactory @Inject constructor(private val credentials
text != htmlText && htmlText != "<p>${text.trim()}</p>\n" text != htmlText && htmlText != "<p>${text.trim()}</p>\n"
fun createFormattedTextEvent(roomId: String, textContent: TextContent): Event { fun createFormattedTextEvent(roomId: String, textContent: TextContent): Event {
val content = MessageTextContent( return createEvent(roomId, textContent.toMessageTextContent())
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)
} }
fun createReplaceTextEvent(roomId: String, fun createReplaceTextEvent(roomId: String,
targetEventId: String, targetEventId: String,
newBodyText: String, newBodyText: String,
newBodyAutoMarkdown: Boolean, newBodyAutoMarkdown: Boolean,
msgType: String, msgType: String,
compatibilityText: String): Event { compatibilityText: String): Event {
val newContent = if (newBodyAutoMarkdown) { return createEvent(roomId,
val document = parser.parse(newBodyText)
val htmlText = renderer.render(document)
if (isFormattedTextPertinent(newBodyText, htmlText)) {
MessageTextContent( MessageTextContent(
type = MessageType.MSGTYPE_TEXT, type = msgType,
format = MessageType.FORMAT_MATRIX_HTML, body = compatibilityText,
body = newBodyText, relatesTo = RelationDefaultContent(RelationType.REPLACE, targetEventId),
formattedBody = htmlText newContent = createTextContent(newBodyText, newBodyAutoMarkdown)
) .toMessageTextContent()
} else { .toContent()
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)
} }
fun createMediaEvent(roomId: String, attachment: ContentAttachmentData): Event { fun createMediaEvent(roomId: String, attachment: ContentAttachmentData): Event {

View file

@ -16,6 +16,9 @@
package im.vector.matrix.android.internal.session.room.send 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 * Contains a text and eventually a formatted text
*/ */
@ -26,3 +29,13 @@ data class TextContent(
) { ) {
fun takeFormatted() = formattedText ?: text 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
)
}