diff --git a/build.gradle b/build.gradle index 43348c63..51aa9d6c 100644 --- a/build.gradle +++ b/build.gradle @@ -66,6 +66,7 @@ ext { 'x-annotations' : 'androidx.annotation:annotation:1.1.0', 'x-recycler-view' : 'androidx.recyclerview:recyclerview:1.0.0', 'x-core' : 'androidx.core:core:1.0.2', + 'x-appcompat' : 'androidx.appcompat:appcompat:1.1.0', 'commonmark' : "com.atlassian.commonmark:commonmark:$commonMarkVersion", 'commonmark-strikethrough': "com.atlassian.commonmark:commonmark-ext-gfm-strikethrough:$commonMarkVersion", 'commonmark-table' : "com.atlassian.commonmark:commonmark-ext-gfm-tables:$commonMarkVersion", diff --git a/markwon-core/build.gradle b/markwon-core/build.gradle index 6ff67d77..8b2f1be6 100644 --- a/markwon-core/build.gradle +++ b/markwon-core/build.gradle @@ -22,6 +22,7 @@ dependencies { // @since 4.1.0 to allow PrecomputedTextSetterCompat // note that this dependency must be added on a client side explicitly compileOnly it['x-core'] + compileOnly it['x-appcompat'] } deps['test'].with { diff --git a/markwon-core/src/main/java/io/noties/markwon/PrecomputedFutureTextSetterCompat.java b/markwon-core/src/main/java/io/noties/markwon/PrecomputedFutureTextSetterCompat.java new file mode 100644 index 00000000..66efe187 --- /dev/null +++ b/markwon-core/src/main/java/io/noties/markwon/PrecomputedFutureTextSetterCompat.java @@ -0,0 +1,61 @@ +package io.noties.markwon; + +import android.text.Spanned; +import android.util.Log; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.appcompat.widget.AppCompatTextView; +import androidx.core.text.PrecomputedTextCompat; + +import java.util.concurrent.Executor; + +/** + * Please note this class requires `androidx.core:core` artifact being explicitly added to your dependencies. + * This is intended to be used in a RecyclerView. + * + * @see io.noties.markwon.Markwon.TextSetter + */ +public class PrecomputedFutureTextSetterCompat implements Markwon.TextSetter { + + /** + * @param executor for background execution of text pre-computation, + * if not provided the standard, single threaded one will be used. + */ + @NonNull + public static PrecomputedFutureTextSetterCompat create(@Nullable Executor executor) { + return new PrecomputedFutureTextSetterCompat(executor); + } + + @NonNull + public static PrecomputedFutureTextSetterCompat create() { + return new PrecomputedFutureTextSetterCompat(null); + } + + @Nullable + private final Executor executor; + + @SuppressWarnings("WeakerAccess") + PrecomputedFutureTextSetterCompat(@Nullable Executor executor) { + this.executor = executor; + } + + @Override + public void setText( + @NonNull TextView textView, + @NonNull Spanned markdown, + @NonNull TextView.BufferType bufferType, + @NonNull Runnable onComplete) { + + if (textView instanceof AppCompatTextView) { + ((AppCompatTextView) textView).setTextFuture(PrecomputedTextCompat.getTextFuture( + markdown, ((AppCompatTextView) textView).getTextMetricsParamsCompat(), executor + )); + + onComplete.run(); + } else { + Log.w("Markwon, no-op", "PrecomputedFutureTextSetterCompat: textView provided is not an AppCompatTextView!"); + } + } +} diff --git a/sample/build.gradle b/sample/build.gradle index 595fd54b..b87cab5e 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -54,6 +54,7 @@ dependencies { deps.with { implementation it['x-recycler-view'] implementation it['x-core'] // for precomputedTextCompat + implementation it['x-appcompat'] // for setTextFuture implementation it['okhttp'] implementation it['prism4j'] implementation it['debug'] diff --git a/sample/src/main/AndroidManifest.xml b/sample/src/main/AndroidManifest.xml index f85d8750..6b148bbc 100644 --- a/sample/src/main/AndroidManifest.xml +++ b/sample/src/main/AndroidManifest.xml @@ -28,6 +28,7 @@ + + \ No newline at end of file diff --git a/sample/src/main/res/values/strings-samples.xml b/sample/src/main/res/values/strings-samples.xml index 0305b471..1743aa35 100644 --- a/sample/src/main/res/values/strings-samples.xml +++ b/sample/src/main/res/values/strings-samples.xml @@ -25,6 +25,8 @@ # \# PrecomputedText\n\nUsage of TextSetter and PrecomputedTextCompat + # \# PrecomputedFutureText\n\nUsage of TextSetter and PrecomputedFutureTextSetterCompat + # \# Editor\n\n`MarkwonEditor` sample usage to highlight user input in EditText # \# Inline Parser\n\nUsage of custom inline parser