diff --git a/app-sample/samples.json b/app-sample/samples.json index ab7eb06a..7a6ad3c4 100644 --- a/app-sample/samples.json +++ b/app-sample/samples.json @@ -1,4 +1,16 @@ [ + { + "javaClassName": "io.noties.markwon.app.samples.CacheMarkwonSample", + "id": "202007189102458", + "title": "Cache Markwon instance", + "description": "A static cache for `Markwon` instance to be associated with a `Context`", + "artifacts": [ + "CORE" + ], + "tags": [ + "cache" + ] + }, { "javaClassName": "io.noties.markwon.app.samples.tasklist.TaskListMutateSample", "id": "202007184140901", @@ -756,12 +768,13 @@ ] }, { - "javaClassName": "io.noties.markwon.app.samples.GithubUserIssueOnTextAddedSample", + "javaClassName": "io.noties.markwon.app.samples.GithubUserIssueInlineParsingSample", "id": "202006181162024", "title": "User mention and issue (via text)", "description": "Github-like user mention and issue rendering via `CorePlugin.OnTextAddedListener`", "artifacts": [ - "CORE" + "CORE", + "INLINE_PARSER" ], "tags": [ "parsing", @@ -770,13 +783,12 @@ ] }, { - "javaClassName": "io.noties.markwon.app.samples.GithubUserIssueInlineParsingSample", + "javaClassName": "io.noties.markwon.app.samples.GithubUserIssueOnTextAddedSample", "id": "202006181162024", "title": "User mention and issue (via text)", "description": "Github-like user mention and issue rendering via `CorePlugin.OnTextAddedListener`", "artifacts": [ - "CORE", - "INLINE_PARSER" + "CORE" ], "tags": [ "parsing", diff --git a/app-sample/src/main/java/io/noties/markwon/app/sample/Tags.kt b/app-sample/src/main/java/io/noties/markwon/app/sample/Tags.kt index 595d5453..3c3d9f51 100644 --- a/app-sample/src/main/java/io/noties/markwon/app/sample/Tags.kt +++ b/app-sample/src/main/java/io/noties/markwon/app/sample/Tags.kt @@ -33,4 +33,5 @@ object Tags { const val html = "HTML" const val knownBug = "known-bug" const val precomputedText = "precomputed-text" + const val cache = "cache" } \ No newline at end of file diff --git a/app-sample/src/main/java/io/noties/markwon/app/samples/CacheMarkwonSample.kt b/app-sample/src/main/java/io/noties/markwon/app/samples/CacheMarkwonSample.kt new file mode 100644 index 00000000..ccac4534 --- /dev/null +++ b/app-sample/src/main/java/io/noties/markwon/app/samples/CacheMarkwonSample.kt @@ -0,0 +1,59 @@ +package io.noties.markwon.app.samples + +import android.content.Context +import io.noties.debug.Debug +import io.noties.markwon.Markwon +import io.noties.markwon.app.sample.Tags +import io.noties.markwon.app.sample.ui.MarkwonTextViewSample +import io.noties.markwon.ext.strikethrough.StrikethroughPlugin +import io.noties.markwon.sample.annotations.MarkwonArtifact +import io.noties.markwon.sample.annotations.MarkwonSampleInfo +import java.util.Collections +import java.util.WeakHashMap + +@MarkwonSampleInfo( + id = "202007189102458", + title = "Cache Markwon instance", + description = "A static cache for `Markwon` instance " + + "to be associated with a `Context`", + artifacts = [MarkwonArtifact.CORE], + tags = [Tags.cache] +) +class CacheMarkwonSample : MarkwonTextViewSample() { + override fun render() { + render("# First!") + render("## Second!!") + render("### Third!!!") + } + + fun render(md: String) { + val markwon = MarkwonCache.with(context) + Debug.i("markwon: ${markwon.hashCode()}, md: '$md'") + markwon.setMarkdown(textView, md) + } +} + +object MarkwonCache { + private val cache = Collections.synchronizedMap(WeakHashMap()) + + fun with(context: Context): Markwon { + // yeah, why work as expected? new value is returned each time, no caching occur + // kotlin: 1.3.72 + // intellij plugin: 1.3.72-release-Studio4.0-5 +// return cache.getOrPut(context) { +// // create your markwon instance here +// return Markwon.builder(context) +// .usePlugin(StrikethroughPlugin.create()) +// .build() +// } + + return cache[context] ?: { + Markwon.builder(context) + .usePlugin(StrikethroughPlugin.create()) + .build() + .also { + cache[context] = it + } + }.invoke() + } +} \ No newline at end of file diff --git a/art/sample-app.epgz b/art/sample-app.epgz index d5d47b94..65a84969 100644 Binary files a/art/sample-app.epgz and b/art/sample-app.epgz differ