Moved LinkResolver to independent entity
This commit is contained in:
		
							parent
							
								
									213f5cf281
								
							
						
					
					
						commit
						ffb5848c3c
					
				@ -10,6 +10,7 @@
 | 
			
		||||
* removed MarkwonPlugin#configureHtmlRenderer -> now part of HtmlPlugin
 | 
			
		||||
* TagHandler now has `supportedTags()` method
 | 
			
		||||
* html is moved completely to html-plugin
 | 
			
		||||
* OnTextAddedListener
 | 
			
		||||
* OnTextAddedListener (CorePlugin)
 | 
			
		||||
* ImageSizeResolver signature change (accept AsyncDrawable)
 | 
			
		||||
* JLatexMathPlugin builder has vertical & horizontal padding
 | 
			
		||||
* JLatexMathPlugin builder has vertical & horizontal padding
 | 
			
		||||
* LinkResolver is now an independent entity (previously part of LinkSpan)
 | 
			
		||||
@ -0,0 +1,14 @@
 | 
			
		||||
package io.noties.markwon;
 | 
			
		||||
 | 
			
		||||
import android.view.View;
 | 
			
		||||
 | 
			
		||||
import androidx.annotation.NonNull;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @see LinkResolverDef
 | 
			
		||||
 * @see MarkwonConfiguration.Builder#linkResolver(LinkResolver)
 | 
			
		||||
 * @since 4.0.0-SNAPSHOT
 | 
			
		||||
 */
 | 
			
		||||
public interface LinkResolver {
 | 
			
		||||
    void resolve(@NonNull View view, @NonNull String link);
 | 
			
		||||
}
 | 
			
		||||
@ -10,11 +10,9 @@ import android.view.View;
 | 
			
		||||
 | 
			
		||||
import androidx.annotation.NonNull;
 | 
			
		||||
 | 
			
		||||
import io.noties.markwon.core.spans.LinkSpan;
 | 
			
		||||
 | 
			
		||||
public class LinkResolverDef implements LinkSpan.Resolver {
 | 
			
		||||
public class LinkResolverDef implements LinkResolver {
 | 
			
		||||
    @Override
 | 
			
		||||
    public void resolve(View view, @NonNull String link) {
 | 
			
		||||
    public void resolve(@NonNull View view, @NonNull String link) {
 | 
			
		||||
        final Uri uri = Uri.parse(link);
 | 
			
		||||
        final Context context = view.getContext();
 | 
			
		||||
        final Intent intent = new Intent(Intent.ACTION_VIEW, uri);
 | 
			
		||||
 | 
			
		||||
@ -3,7 +3,6 @@ package io.noties.markwon;
 | 
			
		||||
import androidx.annotation.NonNull;
 | 
			
		||||
 | 
			
		||||
import io.noties.markwon.core.MarkwonTheme;
 | 
			
		||||
import io.noties.markwon.core.spans.LinkSpan;
 | 
			
		||||
import io.noties.markwon.image.AsyncDrawableLoader;
 | 
			
		||||
import io.noties.markwon.image.ImageSizeResolver;
 | 
			
		||||
import io.noties.markwon.image.ImageSizeResolverDef;
 | 
			
		||||
@ -26,7 +25,7 @@ public class MarkwonConfiguration {
 | 
			
		||||
    private final MarkwonTheme theme;
 | 
			
		||||
    private final AsyncDrawableLoader asyncDrawableLoader;
 | 
			
		||||
    private final SyntaxHighlight syntaxHighlight;
 | 
			
		||||
    private final LinkSpan.Resolver linkResolver;
 | 
			
		||||
    private final LinkResolver linkResolver;
 | 
			
		||||
    private final UrlProcessor urlProcessor;
 | 
			
		||||
    private final ImageSizeResolver imageSizeResolver;
 | 
			
		||||
 | 
			
		||||
@ -59,7 +58,7 @@ public class MarkwonConfiguration {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @NonNull
 | 
			
		||||
    public LinkSpan.Resolver linkResolver() {
 | 
			
		||||
    public LinkResolver linkResolver() {
 | 
			
		||||
        return linkResolver;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -87,7 +86,7 @@ public class MarkwonConfiguration {
 | 
			
		||||
        private MarkwonTheme theme;
 | 
			
		||||
        private AsyncDrawableLoader asyncDrawableLoader;
 | 
			
		||||
        private SyntaxHighlight syntaxHighlight;
 | 
			
		||||
        private LinkSpan.Resolver linkResolver;
 | 
			
		||||
        private LinkResolver linkResolver;
 | 
			
		||||
        private UrlProcessor urlProcessor;
 | 
			
		||||
        private ImageSizeResolver imageSizeResolver;
 | 
			
		||||
        private MarkwonSpansFactory spansFactory;
 | 
			
		||||
@ -111,7 +110,7 @@ public class MarkwonConfiguration {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        @NonNull
 | 
			
		||||
        public Builder linkResolver(@NonNull LinkSpan.Resolver linkResolver) {
 | 
			
		||||
        public Builder linkResolver(@NonNull LinkResolver linkResolver) {
 | 
			
		||||
            this.linkResolver = linkResolver;
 | 
			
		||||
            return this;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@ -6,19 +6,19 @@ import android.view.View;
 | 
			
		||||
 | 
			
		||||
import androidx.annotation.NonNull;
 | 
			
		||||
 | 
			
		||||
import io.noties.markwon.LinkResolver;
 | 
			
		||||
import io.noties.markwon.core.MarkwonTheme;
 | 
			
		||||
 | 
			
		||||
public class LinkSpan extends URLSpan {
 | 
			
		||||
 | 
			
		||||
    public interface Resolver {
 | 
			
		||||
        void resolve(View view, @NonNull String link);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private final MarkwonTheme theme;
 | 
			
		||||
    private final String link;
 | 
			
		||||
    private final Resolver resolver;
 | 
			
		||||
    private final LinkResolver resolver;
 | 
			
		||||
 | 
			
		||||
    public LinkSpan(@NonNull MarkwonTheme theme, @NonNull String link, @NonNull Resolver resolver) {
 | 
			
		||||
    public LinkSpan(
 | 
			
		||||
            @NonNull MarkwonTheme theme,
 | 
			
		||||
            @NonNull String link,
 | 
			
		||||
            @NonNull LinkResolver resolver) {
 | 
			
		||||
        super(link);
 | 
			
		||||
        this.theme = theme;
 | 
			
		||||
        this.link = link;
 | 
			
		||||
 | 
			
		||||
@ -5,9 +5,10 @@ import android.graphics.Rect;
 | 
			
		||||
import androidx.annotation.NonNull;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @see ImageSizeResolverDef
 | 
			
		||||
 * @see io.noties.markwon.MarkwonConfiguration.Builder#imageSizeResolver(ImageSizeResolver)
 | 
			
		||||
 * @since 1.0.1
 | 
			
		||||
 */
 | 
			
		||||
@SuppressWarnings({"WeakerAccess", "unused"})
 | 
			
		||||
public abstract class ImageSizeResolver {
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
 | 
			
		||||
@ -1,16 +1,22 @@
 | 
			
		||||
package io.noties.markwon.image;
 | 
			
		||||
 | 
			
		||||
import android.graphics.Rect;
 | 
			
		||||
import android.graphics.drawable.Drawable;
 | 
			
		||||
 | 
			
		||||
import org.junit.Before;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
import org.junit.runner.RunWith;
 | 
			
		||||
import org.mockito.ArgumentCaptor;
 | 
			
		||||
import org.mockito.Mockito;
 | 
			
		||||
import org.robolectric.RobolectricTestRunner;
 | 
			
		||||
import org.robolectric.annotation.Config;
 | 
			
		||||
 | 
			
		||||
import static org.junit.Assert.assertEquals;
 | 
			
		||||
import static io.noties.markwon.image.ImageSizeResolverDef.UNIT_EM;
 | 
			
		||||
import static io.noties.markwon.image.ImageSizeResolverDef.UNIT_PERCENT;
 | 
			
		||||
import static org.junit.Assert.assertEquals;
 | 
			
		||||
import static org.mockito.Mockito.mock;
 | 
			
		||||
import static org.mockito.Mockito.verify;
 | 
			
		||||
import static org.mockito.Mockito.when;
 | 
			
		||||
 | 
			
		||||
@RunWith(RobolectricTestRunner.class)
 | 
			
		||||
@Config(manifest = Config.NONE)
 | 
			
		||||
@ -23,6 +29,42 @@ public class ImageSizeResolverDefTest {
 | 
			
		||||
        def = new ImageSizeResolverDef();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void correct_redirect() {
 | 
			
		||||
        // @since 4.0.0-SNAPSHOT the main method is changed to accept AsyncDrawable
 | 
			
		||||
 | 
			
		||||
        final ImageSizeResolverDef def = mock(ImageSizeResolverDef.class, Mockito.CALLS_REAL_METHODS);
 | 
			
		||||
        final AsyncDrawable drawable = mock(AsyncDrawable.class);
 | 
			
		||||
 | 
			
		||||
        final ImageSize imageSize = mock(ImageSize.class);
 | 
			
		||||
        final Drawable result = mock(Drawable.class);
 | 
			
		||||
        final Rect rect = mock(Rect.class);
 | 
			
		||||
        when(result.getBounds()).thenReturn(rect);
 | 
			
		||||
 | 
			
		||||
        when(drawable.getImageSize()).thenReturn(imageSize);
 | 
			
		||||
        when(drawable.getResult()).thenReturn(result);
 | 
			
		||||
        when(drawable.getLastKnownCanvasWidth()).thenReturn(111);
 | 
			
		||||
        when(drawable.getLastKnowTextSize()).thenReturn(24.0F);
 | 
			
		||||
 | 
			
		||||
        def.resolveImageSize(drawable);
 | 
			
		||||
 | 
			
		||||
        final ArgumentCaptor<ImageSize> imageSizeArgumentCaptor = ArgumentCaptor.forClass(ImageSize.class);
 | 
			
		||||
        final ArgumentCaptor<Rect> rectArgumentCaptor = ArgumentCaptor.forClass(Rect.class);
 | 
			
		||||
        final ArgumentCaptor<Integer> integerArgumentCaptor = ArgumentCaptor.forClass(Integer.class);
 | 
			
		||||
        final ArgumentCaptor<Float> floatArgumentCaptor = ArgumentCaptor.forClass(Float.class);
 | 
			
		||||
 | 
			
		||||
        verify(def).resolveImageSize(
 | 
			
		||||
                imageSizeArgumentCaptor.capture(),
 | 
			
		||||
                rectArgumentCaptor.capture(),
 | 
			
		||||
                integerArgumentCaptor.capture(),
 | 
			
		||||
                floatArgumentCaptor.capture());
 | 
			
		||||
 | 
			
		||||
        assertEquals(imageSize, imageSizeArgumentCaptor.getValue());
 | 
			
		||||
        assertEquals(rect, rectArgumentCaptor.getValue());
 | 
			
		||||
        assertEquals((Integer) 111, integerArgumentCaptor.getValue());
 | 
			
		||||
        assertEquals((Float) 24.0F, floatArgumentCaptor.getValue());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void no_image_size() {
 | 
			
		||||
        // no image size returns image original bounds
 | 
			
		||||
 | 
			
		||||
@ -6,4 +6,4 @@ existing links and keep only the ones it creates.
 | 
			
		||||
 | 
			
		||||
Please note that usage of this plugin introduces significant performance drop due to not
 | 
			
		||||
optimal implementation of underlying `android.text.util.Linkify`. If you have any ideas of how
 | 
			
		||||
to improve this - PR are welcome!
 | 
			
		||||
to improve this - PRs are welcome!
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user