mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-03-29 22:41:56 +08:00
Add a way to prefetch remote images to cache with Image.prefetch
Summary:Adds `Image.prefetch` to prefetch remote images before they are used in an actual `Image` component. This is based off of #4420 by sospartan and skevy's work. Closes https://github.com/facebook/react-native/pull/6774 Differential Revision: D3153729 Pulled By: bestander fb-gh-sync-id: ef61412e051a49b42ae885edce7905a8ca0da23f fbshipit-source-id: ef61412e051a49b42ae885edce7905a8ca0da23f
This commit is contained in:
committed by
Facebook Github Bot 1
parent
4450d789e0
commit
f7bcb3e98d
@@ -0,0 +1,23 @@
|
||||
include_defs('//ReactAndroid/DEFS')
|
||||
|
||||
android_library(
|
||||
name = 'image',
|
||||
srcs = glob(['*.java']),
|
||||
deps = [
|
||||
react_native_dep('libraries/fresco/fresco-react-native:fbcore'),
|
||||
react_native_dep('libraries/fresco/fresco-react-native:fresco-drawee'),
|
||||
react_native_dep('libraries/fresco/fresco-react-native:fresco-react-native'),
|
||||
react_native_dep('libraries/fresco/fresco-react-native:imagepipeline'),
|
||||
react_native_dep('third-party/java/infer-annotations:infer-annotations'),
|
||||
react_native_dep('third-party/java/jsr-305:jsr-305'),
|
||||
react_native_target('java/com/facebook/react/bridge:bridge'),
|
||||
react_native_target('java/com/facebook/react/common:common'),
|
||||
],
|
||||
visibility = [
|
||||
'PUBLIC',
|
||||
],
|
||||
)
|
||||
|
||||
project_config(
|
||||
src_target = ':image',
|
||||
)
|
||||
@@ -0,0 +1,82 @@
|
||||
/**
|
||||
* Copyright (c) 2015-present, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
package com.facebook.react.modules.image;
|
||||
|
||||
import android.net.Uri;
|
||||
|
||||
import com.facebook.common.executors.CallerThreadExecutor;
|
||||
import com.facebook.datasource.BaseDataSubscriber;
|
||||
import com.facebook.datasource.DataSource;
|
||||
import com.facebook.datasource.DataSubscriber;
|
||||
import com.facebook.drawee.backends.pipeline.Fresco;
|
||||
import com.facebook.imagepipeline.request.ImageRequest;
|
||||
import com.facebook.imagepipeline.request.ImageRequestBuilder;
|
||||
import com.facebook.react.bridge.Promise;
|
||||
import com.facebook.react.bridge.ReactApplicationContext;
|
||||
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
||||
import com.facebook.react.bridge.ReactMethod;
|
||||
|
||||
public class ImageLoaderModule extends ReactContextBaseJavaModule {
|
||||
|
||||
private static final String ERROR_INVALID_URI = "E_INVALID_URI";
|
||||
private static final String ERROR_PREFETCH_FAILURE = "E_PREFETCH_FAILURE";
|
||||
|
||||
public ImageLoaderModule(ReactApplicationContext reactContext) {
|
||||
super(reactContext);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "ImageLoader";
|
||||
}
|
||||
|
||||
/**
|
||||
* Prefetches the given image to the Fresco image disk cache.
|
||||
*
|
||||
* @param uriString the URI of the remote image to prefetch
|
||||
* @param promise the promise that is fulfilled when the image is successfully prefetched
|
||||
* or rejected when there is an error
|
||||
*/
|
||||
@ReactMethod
|
||||
public void prefetchImage(String uriString, final Promise promise) {
|
||||
if (uriString == null || uriString.isEmpty()) {
|
||||
promise.reject(ERROR_INVALID_URI, "Cannot prefetch an image for an empty URI");
|
||||
return;
|
||||
}
|
||||
|
||||
Uri uri = Uri.parse(uriString);
|
||||
ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri).build();
|
||||
|
||||
DataSource<Void> prefetchSource = Fresco.getImagePipeline().prefetchToDiskCache(request, this);
|
||||
DataSubscriber<Void> prefetchSubscriber = new BaseDataSubscriber<Void>() {
|
||||
@Override
|
||||
protected void onNewResultImpl(DataSource<Void> dataSource) {
|
||||
if (!dataSource.isFinished()) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
promise.resolve(true);
|
||||
} finally {
|
||||
dataSource.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onFailureImpl(DataSource<Void> dataSource) {
|
||||
try {
|
||||
promise.reject(ERROR_PREFETCH_FAILURE, dataSource.getFailureCause());
|
||||
} finally {
|
||||
dataSource.close();
|
||||
}
|
||||
}
|
||||
};
|
||||
prefetchSource.subscribe(prefetchSubscriber, CallerThreadExecutor.getInstance());
|
||||
}
|
||||
}
|
||||
@@ -4,11 +4,33 @@ android_library(
|
||||
name = 'shell',
|
||||
srcs = glob(['**/*.java']),
|
||||
deps = [
|
||||
react_native_target('res:shell'),
|
||||
react_native_target('java/com/facebook/react:react'),
|
||||
react_native_dep('libraries/soloader/java/com/facebook/soloader:soloader'),
|
||||
react_native_dep('third-party/android/support/v4:lib-support-v4'),
|
||||
react_native_dep('third-party/java/infer-annotations:infer-annotations'),
|
||||
react_native_dep('third-party/java/jsr-305:jsr-305'),
|
||||
react_native_target('java/com/facebook/react/bridge:bridge'),
|
||||
react_native_target('java/com/facebook/react/common:common'),
|
||||
react_native_target('java/com/facebook/react/devsupport:devsupport'),
|
||||
react_native_target('java/com/facebook/react/modules/appstate:appstate'),
|
||||
react_native_target('java/com/facebook/react/modules/camera:camera'),
|
||||
react_native_target('java/com/facebook/react/modules/clipboard:clipboard'),
|
||||
react_native_target('java/com/facebook/react/modules/core:core'),
|
||||
react_native_target('java/com/facebook/react/modules/datepicker:datepicker'),
|
||||
react_native_target('java/com/facebook/react/modules/debug:debug'),
|
||||
react_native_target('java/com/facebook/react/modules/dialog:dialog'),
|
||||
react_native_target('java/com/facebook/react/modules/fresco:fresco'),
|
||||
react_native_target('java/com/facebook/react/modules/image:image'),
|
||||
react_native_target('java/com/facebook/react/modules/intent:intent'),
|
||||
react_native_target('java/com/facebook/react/modules/location:location'),
|
||||
react_native_target('java/com/facebook/react/modules/netinfo:netinfo'),
|
||||
react_native_target('java/com/facebook/react/modules/network:network'),
|
||||
react_native_target('java/com/facebook/react/modules/statusbar:statusbar'),
|
||||
react_native_target('java/com/facebook/react/modules/storage:storage'),
|
||||
react_native_target('java/com/facebook/react/modules/timepicker:timepicker'),
|
||||
react_native_target('java/com/facebook/react/modules/toast:toast'),
|
||||
react_native_target('java/com/facebook/react/modules/vibration:vibration'),
|
||||
react_native_target('java/com/facebook/react/modules/websocket:websocket'),
|
||||
react_native_target('java/com/facebook/react/uimanager:uimanager'),
|
||||
react_native_target('java/com/facebook/react/views/art:art'),
|
||||
react_native_target('java/com/facebook/react/views/drawer:drawer'),
|
||||
react_native_target('java/com/facebook/react/views/image:image'),
|
||||
@@ -20,36 +42,15 @@ android_library(
|
||||
react_native_target('java/com/facebook/react/views/slider:slider'),
|
||||
react_native_target('java/com/facebook/react/views/swiperefresh:swiperefresh'),
|
||||
react_native_target('java/com/facebook/react/views/switchview:switchview'),
|
||||
react_native_target('java/com/facebook/react/views/text:text'),
|
||||
react_native_target('java/com/facebook/react/views/text/frescosupport:frescosupport'),
|
||||
react_native_target('java/com/facebook/react/views/text:text'),
|
||||
react_native_target('java/com/facebook/react/views/textinput:textinput'),
|
||||
react_native_target('java/com/facebook/react/views/toolbar:toolbar'),
|
||||
react_native_target('java/com/facebook/react/views/view:view'),
|
||||
react_native_target('java/com/facebook/react/views/viewpager:viewpager'),
|
||||
react_native_target('java/com/facebook/react/views/webview:webview'),
|
||||
react_native_target('java/com/facebook/react/modules/appstate:appstate'),
|
||||
react_native_target('java/com/facebook/react/modules/vibration:vibration'),
|
||||
react_native_target('java/com/facebook/react/modules/camera:camera'),
|
||||
react_native_target('java/com/facebook/react/modules/clipboard:clipboard'),
|
||||
react_native_target('java/com/facebook/react/modules/core:core'),
|
||||
react_native_target('java/com/facebook/react/modules/datepicker:datepicker'),
|
||||
react_native_target('java/com/facebook/react/modules/debug:debug'),
|
||||
react_native_target('java/com/facebook/react/modules/dialog:dialog'),
|
||||
react_native_target('java/com/facebook/react/modules/fresco:fresco'),
|
||||
react_native_target('java/com/facebook/react/modules/intent:intent'),
|
||||
react_native_target('java/com/facebook/react/modules/location:location'),
|
||||
react_native_target('java/com/facebook/react/modules/netinfo:netinfo'),
|
||||
react_native_target('java/com/facebook/react/modules/network:network'),
|
||||
react_native_target('java/com/facebook/react/modules/statusbar:statusbar'),
|
||||
react_native_target('java/com/facebook/react/modules/storage:storage'),
|
||||
react_native_target('java/com/facebook/react/modules/timepicker:timepicker'),
|
||||
react_native_target('java/com/facebook/react/modules/toast:toast'),
|
||||
react_native_target('java/com/facebook/react/uimanager:uimanager'),
|
||||
react_native_target('java/com/facebook/react/modules/websocket:websocket'),
|
||||
react_native_dep('libraries/soloader/java/com/facebook/soloader:soloader'),
|
||||
react_native_dep('third-party/android/support/v4:lib-support-v4'),
|
||||
react_native_dep('third-party/java/infer-annotations:infer-annotations'),
|
||||
react_native_dep('third-party/java/jsr-305:jsr-305'),
|
||||
react_native_target('java/com/facebook/react:react'),
|
||||
react_native_target('res:shell'),
|
||||
],
|
||||
visibility = [
|
||||
'PUBLIC',
|
||||
|
||||
@@ -25,6 +25,7 @@ import com.facebook.react.modules.clipboard.ClipboardModule;
|
||||
import com.facebook.react.modules.datepicker.DatePickerDialogModule;
|
||||
import com.facebook.react.modules.dialog.DialogModule;
|
||||
import com.facebook.react.modules.fresco.FrescoModule;
|
||||
import com.facebook.react.modules.image.ImageLoaderModule;
|
||||
import com.facebook.react.modules.intent.IntentModule;
|
||||
import com.facebook.react.modules.location.LocationModule;
|
||||
import com.facebook.react.modules.netinfo.NetInfoModule;
|
||||
@@ -76,6 +77,7 @@ public class MainReactPackage implements ReactPackage {
|
||||
new DialogModule(reactContext),
|
||||
new FrescoModule(reactContext),
|
||||
new ImageEditingManager(reactContext),
|
||||
new ImageLoaderModule(reactContext),
|
||||
new ImageStoreManager(reactContext),
|
||||
new IntentModule(reactContext),
|
||||
new LocationModule(reactContext),
|
||||
|
||||
Reference in New Issue
Block a user