From 20c7d80bb1325b0213240e2154127950a4767a60 Mon Sep 17 00:00:00 2001 From: ganfra Date: Fri, 24 Jul 2020 11:30:27 +0200 Subject: [PATCH] Clean code --- .../timeline/factory/MessageItemFactory.kt | 30 ++++++++++++++----- .../riotx/features/html/EventHtmlRenderer.kt | 4 +-- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/factory/MessageItemFactory.kt b/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/factory/MessageItemFactory.kt index 88ad24a9e7..47a15327b6 100644 --- a/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/factory/MessageItemFactory.kt +++ b/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/factory/MessageItemFactory.kt @@ -353,17 +353,23 @@ class MessageItemFactory @Inject constructor( codeVisitor.visit(localFormattedBody) when (codeVisitor.codeKind) { CodeVisitor.Kind.BLOCK -> { - val codeFormattedBlock = htmlRenderer.get().render(localFormattedBody) ?: messageContent.formattedBody!! - buildCodeBlockItem(codeFormattedBlock, informationData, highlight, callback, attributes) + val codeFormattedBlock = htmlRenderer.get().render(localFormattedBody) + if (codeFormattedBlock == null) { + buildFormattedTextItem(messageContent, informationData, highlight, callback, attributes) + } else { + buildCodeBlockItem(codeFormattedBlock, informationData, highlight, callback, attributes) + } } CodeVisitor.Kind.INLINE -> { - val codeFormatted = htmlRenderer.get().render(localFormattedBody)?: messageContent.formattedBody!! - buildMessageTextItem(codeFormatted, false, informationData, highlight, callback, attributes) + val codeFormatted = htmlRenderer.get().render(localFormattedBody) + if (codeFormatted == null) { + buildFormattedTextItem(messageContent, informationData, highlight, callback, attributes) + } else { + buildMessageTextItem(codeFormatted, false, informationData, highlight, callback, attributes) + } } CodeVisitor.Kind.NONE -> { - val compressed = htmlCompressor.compress(messageContent.formattedBody!!) - val formattedBody = htmlRenderer.get().render(compressed) - buildMessageTextItem(formattedBody, true, informationData, highlight, callback, attributes) + buildFormattedTextItem(messageContent, informationData, highlight, callback, attributes) } } } else { @@ -371,6 +377,16 @@ class MessageItemFactory @Inject constructor( } } + private fun buildFormattedTextItem(messageContent: MessageTextContent, + informationData: MessageInformationData, + highlight: Boolean, + callback: TimelineEventController.Callback?, + attributes: AbsMessageItem.Attributes): MessageTextItem? { + val compressed = htmlCompressor.compress(messageContent.formattedBody!!) + val formattedBody = htmlRenderer.get().render(compressed) + return buildMessageTextItem(formattedBody, true, informationData, highlight, callback, attributes) + } + private fun buildMessageTextItem(body: CharSequence, isFormatted: Boolean, informationData: MessageInformationData, diff --git a/vector/src/main/java/im/vector/riotx/features/html/EventHtmlRenderer.kt b/vector/src/main/java/im/vector/riotx/features/html/EventHtmlRenderer.kt index f6cd898d44..8253669380 100644 --- a/vector/src/main/java/im/vector/riotx/features/html/EventHtmlRenderer.kt +++ b/vector/src/main/java/im/vector/riotx/features/html/EventHtmlRenderer.kt @@ -44,7 +44,7 @@ class EventHtmlRenderer @Inject constructor(context: Context, fun render(text: String): CharSequence { return try { markwon.toMarkdown(text) - }catch (failure: Throwable){ + } catch (failure: Throwable) { Timber.v("Fail to render $text to html") text } @@ -53,7 +53,7 @@ class EventHtmlRenderer @Inject constructor(context: Context, fun render(node: Node): CharSequence? { return try { markwon.render(node) - }catch (failure: Throwable){ + } catch (failure: Throwable) { Timber.v("Fail to render $node to html") return null }