-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make it possible to obtain the cache path of an image. #351
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,18 +2,28 @@ | |
|
||
import android.app.Activity; | ||
|
||
import android.support.annotation.Nullable; | ||
import com.bumptech.glide.Glide; | ||
import com.bumptech.glide.load.DataSource; | ||
import com.bumptech.glide.load.engine.GlideException; | ||
import com.bumptech.glide.load.model.GlideUrl; | ||
import com.bumptech.glide.request.RequestListener; | ||
import com.bumptech.glide.request.target.Target; | ||
|
||
import com.facebook.react.bridge.Promise; | ||
import com.facebook.react.bridge.ReactApplicationContext; | ||
import com.facebook.react.bridge.ReactContextBaseJavaModule; | ||
import com.facebook.react.bridge.ReactMethod; | ||
import com.facebook.react.bridge.ReadableArray; | ||
import com.facebook.react.bridge.ReadableMap; | ||
import com.facebook.react.views.imagehelper.ImageSource; | ||
|
||
import java.io.File; | ||
|
||
class FastImageViewModule extends ReactContextBaseJavaModule { | ||
|
||
private static final String REACT_CLASS = "FastImageView"; | ||
private static final String ERROR_LOAD_FAILED = "ERROR_LOAD_FAILED"; | ||
|
||
FastImageViewModule(ReactApplicationContext reactContext) { | ||
super(reactContext); | ||
|
@@ -33,6 +43,7 @@ public void preload(final ReadableArray sources) { | |
public void run() { | ||
for (int i = 0; i < sources.size(); i++) { | ||
final ReadableMap source = sources.getMap(i); | ||
|
||
final FastImageSource imageSource = FastImageViewConverter.getImageSource(activity, source); | ||
|
||
Glide | ||
|
@@ -45,12 +56,47 @@ public void run() { | |
// - data:image/png;base64 | ||
.load( | ||
imageSource.isBase64Resource() ? imageSource.getSource() : | ||
imageSource.isResource() ? imageSource.getUri() : imageSource.getGlideUrl() | ||
imageSource.isResource() ? imageSource.getUri() : imageSource.getGlideUrl() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same thing, keep the indentation here please :3 |
||
) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aaaand this empty line ;) |
||
.apply(FastImageViewConverter.getOptions(source)) | ||
.preload(); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
@ReactMethod | ||
public void loadImage(final ReadableMap source, final Promise promise) { | ||
final Activity activity = getCurrentActivity(); | ||
if (activity == null) return; | ||
activity.runOnUiThread(new Runnable() { | ||
@Override | ||
public void run() { | ||
final FastImageSource imageSource = FastImageViewConverter.getImageSource(activity, source); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And finally removing this empty line |
||
final GlideUrl glideUrl = imageSource.getGlideUrl(); | ||
|
||
Glide | ||
.with(activity.getApplicationContext()) | ||
.asFile() | ||
.load(glideUrl) | ||
.apply(FastImageViewConverter.getOptions(source)) | ||
.listener(new RequestListener<File>() { | ||
@Override | ||
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<File> target, boolean isFirstResource) { | ||
promise.reject(ERROR_LOAD_FAILED, e); | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean onResourceReady(File resource, Object model, Target<File> target, DataSource dataSource, boolean isFirstResource) { | ||
promise.resolve(resource.getAbsolutePath()); | ||
return false; | ||
} | ||
}) | ||
.submit(); | ||
} | ||
}); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should keep the author's code style, by removing this empty line between variable declarations ;)