Skip to content
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

Proper support for GIFs when using a RetainingDataSource #2185

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -443,7 +443,7 @@ protected void submitRequest() {
mEventTracker.recordEvent(Event.ON_SUBMIT_CACHE_HIT);
getControllerListener().onSubmit(mId, mCallerContext);
onImageLoadedFromCacheImmediately(mId, closeableImage);
onNewResultInternal(mId, mDataSource, closeableImage, 1.0f, true, true);
onNewResultInternal(mId, mDataSource, closeableImage, 1.0f, true, true, true);
FrescoSystrace.endSection();
return;
}
Expand All @@ -470,10 +470,11 @@ public void onNewResultImpl(DataSource<T> dataSource) {
// isFinished must be obtained before image, otherwise we might set intermediate result
// as final image.
boolean isFinished = dataSource.isFinished();
boolean hasMultipleResults = dataSource.hasMultipleResults();
float progress = dataSource.getProgress();
T image = dataSource.getResult();
if (image != null) {
onNewResultInternal(id, dataSource, image, progress, isFinished, wasImmediate);
onNewResultInternal(id, dataSource, image, progress, isFinished, wasImmediate, hasMultipleResults);
} else if (isFinished) {
onFailureInternal(id, dataSource, new NullPointerException(), /* isFinished */ true);
}
Expand All @@ -499,7 +500,8 @@ private void onNewResultInternal(
@Nullable T image,
float progress,
boolean isFinished,
boolean wasImmediate) {
boolean wasImmediate,
boolean deliverTempResult) {
try {
FrescoSystrace.beginSection("AbstractDraweeController#onNewResultInternal");
// ignore late callbacks (data source that returned the new result is not the one we expected)
Expand Down Expand Up @@ -532,6 +534,10 @@ private void onNewResultInternal(
mDataSource = null;
mSettableDraweeHierarchy.setImage(drawable, 1f, wasImmediate);
getControllerListener().onFinalImageSet(id, getImageInfo(image), getAnimatable());
} else if (deliverTempResult) {
logMessageAndImage("set_temporary_result @ onNewResult", image);
mSettableDraweeHierarchy.setImage(drawable, 1f, wasImmediate);
getControllerListener().onFinalImageSet(id, getImageInfo(image), getAnimatable());
// IMPORTANT: do not execute any instance-specific code after this point
Crysis21 marked this conversation as resolved.
Show resolved Hide resolved
} else {
logMessageAndImage("set_intermediate_result @ onNewResult", image);
Expand Down
Expand Up @@ -319,4 +319,9 @@ public void run() {
});
}
}

@Override
public boolean hasMultipleResults() {
return false;
}
}
7 changes: 7 additions & 0 deletions fbcore/src/main/java/com/facebook/datasource/DataSource.java
Expand Up @@ -49,6 +49,13 @@ public interface DataSource<T> {
*/
boolean hasResult();

/**
*
* @return true if any resources poured in the datasource should be sent to controllers. Usefull
* with a RetainingDataSource to loaded resources are displayed correctly.
*/
boolean hasMultipleResults();

/**
* @return true if request is finished, false otherwise
*/
Expand Down
Expand Up @@ -139,5 +139,10 @@ public void onProgressUpdate(DataSource<T> dataSource) {
RetainingDataSource.this.onDatasourceProgress(dataSource);
}
}

@Override
public boolean hasMultipleResults() {
return true;
}
}
}
Expand Up @@ -26,19 +26,35 @@
import com.facebook.fresco.samples.showcase.misc.ImageUriProvider;
import com.facebook.imagepipeline.image.CloseableImage;
import com.facebook.imagepipeline.request.ImageRequest;
import android.graphics.drawable.Animatable;
import com.facebook.imagepipeline.image.ImageInfo;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.drawee.controller.ControllerListener;
import java.util.List;

public class RetainingDataSourceSupplierFragment extends BaseShowcaseFragment {

private List<Uri> mSampleUris;
private int mUriIndex = 0;

private ControllerListener controllerListener = new BaseControllerListener<ImageInfo>() {
@Override
public void onFinalImageSet(
String id,
@Nullable ImageInfo imageInfo,
@Nullable Animatable anim) {
if (anim != null) {
// app-specific logic to enable animation starting
anim.start();
}
}
};

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mSampleUris =
ImageUriProvider.getInstance(getContext()).getSampleUris(ImageUriProvider.ImageSize.M);
mSampleUris = ImageUriProvider.getInstance(getContext()).getSampleGifUris();
}

@Nullable
Expand All @@ -54,7 +70,10 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
final RetainingDataSourceSupplier<CloseableReference<CloseableImage>> retainingSupplier =
new RetainingDataSourceSupplier<>();
simpleDraweeView.setController(
Fresco.newDraweeControllerBuilder().setDataSourceSupplier(retainingSupplier).build());
Fresco.newDraweeControllerBuilder()
.setDataSourceSupplier(retainingSupplier)
.setControllerListener(controllerListener)
.build());
replaceImage(retainingSupplier);
simpleDraweeView.setOnClickListener(
new View.OnClickListener() {
Expand Down
Expand Up @@ -146,6 +146,13 @@ public enum ImageSize {
private static final String SAMPLE_URI_WEBP_ANIMATED =
"https://www.gstatic.com/webp/animated/1.webp";

private static final String[] SAMPLE_URIS_GIFS =
new String [] {
"https://media2.giphy.com/media/3oge84qhopFbFFkwec/giphy.gif",
"https://media3.giphy.com/media/uegrGBitPHtKM/giphy.gif",
"https://media0.giphy.com/media/SWd9mTHEMIxQ4/giphy.gif"
};

private static ImageUriProvider sInstance;

private final SharedPreferences mSharedPreferences;
Expand Down Expand Up @@ -279,6 +286,14 @@ public List<Uri> getRandomSampleUris(final ImageSize imageSize, final int numIma
return data;
}

public List<Uri> getSampleGifUris(){
ArrayList<Uri> uris = new ArrayList<>();
for (String uri : SAMPLE_URIS_GIFS) {
uris.add(Uri.parse(uri));
}
return uris;
}

private Uri applyOverrideSettings(
String uriString,
UriModification urlModification) {
Expand Down