diff --git a/app/src/main/java/com/stand/standapp/ui/chat/components/ChatArea.kt b/app/src/main/java/com/stand/standapp/ui/chat/components/ChatArea.kt index b37759d..11e6d3a 100644 --- a/app/src/main/java/com/stand/standapp/ui/chat/components/ChatArea.kt +++ b/app/src/main/java/com/stand/standapp/ui/chat/components/ChatArea.kt @@ -173,15 +173,7 @@ fun MarkwonRenderer(markdown: String, modifier: Modifier) { setPadding(0, 0, 0, 0) } - val markwon = io.noties.markwon.Markwon.builder(ctx) - .usePlugin(io.noties.markwon.core.CorePlugin.create()) - .usePlugin(io.noties.markwon.ext.tables.TablePlugin.create(ctx)) - .usePlugin(io.noties.markwon.html.HtmlPlugin.create()) - .usePlugin(io.noties.markwon.linkify.LinkifyPlugin.create()) - .usePlugin(io.noties.markwon.ext.strikethrough.StrikethroughPlugin.create()) - .usePlugin(io.noties.markwon.ext.tasklist.TaskListPlugin.create(ctx)) - .build() - + val markwon = MarkwonHolder.get(ctx) textView.tag = markwon textView }, @@ -200,3 +192,26 @@ fun MarkwonRenderer(markdown: String, modifier: Modifier) { modifier = modifier ) } + +private object MarkwonHolder { + @Volatile + private var instance: io.noties.markwon.Markwon? = null + + fun get(context: android.content.Context): io.noties.markwon.Markwon { + instance?.let { return it } + return synchronized(this) { + instance ?: build(context.applicationContext).also { instance = it } + } + } + + private fun build(context: android.content.Context): io.noties.markwon.Markwon { + return io.noties.markwon.Markwon.builder(context) + .usePlugin(io.noties.markwon.core.CorePlugin.create()) + .usePlugin(io.noties.markwon.ext.tables.TablePlugin.create(context)) + .usePlugin(io.noties.markwon.html.HtmlPlugin.create()) + .usePlugin(io.noties.markwon.linkify.LinkifyPlugin.create()) + .usePlugin(io.noties.markwon.ext.strikethrough.StrikethroughPlugin.create()) + .usePlugin(io.noties.markwon.ext.tasklist.TaskListPlugin.create(context)) + .build() + } +}