Add bufferType Markwon option and fix GIF in sample

This commit is contained in:
Dimitry Ivanov 2018-11-26 16:11:16 +03:00
parent 66bb33a76b
commit 6eb8e64d75
5 changed files with 28 additions and 14 deletions

View File

@ -9,7 +9,6 @@ import android.view.View;
import android.widget.TextView; import android.widget.TextView;
import pl.droidsonroids.gif.GifDrawable; import pl.droidsonroids.gif.GifDrawable;
import ru.noties.debug.Debug;
import ru.noties.markwon.spans.AsyncDrawableSpan; import ru.noties.markwon.spans.AsyncDrawableSpan;
public abstract class GifProcessor { public abstract class GifProcessor {
@ -26,21 +25,17 @@ public abstract class GifProcessor {
@Override @Override
public void process(@NonNull final TextView textView) { public void process(@NonNull final TextView textView) {
Debug.i("textView: %s", textView);
// here is what we will do additionally: // here is what we will do additionally:
// we query for all asyncDrawableSpans // we query for all asyncDrawableSpans
// we check if they are inside clickableSpan // we check if they are inside clickableSpan
// if not we apply onGifListener // if not we apply onGifListener
final Spannable spannable = spannable(textView); final Spannable spannable = spannable(textView);
Debug.i(spannable);
if (spannable == null) { if (spannable == null) {
return; return;
} }
Debug.i(spannable);
final AsyncDrawableSpan[] asyncDrawableSpans = final AsyncDrawableSpan[] asyncDrawableSpans =
spannable.getSpans(0, spannable.length(), AsyncDrawableSpan.class); spannable.getSpans(0, spannable.length(), AsyncDrawableSpan.class);
if (asyncDrawableSpans == null if (asyncDrawableSpans == null
@ -48,8 +43,6 @@ public abstract class GifProcessor {
return; return;
} }
Debug.i(asyncDrawableSpans);
int start; int start;
int end; int end;
ClickableSpan[] clickableSpans; ClickableSpan[] clickableSpans;
@ -59,8 +52,6 @@ public abstract class GifProcessor {
start = spannable.getSpanStart(asyncDrawableSpan); start = spannable.getSpanStart(asyncDrawableSpan);
end = spannable.getSpanEnd(asyncDrawableSpan); end = spannable.getSpanEnd(asyncDrawableSpan);
Debug.i(asyncDrawableSpan, start, end);
if (start < 0 if (start < 0
|| end < 0) { || end < 0) {
continue; continue;
@ -84,7 +75,6 @@ public abstract class GifProcessor {
@Nullable @Nullable
private static Spannable spannable(@NonNull TextView textView) { private static Spannable spannable(@NonNull TextView textView) {
final CharSequence charSequence = textView.getText(); final CharSequence charSequence = textView.getText();
Debug.i("type: %s, spanned: %s, spannable: %s", charSequence.getClass().getName(), charSequence instanceof Spanned, charSequence instanceof Spannable);
if (charSequence instanceof Spannable) { if (charSequence instanceof Spannable) {
return (Spannable) charSequence; return (Spannable) charSequence;
} }
@ -96,12 +86,11 @@ public abstract class GifProcessor {
@NonNull AsyncDrawableSpan span, @NonNull AsyncDrawableSpan span,
@NonNull GifAwareAsyncDrawable drawable) { @NonNull GifAwareAsyncDrawable drawable) {
Debug.i("textView: %s, span: %s, drawable: %s", textView, span, drawable);
// important thing here is to obtain new spannable from textView // important thing here is to obtain new spannable from textView
// as with each `setText()` new spannable is created and keeping reference // as with each `setText()` new spannable is created and keeping reference
// to an older one won't affect textView // to an older one won't affect textView
final Spannable spannable = spannable(textView); final Spannable spannable = spannable(textView);
if (spannable == null) { if (spannable == null) {
return; return;
} }

View File

@ -6,6 +6,7 @@ import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.text.method.LinkMovementMethod;
import android.view.View; import android.view.View;
import android.widget.TextView; import android.widget.TextView;
@ -66,6 +67,8 @@ public class MainActivity extends Activity {
appBarRenderer.render(appBarState()); appBarRenderer.render(appBarState());
textView.setMovementMethod(LinkMovementMethod.getInstance());
markdownLoader.load(uri(), new MarkdownLoader.OnMarkdownTextLoaded() { markdownLoader.load(uri(), new MarkdownLoader.OnMarkdownTextLoaded() {
@Override @Override
public void apply(final String text) { public void apply(final String text) {

View File

@ -29,6 +29,15 @@ public abstract class Markwon2 {
public interface Builder { public interface Builder {
/**
* Specify bufferType when applying text to a TextView {@code textView.setText(CharSequence,BufferType)}.
* By default `BufferType.SPANNABLE` is used
*
* @param bufferType BufferType
*/
@NonNull
Builder bufferType(@NonNull TextView.BufferType bufferType);
@NonNull @NonNull
Builder use(@NonNull MarkwonPlugin plugin); Builder use(@NonNull MarkwonPlugin plugin);

View File

@ -2,6 +2,7 @@ package ru.noties.markwon;
import android.content.Context; import android.content.Context;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.widget.TextView;
import org.commonmark.parser.Parser; import org.commonmark.parser.Parser;
@ -17,11 +18,19 @@ class MarkwonBuilderImpl implements Markwon2.Builder {
private final Context context; private final Context context;
private final List<MarkwonPlugin> plugins = new ArrayList<>(3); private final List<MarkwonPlugin> plugins = new ArrayList<>(3);
private TextView.BufferType bufferType = TextView.BufferType.SPANNABLE;
MarkwonBuilderImpl(@NonNull Context context) { MarkwonBuilderImpl(@NonNull Context context) {
this.context = context; this.context = context;
} }
@NonNull
@Override
public Markwon2.Builder bufferType(@NonNull TextView.BufferType bufferType) {
this.bufferType = bufferType;
return this;
}
@NonNull @NonNull
@Override @Override
public Markwon2.Builder use(@NonNull MarkwonPlugin plugin) { public Markwon2.Builder use(@NonNull MarkwonPlugin plugin) {
@ -52,6 +61,7 @@ class MarkwonBuilderImpl implements Markwon2.Builder {
asyncDrawableLoaderBuilder.build()); asyncDrawableLoaderBuilder.build());
return new MarkwonImpl( return new MarkwonImpl(
bufferType,
parserBuilder.build(), parserBuilder.build(),
visitorBuilder.build(configuration), visitorBuilder.build(configuration),
Collections.unmodifiableList(plugins) Collections.unmodifiableList(plugins)

View File

@ -10,14 +10,17 @@ import java.util.List;
class MarkwonImpl extends Markwon2 { class MarkwonImpl extends Markwon2 {
private final TextView.BufferType bufferType;
private final Parser parser; private final Parser parser;
private final MarkwonVisitor visitor; private final MarkwonVisitor visitor;
private final List<MarkwonPlugin> plugins; private final List<MarkwonPlugin> plugins;
MarkwonImpl( MarkwonImpl(
@NonNull TextView.BufferType bufferType,
@NonNull Parser parser, @NonNull Parser parser,
@NonNull MarkwonVisitor visitor, @NonNull MarkwonVisitor visitor,
@NonNull List<MarkwonPlugin> plugins) { @NonNull List<MarkwonPlugin> plugins) {
this.bufferType = bufferType;
this.parser = parser; this.parser = parser;
this.visitor = visitor; this.visitor = visitor;
this.plugins = plugins; this.plugins = plugins;
@ -59,7 +62,7 @@ class MarkwonImpl extends Markwon2 {
plugin.beforeSetText(textView, markdown); plugin.beforeSetText(textView, markdown);
} }
textView.setText(markdown); textView.setText(markdown, bufferType);
for (MarkwonPlugin plugin : plugins) { for (MarkwonPlugin plugin : plugins) {
plugin.afterSetText(textView); plugin.afterSetText(textView);