Skip to content

Commit

Permalink
Added new API to request a decoded image with an optional UI componen…
Browse files Browse the repository at this point in the history
…t ID

Summary:
You can now fetch an image and specify a UI component ID:

```
String uiComponentId = "abc";

imagePipeline.fetchDecodedImage(
      imageRequest,
      callerContext,
      lowestPermittedRequestLevelOnSubmit,
       requestListener,
       uiComponentId)
```

Differential Revision: D16859616

fbshipit-source-id: a19dc584556fd568791706fd5700bdb05d51dc04
  • Loading branch information
oprisnik authored and facebook-github-bot committed Aug 17, 2019
1 parent 7d4dab0 commit 017c007
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 3 deletions.
Expand Up @@ -161,6 +161,37 @@ public String toString() {
};
}

/**
* Returns a DataSource supplier that will on get submit the request for execution and return a
* DataSource representing the pending results of the task.
*
* @param imageRequest the request to submit (what to execute).
* @param callerContext the caller context of the caller of data source supplier
* @param requestLevel which level to look down until for the image
* @param requestListener additional image request listener independent of ImageRequest listeners
* @param uiComponentId optional UI component ID requesting the image
* @return a DataSource representing pending results and completion of the request
*/
public Supplier<DataSource<CloseableReference<CloseableImage>>> getDataSourceSupplier(
final ImageRequest imageRequest,
final Object callerContext,
final ImageRequest.RequestLevel requestLevel,
final @Nullable RequestListener requestListener,
final @Nullable String uiComponentId) {
return new Supplier<DataSource<CloseableReference<CloseableImage>>>() {
@Override
public DataSource<CloseableReference<CloseableImage>> get() {
return fetchDecodedImage(
imageRequest, callerContext, requestLevel, requestListener, uiComponentId);
}

@Override
public String toString() {
return Objects.toStringHelper(this).add("uri", imageRequest.getSourceUri()).toString();
}
};
}

/**
* Returns a DataSource supplier that will on get submit the request for execution and return a
* DataSource representing the pending results of the task.
Expand Down Expand Up @@ -265,6 +296,29 @@ public DataSource<CloseableReference<CloseableImage>> fetchDecodedImage(
Object callerContext,
ImageRequest.RequestLevel lowestPermittedRequestLevelOnSubmit,
@Nullable RequestListener requestListener) {
return fetchDecodedImage(
imageRequest, callerContext, lowestPermittedRequestLevelOnSubmit, requestListener, null);
}

/**
* Submits a request for execution and returns a DataSource representing the pending decoded
* image(s).
*
* <p>The returned DataSource must be closed once the client has finished with it.
*
* @param imageRequest the request to submit
* @param callerContext the caller context for image request
* @param lowestPermittedRequestLevelOnSubmit the lowest request level permitted for image reques
* @param requestListener additional image request listener independent of ImageRequest listeners
* @param uiComponentId optional UI component ID that is requesting the image
* @return a DataSource representing the pending decoded image(s)
*/
public DataSource<CloseableReference<CloseableImage>> fetchDecodedImage(
ImageRequest imageRequest,
Object callerContext,
ImageRequest.RequestLevel lowestPermittedRequestLevelOnSubmit,
@Nullable RequestListener requestListener,
@Nullable String uiComponentId) {
try {
Producer<CloseableReference<CloseableImage>> producerSequence =
mProducerSequenceFactory.getDecodedImageProducerSequence(imageRequest);
Expand All @@ -273,7 +327,8 @@ public DataSource<CloseableReference<CloseableImage>> fetchDecodedImage(
imageRequest,
lowestPermittedRequestLevelOnSubmit,
callerContext,
requestListener);
requestListener,
uiComponentId);
} catch (Exception exception) {
return DataSources.immediateFailedDataSource(exception);
}
Expand Down Expand Up @@ -326,7 +381,8 @@ public DataSource<CloseableReference<PooledByteBuffer>> fetchEncodedImage(
imageRequest,
ImageRequest.RequestLevel.FULL_FETCH,
callerContext,
requestListener);
requestListener,
null);
} catch (Exception exception) {
return DataSources.immediateFailedDataSource(exception);
}
Expand Down Expand Up @@ -732,7 +788,8 @@ private <T> DataSource<CloseableReference<T>> submitFetchRequest(
ImageRequest imageRequest,
ImageRequest.RequestLevel lowestPermittedRequestLevelOnSubmit,
Object callerContext,
@Nullable RequestListener requestListener) {
@Nullable RequestListener requestListener,
@Nullable String uiComponentId) {
if (FrescoSystrace.isTracing()) {
FrescoSystrace.beginSection("ImagePipeline#submitFetchRequest");
}
Expand All @@ -753,6 +810,7 @@ private <T> DataSource<CloseableReference<T>> submitFetchRequest(
new SettableProducerContext(
imageRequest,
generateUniqueFutureId(),
uiComponentId,
requestListener2,
callerContext,
lowestPermittedRequestLevel,
Expand Down
Expand Up @@ -20,6 +20,7 @@
public class BaseProducerContext implements ProducerContext {
private final ImageRequest mImageRequest;
private final String mId;
private final @Nullable String mUiComponentId;
private final ProducerListener2 mProducerListener;
private final Object mCallerContext;
private final ImageRequest.RequestLevel mLowestPermittedRequestLevel;
Expand All @@ -44,8 +45,31 @@ public BaseProducerContext(
boolean isPrefetch,
boolean isIntermediateResultExpected,
Priority priority) {
this(
imageRequest,
id,
null,
producerListener,
callerContext,
lowestPermittedRequestLevel,
isPrefetch,
isIntermediateResultExpected,
priority);
}

public BaseProducerContext(
ImageRequest imageRequest,
String id,
@Nullable String uiComponentId,
ProducerListener2 producerListener,
Object callerContext,
ImageRequest.RequestLevel lowestPermittedRequestLevel,
boolean isPrefetch,
boolean isIntermediateResultExpected,
Priority priority) {
mImageRequest = imageRequest;
mId = id;
mUiComponentId = uiComponentId;
mProducerListener = producerListener;
mCallerContext = callerContext;
mLowestPermittedRequestLevel = lowestPermittedRequestLevel;
Expand All @@ -68,6 +92,11 @@ public String getId() {
return mId;
}

@Nullable
public String getUiComponentId() {
return mUiComponentId;
}

@Override
public ProducerListener2 getProducerListener() {
return mProducerListener;
Expand Down
Expand Up @@ -9,6 +9,7 @@

import com.facebook.imagepipeline.common.Priority;
import com.facebook.imagepipeline.request.ImageRequest;
import javax.annotation.Nullable;

/**
* Used to pass context information to producers.
Expand All @@ -33,6 +34,12 @@ public interface ProducerContext {
*/
String getId();

/**
* @return optional id of the UI component requesting the image
*/
@Nullable
String getUiComponentId();

/**
* @return ProducerListener2 for producer's events
*/
Expand Down
Expand Up @@ -9,6 +9,7 @@

import com.facebook.imagepipeline.common.Priority;
import com.facebook.imagepipeline.request.ImageRequest;
import javax.annotation.Nullable;
import javax.annotation.concurrent.ThreadSafe;

/**
Expand All @@ -21,6 +22,7 @@ public SettableProducerContext(ProducerContext context) {
this(
context.getImageRequest(),
context.getId(),
context.getUiComponentId(),
context.getProducerListener(),
context.getCallerContext(),
context.getLowestPermittedRequestLevel(),
Expand All @@ -33,6 +35,7 @@ public SettableProducerContext(ImageRequest overrideRequest, ProducerContext con
this(
overrideRequest,
context.getId(),
context.getUiComponentId(),
context.getProducerListener(),
context.getCallerContext(),
context.getLowestPermittedRequestLevel(),
Expand Down Expand Up @@ -61,6 +64,28 @@ public SettableProducerContext(
priority);
}

public SettableProducerContext(
ImageRequest imageRequest,
String id,
@Nullable String uiComponentId,
ProducerListener2 producerListener,
Object callerContext,
ImageRequest.RequestLevel lowestPermittedRequestLevel,
boolean isPrefetch,
boolean isIntermediateResultExpected,
Priority priority) {
super(
imageRequest,
id,
uiComponentId,
producerListener,
callerContext,
lowestPermittedRequestLevel,
isPrefetch,
isIntermediateResultExpected,
priority);
}

/**
* Set whether the request is a prefetch request or not.
* @param isPrefetch
Expand Down

0 comments on commit 017c007

Please sign in to comment.