Add image loader module based on Glide

This commit is contained in:
Dimitry Ivanov 2019-05-30 00:08:47 +03:00
parent 19091b5675
commit 0b0d3c4753
14 changed files with 234 additions and 9 deletions

View File

@ -4,7 +4,7 @@ buildscript {
jcenter() jcenter()
} }
dependencies { dependencies {
classpath 'com.android.tools.build:gradle:3.3.2' classpath 'com.android.tools.build:gradle:3.4.1'
classpath 'com.github.ben-manes:gradle-versions-plugin:0.20.0' classpath 'com.github.ben-manes:gradle-versions-plugin:0.20.0'
} }
} }
@ -29,8 +29,8 @@ task clean(type: Delete) {
delete rootProject.buildDir delete rootProject.buildDir
} }
task wrapper(type: Wrapper) { wrapper {
gradleVersion '4.10.2' gradleVersion '5.1.1'
distributionType 'all' distributionType 'all'
} }
@ -72,7 +72,8 @@ ext {
'debug' : 'ru.noties:debug:3.0.0@jar', 'debug' : 'ru.noties:debug:3.0.0@jar',
'adapt' : 'ru.noties:adapt:1.1.0', 'adapt' : 'ru.noties:adapt:1.1.0',
'dagger' : "com.google.dagger:dagger:$daggerVersion", 'dagger' : "com.google.dagger:dagger:$daggerVersion",
'picasso' : "com.squareup.picasso:picasso:2.71828" 'picasso' : 'com.squareup.picasso:picasso:2.71828',
'glide' : 'com.github.bumptech.glide:glide:4.9.0'
] ]
deps['annotationProcessor'] = [ deps['annotationProcessor'] = [

Binary file not shown.

View File

@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists

2
gradlew vendored
View File

@ -28,7 +28,7 @@ APP_NAME="Gradle"
APP_BASE_NAME=`basename "$0"` APP_BASE_NAME=`basename "$0"`
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS="" DEFAULT_JVM_OPTS='"-Xmx64m"'
# Use the maximum available, or set MAX_FD != -1 to use that value. # Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum" MAX_FD="maximum"

2
gradlew.bat vendored
View File

@ -14,7 +14,7 @@ set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME% set APP_HOME=%DIRNAME%
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS= set DEFAULT_JVM_OPTS="-Xmx64m"
@rem Find java.exe @rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome if defined JAVA_HOME goto findJavaFromJavaHome

View File

@ -0,0 +1,21 @@
apply plugin: 'com.android.library'
android {
compileSdkVersion config['compile-sdk']
buildToolsVersion config['build-tools']
defaultConfig {
minSdkVersion config['min-sdk']
targetSdkVersion config['target-sdk']
versionCode 1
versionName version
}
}
dependencies {
api project(':markwon-core')
api deps['glide']
}
registerArtifact(this)

View File

@ -0,0 +1,4 @@
POM_NAME=Image Glide
POM_ARTIFACT_ID=image-glide
POM_DESCRIPTION=Markwon image loading module (based on Glide library)
POM_PACKAGING=aar

View File

@ -0,0 +1 @@
<manifest package="ru.noties.markwon.image.glide" />

View File

@ -0,0 +1,189 @@
package ru.noties.markwon.image.glide;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Spanned;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;
import org.commonmark.node.Image;
import java.util.HashMap;
import java.util.Map;
import ru.noties.markwon.AbstractMarkwonPlugin;
import ru.noties.markwon.MarkwonConfiguration;
import ru.noties.markwon.MarkwonSpansFactory;
import ru.noties.markwon.image.AsyncDrawable;
import ru.noties.markwon.image.AsyncDrawableLoader;
import ru.noties.markwon.image.AsyncDrawableScheduler;
import ru.noties.markwon.image.DrawableUtils;
import ru.noties.markwon.image.ImageSpanFactory;
/**
* @since 4.0.0-SNAPSHOT
*/
public class GlideImagesPlugin extends AbstractMarkwonPlugin {
public interface GlideStore {
@NonNull
RequestBuilder<Drawable> load(@NonNull AsyncDrawable drawable);
void cancel(@NonNull Target<?> target);
}
@NonNull
public static GlideImagesPlugin create(@NonNull final Context context) {
return create(new GlideStore() {
@NonNull
@Override
public RequestBuilder<Drawable> load(@NonNull AsyncDrawable drawable) {
return Glide.with(context)
.load(drawable.getDestination());
}
@Override
public void cancel(@NonNull Target<?> target) {
Glide.with(context)
.clear(target);
}
});
}
@NonNull
public static GlideImagesPlugin create(@NonNull final RequestManager requestManager) {
return create(new GlideStore() {
@NonNull
@Override
public RequestBuilder<Drawable> load(@NonNull AsyncDrawable drawable) {
return requestManager.load(drawable.getDestination());
}
@Override
public void cancel(@NonNull Target<?> target) {
requestManager.clear(target);
}
});
}
@NonNull
public static GlideImagesPlugin create(@NonNull GlideStore glideStore) {
return new GlideImagesPlugin(glideStore);
}
private final GlideAsyncDrawableLoader glideAsyncDrawableLoader;
@SuppressWarnings("WeakerAccess")
GlideImagesPlugin(@NonNull GlideStore glideStore) {
this.glideAsyncDrawableLoader = new GlideAsyncDrawableLoader(glideStore);
}
@Override
public void configureSpansFactory(@NonNull MarkwonSpansFactory.Builder builder) {
builder.setFactory(Image.class, new ImageSpanFactory());
}
@Override
public void configureConfiguration(@NonNull MarkwonConfiguration.Builder builder) {
builder.asyncDrawableLoader(glideAsyncDrawableLoader);
}
@Override
public void beforeSetText(@NonNull TextView textView, @NonNull Spanned markdown) {
AsyncDrawableScheduler.unschedule(textView);
}
@Override
public void afterSetText(@NonNull TextView textView) {
AsyncDrawableScheduler.schedule(textView);
}
private static class GlideAsyncDrawableLoader extends AsyncDrawableLoader {
private final GlideStore glideStore;
private final Map<AsyncDrawable, Target<?>> cache = new HashMap<>(2);
GlideAsyncDrawableLoader(@NonNull GlideStore glideStore) {
this.glideStore = glideStore;
}
@Override
public void load(@NonNull AsyncDrawable drawable) {
final Target<Drawable> target = new AsyncDrawableTarget(drawable);
cache.put(drawable, target);
glideStore.load(drawable)
.into(target);
}
@Override
public void cancel(@NonNull AsyncDrawable drawable) {
final Target<?> target = cache.remove(drawable);
if (target != null) {
glideStore.cancel(target);
}
}
@Nullable
@Override
public Drawable placeholder(@NonNull AsyncDrawable drawable) {
return null;
}
private class AsyncDrawableTarget extends CustomTarget<Drawable> {
private final AsyncDrawable drawable;
AsyncDrawableTarget(@NonNull AsyncDrawable drawable) {
this.drawable = drawable;
}
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
if (cache.remove(drawable) != null) {
if (drawable.isAttached()) {
DrawableUtils.applyIntrinsicBoundsIfEmpty(resource);
drawable.setResult(resource);
}
}
}
@Override
public void onLoadStarted(@Nullable Drawable placeholder) {
if (placeholder != null
&& drawable.isAttached()) {
DrawableUtils.applyIntrinsicBoundsIfEmpty(placeholder);
drawable.setResult(placeholder);
}
}
@Override
public void onLoadFailed(@Nullable Drawable errorDrawable) {
if (cache.remove(drawable) != null) {
if (errorDrawable != null
&& drawable.isAttached()) {
DrawableUtils.applyIntrinsicBoundsIfEmpty(errorDrawable);
drawable.setResult(errorDrawable);
}
}
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
// we won't be checking if target is still present as cancellation
// must remove target anyway
if (drawable.isAttached()) {
drawable.clearResult();
}
}
}
}
}

View File

@ -1,4 +1,4 @@
POM_NAME=Image POM_NAME=Image Picasso
POM_ARTIFACT_ID=image-picasso POM_ARTIFACT_ID=image-picasso
POM_DESCRIPTION=Markwon image loading module (based on Picasso library) POM_DESCRIPTION=Markwon image loading module (based on Picasso library)
POM_PACKAGING=aar POM_PACKAGING=aar

View File

@ -70,6 +70,7 @@ public class PicassoImagesPlugin extends AbstractMarkwonPlugin {
private final PicassoAsyncDrawableLoader picassoAsyncDrawableLoader; private final PicassoAsyncDrawableLoader picassoAsyncDrawableLoader;
@SuppressWarnings("WeakerAccess")
PicassoImagesPlugin(@NonNull PicassoStore picassoStore) { PicassoImagesPlugin(@NonNull PicassoStore picassoStore) {
this.picassoAsyncDrawableLoader = new PicassoAsyncDrawableLoader(picassoStore); this.picassoAsyncDrawableLoader = new PicassoAsyncDrawableLoader(picassoStore);
} }

View File

@ -44,6 +44,11 @@ dependencies {
implementation project(':markwon-recycler') implementation project(':markwon-recycler')
implementation project(':markwon-recycler-table') implementation project(':markwon-recycler-table')
// these are for glide
implementation 'com.android.support:animated-vector-drawable:28.0.0'
implementation 'com.android.support:support-fragment:28.0.0'
implementation project(':markwon-image-glide')
implementation project(':markwon-image-picasso') implementation project(':markwon-image-picasso')
deps.with { deps.with {

View File

@ -32,6 +32,7 @@ import ru.noties.markwon.MarkwonVisitor;
import ru.noties.markwon.core.CorePlugin; import ru.noties.markwon.core.CorePlugin;
import ru.noties.markwon.html.HtmlPlugin; import ru.noties.markwon.html.HtmlPlugin;
import ru.noties.markwon.image.AsyncDrawable; import ru.noties.markwon.image.AsyncDrawable;
import ru.noties.markwon.image.glide.GlideImagesPlugin;
import ru.noties.markwon.image.picasso.PicassoImagesPlugin; import ru.noties.markwon.image.picasso.PicassoImagesPlugin;
import ru.noties.markwon.recycler.MarkwonAdapter; import ru.noties.markwon.recycler.MarkwonAdapter;
import ru.noties.markwon.recycler.SimpleEntry; import ru.noties.markwon.recycler.SimpleEntry;
@ -85,7 +86,8 @@ public class RecyclerActivity extends Activity {
// .addSchemeHandler(OkHttpNetworkSchemeHandler.create()) // .addSchemeHandler(OkHttpNetworkSchemeHandler.create())
// .addMediaDecoder(SvgMediaDecoder.create()); // .addMediaDecoder(SvgMediaDecoder.create());
// })) // }))
.usePlugin(PicassoImagesPlugin.create(context)) // .usePlugin(PicassoImagesPlugin.create(context))
.usePlugin(GlideImagesPlugin.create(context))
// important to use TableEntryPlugin instead of TablePlugin // important to use TableEntryPlugin instead of TablePlugin
.usePlugin(TableEntryPlugin.create(context)) .usePlugin(TableEntryPlugin.create(context))
.usePlugin(HtmlPlugin.create()) .usePlugin(HtmlPlugin.create())

View File

@ -7,6 +7,7 @@ include ':app', ':sample',
':markwon-ext-tasklist', ':markwon-ext-tasklist',
':markwon-html', ':markwon-html',
':markwon-image', ':markwon-image',
':markwon-image-glide',
':markwon-image-picasso', ':markwon-image-picasso',
':markwon-recycler', ':markwon-recycler',
':markwon-recycler-table', ':markwon-recycler-table',