Moved LinkResolver to independent entity

This commit is contained in:
Dimitry Ivanov 2019-06-22 15:20:46 +03:00
parent 213f5cf281
commit ffb5848c3c
8 changed files with 75 additions and 20 deletions

View File

@ -10,6 +10,7 @@
* removed MarkwonPlugin#configureHtmlRenderer -> now part of HtmlPlugin * removed MarkwonPlugin#configureHtmlRenderer -> now part of HtmlPlugin
* TagHandler now has `supportedTags()` method * TagHandler now has `supportedTags()` method
* html is moved completely to html-plugin * html is moved completely to html-plugin
* OnTextAddedListener * OnTextAddedListener (CorePlugin)
* ImageSizeResolver signature change (accept AsyncDrawable) * 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)

View File

@ -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);
}

View File

@ -10,11 +10,9 @@ import android.view.View;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import io.noties.markwon.core.spans.LinkSpan; public class LinkResolverDef implements LinkResolver {
public class LinkResolverDef implements LinkSpan.Resolver {
@Override @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 Uri uri = Uri.parse(link);
final Context context = view.getContext(); final Context context = view.getContext();
final Intent intent = new Intent(Intent.ACTION_VIEW, uri); final Intent intent = new Intent(Intent.ACTION_VIEW, uri);

View File

@ -3,7 +3,6 @@ package io.noties.markwon;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import io.noties.markwon.core.MarkwonTheme; import io.noties.markwon.core.MarkwonTheme;
import io.noties.markwon.core.spans.LinkSpan;
import io.noties.markwon.image.AsyncDrawableLoader; import io.noties.markwon.image.AsyncDrawableLoader;
import io.noties.markwon.image.ImageSizeResolver; import io.noties.markwon.image.ImageSizeResolver;
import io.noties.markwon.image.ImageSizeResolverDef; import io.noties.markwon.image.ImageSizeResolverDef;
@ -26,7 +25,7 @@ public class MarkwonConfiguration {
private final MarkwonTheme theme; private final MarkwonTheme theme;
private final AsyncDrawableLoader asyncDrawableLoader; private final AsyncDrawableLoader asyncDrawableLoader;
private final SyntaxHighlight syntaxHighlight; private final SyntaxHighlight syntaxHighlight;
private final LinkSpan.Resolver linkResolver; private final LinkResolver linkResolver;
private final UrlProcessor urlProcessor; private final UrlProcessor urlProcessor;
private final ImageSizeResolver imageSizeResolver; private final ImageSizeResolver imageSizeResolver;
@ -59,7 +58,7 @@ public class MarkwonConfiguration {
} }
@NonNull @NonNull
public LinkSpan.Resolver linkResolver() { public LinkResolver linkResolver() {
return linkResolver; return linkResolver;
} }
@ -87,7 +86,7 @@ public class MarkwonConfiguration {
private MarkwonTheme theme; private MarkwonTheme theme;
private AsyncDrawableLoader asyncDrawableLoader; private AsyncDrawableLoader asyncDrawableLoader;
private SyntaxHighlight syntaxHighlight; private SyntaxHighlight syntaxHighlight;
private LinkSpan.Resolver linkResolver; private LinkResolver linkResolver;
private UrlProcessor urlProcessor; private UrlProcessor urlProcessor;
private ImageSizeResolver imageSizeResolver; private ImageSizeResolver imageSizeResolver;
private MarkwonSpansFactory spansFactory; private MarkwonSpansFactory spansFactory;
@ -111,7 +110,7 @@ public class MarkwonConfiguration {
} }
@NonNull @NonNull
public Builder linkResolver(@NonNull LinkSpan.Resolver linkResolver) { public Builder linkResolver(@NonNull LinkResolver linkResolver) {
this.linkResolver = linkResolver; this.linkResolver = linkResolver;
return this; return this;
} }

View File

@ -6,19 +6,19 @@ import android.view.View;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import io.noties.markwon.LinkResolver;
import io.noties.markwon.core.MarkwonTheme; import io.noties.markwon.core.MarkwonTheme;
public class LinkSpan extends URLSpan { public class LinkSpan extends URLSpan {
public interface Resolver {
void resolve(View view, @NonNull String link);
}
private final MarkwonTheme theme; private final MarkwonTheme theme;
private final String link; 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); super(link);
this.theme = theme; this.theme = theme;
this.link = link; this.link = link;

View File

@ -5,9 +5,10 @@ import android.graphics.Rect;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
/** /**
* @see ImageSizeResolverDef
* @see io.noties.markwon.MarkwonConfiguration.Builder#imageSizeResolver(ImageSizeResolver)
* @since 1.0.1 * @since 1.0.1
*/ */
@SuppressWarnings({"WeakerAccess", "unused"})
public abstract class ImageSizeResolver { public abstract class ImageSizeResolver {
/** /**

View File

@ -1,16 +1,22 @@
package io.noties.markwon.image; package io.noties.markwon.image;
import android.graphics.Rect; import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.robolectric.RobolectricTestRunner; import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config; 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_EM;
import static io.noties.markwon.image.ImageSizeResolverDef.UNIT_PERCENT; 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) @RunWith(RobolectricTestRunner.class)
@Config(manifest = Config.NONE) @Config(manifest = Config.NONE)
@ -23,6 +29,42 @@ public class ImageSizeResolverDefTest {
def = new ImageSizeResolverDef(); 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 @Test
public void no_image_size() { public void no_image_size() {
// no image size returns image original bounds // no image size returns image original bounds

View File

@ -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 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 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!