Add bufferType Markwon option and fix GIF in sample
This commit is contained in:
		
							parent
							
								
									66bb33a76b
								
							
						
					
					
						commit
						6eb8e64d75
					
				| @ -9,7 +9,6 @@ import android.view.View; | ||||
| import android.widget.TextView; | ||||
| 
 | ||||
| import pl.droidsonroids.gif.GifDrawable; | ||||
| import ru.noties.debug.Debug; | ||||
| import ru.noties.markwon.spans.AsyncDrawableSpan; | ||||
| 
 | ||||
| public abstract class GifProcessor { | ||||
| @ -26,21 +25,17 @@ public abstract class GifProcessor { | ||||
|         @Override | ||||
|         public void process(@NonNull final TextView textView) { | ||||
| 
 | ||||
|             Debug.i("textView: %s", textView); | ||||
| 
 | ||||
|             // here is what we will do additionally: | ||||
|             // we query for all asyncDrawableSpans | ||||
|             // we check if they are inside clickableSpan | ||||
|             // if not we apply onGifListener | ||||
| 
 | ||||
|             final Spannable spannable = spannable(textView); | ||||
|             Debug.i(spannable); | ||||
| 
 | ||||
|             if (spannable == null) { | ||||
|                 return; | ||||
|             } | ||||
| 
 | ||||
|             Debug.i(spannable); | ||||
| 
 | ||||
|             final AsyncDrawableSpan[] asyncDrawableSpans = | ||||
|                     spannable.getSpans(0, spannable.length(), AsyncDrawableSpan.class); | ||||
|             if (asyncDrawableSpans == null | ||||
| @ -48,8 +43,6 @@ public abstract class GifProcessor { | ||||
|                 return; | ||||
|             } | ||||
| 
 | ||||
|             Debug.i(asyncDrawableSpans); | ||||
| 
 | ||||
|             int start; | ||||
|             int end; | ||||
|             ClickableSpan[] clickableSpans; | ||||
| @ -59,8 +52,6 @@ public abstract class GifProcessor { | ||||
|                 start = spannable.getSpanStart(asyncDrawableSpan); | ||||
|                 end = spannable.getSpanEnd(asyncDrawableSpan); | ||||
| 
 | ||||
|                 Debug.i(asyncDrawableSpan, start, end); | ||||
| 
 | ||||
|                 if (start < 0 | ||||
|                         || end < 0) { | ||||
|                     continue; | ||||
| @ -84,7 +75,6 @@ public abstract class GifProcessor { | ||||
|         @Nullable | ||||
|         private static Spannable spannable(@NonNull TextView textView) { | ||||
|             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) { | ||||
|                 return (Spannable) charSequence; | ||||
|             } | ||||
| @ -96,12 +86,11 @@ public abstract class GifProcessor { | ||||
|                 @NonNull AsyncDrawableSpan span, | ||||
|                 @NonNull GifAwareAsyncDrawable drawable) { | ||||
| 
 | ||||
|             Debug.i("textView: %s, span: %s, drawable: %s", textView, span, drawable); | ||||
| 
 | ||||
|             // important thing here is to obtain new spannable from textView | ||||
|             // as with each `setText()` new spannable is created and keeping reference | ||||
|             // to an older one won't affect textView | ||||
|             final Spannable spannable = spannable(textView); | ||||
| 
 | ||||
|             if (spannable == null) { | ||||
|                 return; | ||||
|             } | ||||
|  | ||||
| @ -6,6 +6,7 @@ import android.net.Uri; | ||||
| import android.os.Bundle; | ||||
| import android.support.annotation.NonNull; | ||||
| import android.support.annotation.Nullable; | ||||
| import android.text.method.LinkMovementMethod; | ||||
| import android.view.View; | ||||
| import android.widget.TextView; | ||||
| 
 | ||||
| @ -66,6 +67,8 @@ public class MainActivity extends Activity { | ||||
| 
 | ||||
|         appBarRenderer.render(appBarState()); | ||||
| 
 | ||||
|         textView.setMovementMethod(LinkMovementMethod.getInstance()); | ||||
| 
 | ||||
|         markdownLoader.load(uri(), new MarkdownLoader.OnMarkdownTextLoaded() { | ||||
|             @Override | ||||
|             public void apply(final String text) { | ||||
|  | ||||
| @ -29,6 +29,15 @@ public abstract class Markwon2 { | ||||
| 
 | ||||
|     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 | ||||
|         Builder use(@NonNull MarkwonPlugin plugin); | ||||
| 
 | ||||
|  | ||||
| @ -2,6 +2,7 @@ package ru.noties.markwon; | ||||
| 
 | ||||
| import android.content.Context; | ||||
| import android.support.annotation.NonNull; | ||||
| import android.widget.TextView; | ||||
| 
 | ||||
| import org.commonmark.parser.Parser; | ||||
| 
 | ||||
| @ -17,11 +18,19 @@ class MarkwonBuilderImpl implements Markwon2.Builder { | ||||
|     private final Context context; | ||||
| 
 | ||||
|     private final List<MarkwonPlugin> plugins = new ArrayList<>(3); | ||||
|     private TextView.BufferType bufferType = TextView.BufferType.SPANNABLE; | ||||
| 
 | ||||
|     MarkwonBuilderImpl(@NonNull Context context) { | ||||
|         this.context = context; | ||||
|     } | ||||
| 
 | ||||
|     @NonNull | ||||
|     @Override | ||||
|     public Markwon2.Builder bufferType(@NonNull TextView.BufferType bufferType) { | ||||
|         this.bufferType = bufferType; | ||||
|         return this; | ||||
|     } | ||||
| 
 | ||||
|     @NonNull | ||||
|     @Override | ||||
|     public Markwon2.Builder use(@NonNull MarkwonPlugin plugin) { | ||||
| @ -52,6 +61,7 @@ class MarkwonBuilderImpl implements Markwon2.Builder { | ||||
|                 asyncDrawableLoaderBuilder.build()); | ||||
| 
 | ||||
|         return new MarkwonImpl( | ||||
|                 bufferType, | ||||
|                 parserBuilder.build(), | ||||
|                 visitorBuilder.build(configuration), | ||||
|                 Collections.unmodifiableList(plugins) | ||||
|  | ||||
| @ -10,14 +10,17 @@ import java.util.List; | ||||
| 
 | ||||
| class MarkwonImpl extends Markwon2 { | ||||
| 
 | ||||
|     private final TextView.BufferType bufferType; | ||||
|     private final Parser parser; | ||||
|     private final MarkwonVisitor visitor; | ||||
|     private final List<MarkwonPlugin> plugins; | ||||
| 
 | ||||
|     MarkwonImpl( | ||||
|             @NonNull TextView.BufferType bufferType, | ||||
|             @NonNull Parser parser, | ||||
|             @NonNull MarkwonVisitor visitor, | ||||
|             @NonNull List<MarkwonPlugin> plugins) { | ||||
|         this.bufferType = bufferType; | ||||
|         this.parser = parser; | ||||
|         this.visitor = visitor; | ||||
|         this.plugins = plugins; | ||||
| @ -59,7 +62,7 @@ class MarkwonImpl extends Markwon2 { | ||||
|             plugin.beforeSetText(textView, markdown); | ||||
|         } | ||||
| 
 | ||||
|         textView.setText(markdown); | ||||
|         textView.setText(markdown, bufferType); | ||||
| 
 | ||||
|         for (MarkwonPlugin plugin : plugins) { | ||||
|             plugin.afterSetText(textView); | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Dimitry Ivanov
						Dimitry Ivanov