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"
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 {

View File

@ -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
)
}