Skip to content

Commit

Permalink
Added DataSourceImageSource and migrate Showcase sample to Vito
Browse files Browse the repository at this point in the history
Summary:
This adds a new `DataSourceImageSource that is specific to the Fresco ImagePipeline (due to DataSource, CloseableImage etc.), hence in the module that also has `ImagePipelineImageSource` that is similarly specific.

This allows folks to use any data source supplier as an image source, such as the `RetainingDataSourceSupplier`

Reviewed By: kartavya-ramnani

Differential Revision: D68023529

fbshipit-source-id: d60f66b96953c3102b593eeb6772283b14e03ac6
  • Loading branch information
oprisnik authored and facebook-github-bot committed Jan 14, 2025
1 parent 4035ab4 commit b7daf22
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 34 deletions.
1 change: 1 addition & 0 deletions samples/showcase/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ dependencies {
implementation project(':vito:core')
implementation project(':vito:core-common-impl')
implementation project(':vito:core-impl')
implementation project(':vito:core-java-impl')
implementation project(':vito:drawee-support')
implementation project(':vito:init')
implementation project(':vito:ktx')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ object ExampleDatabase {
"Drawee",
listOf(
ExampleItem("Simple DraweeSpan") { DraweeSpanSimpleTextFragment() },
ExampleItem("Retaining Data Source Supplier") {
RetainingDataSourceSupplierFragment()
},
)),
ExampleCategory(
"Image Pipeline",
Expand Down Expand Up @@ -115,6 +112,9 @@ object ExampleDatabase {
ExampleItem("Placeholder, Progress, Failure") { ImageLayersFragment() },
ExampleItem("Rounded Corners") { VitoRoundedCornersFragment() },
ExampleItem("Image Transition") { VitoTransitionFragment() },
ExampleItem("Retaining Data Source Supplier") {
RetainingDataSourceSupplierFragment()
},
)),
ExampleCategory(
"Vito Renderer",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,35 @@
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.fresco.samples.showcase.drawee;
package com.facebook.fresco.samples.showcase.vito;

import android.graphics.drawable.Animatable;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.Nullable;
import com.facebook.common.references.CloseableReference;
import com.facebook.datasource.RetainingDataSourceSupplier;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.drawee.controller.ControllerListener;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.fresco.samples.showcase.BaseShowcaseFragment;
import com.facebook.fresco.samples.showcase.R;
import com.facebook.fresco.vito.core.impl.source.DataSourceImageSource;
import com.facebook.fresco.vito.options.ImageOptions;
import com.facebook.fresco.vito.view.VitoView;
import com.facebook.imagepipeline.image.CloseableImage;
import com.facebook.imagepipeline.image.ImageInfo;
import com.facebook.imagepipeline.request.ImageRequest;
import java.util.List;

public class RetainingDataSourceSupplierFragment extends BaseShowcaseFragment {

private static final String CALLER_CONTEXT = "RetainingDataSourceSupplierFragment";

private final ImageOptions mImageOptions = ImageOptions.create().autoPlay(true).build();
private List<Uri> mSampleUris;
private int mUriIndex = 0;

private final ControllerListener<ImageInfo> 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);
Expand All @@ -55,21 +45,18 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
@Override
public View onCreateView(
LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_drawee_retaining_supplier, container, false);
return inflater.inflate(R.layout.fragment_vito_retaining_supplier, container, false);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
final SimpleDraweeView simpleDraweeView = view.findViewById(R.id.drawee_view);
final ImageView imageView = view.findViewById(R.id.image_view);
final RetainingDataSourceSupplier<CloseableReference<CloseableImage>> retainingSupplier =
new RetainingDataSourceSupplier<>();
simpleDraweeView.setController(
Fresco.newDraweeControllerBuilder()
.setDataSourceSupplier(retainingSupplier)
.setControllerListener(controllerListener)
.build());
VitoView.show(
new DataSourceImageSource(retainingSupplier), mImageOptions, CALLER_CONTEXT, imageView);
replaceImage(retainingSupplier);
simpleDraweeView.setOnClickListener(v -> replaceImage(retainingSupplier));
imageView.setOnClickListener(v -> replaceImage(retainingSupplier));
}

private void replaceImage(
Expand All @@ -78,7 +65,9 @@ private void replaceImage(
retainingSupplier.replaceSupplier(
Fresco.getImagePipeline()
.getDataSourceSupplier(
ImageRequest.fromUri(getNextUri()), null, ImageRequest.RequestLevel.FULL_FETCH));
ImageRequest.fromUri(getNextUri()),
CALLER_CONTEXT,
ImageRequest.RequestLevel.FULL_FETCH));
}

private synchronized Uri getNextUri() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
android:orientation="vertical"
>

<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/drawee_view"
<ImageView
android:id="@+id/image_view"
android:layout_width="@dimen/drawee_width_medium"
android:layout_height="@dimen/drawee_height_medium"
/>
Expand All @@ -19,7 +19,7 @@
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_medium"
android:gravity="center"
android:text="@string/drawee_retaining_supplier_help"
android:text="@string/image_retaining_supplier_help"
android:textAppearance="?android:attr/textAppearanceSmall"
/>
</LinearLayout>
2 changes: 1 addition & 1 deletion samples/showcase/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<string name="vito_transition_details_title">Image Detail Activity</string>
<string name="vito_transition_details_help">This is the second activity. Press back to reverse the transition.</string>

<string name="drawee_retaining_supplier_help">Click on the image to show the next one. The previous one will be retained until the new image is ready.</string>
<string name="image_retaining_supplier_help">Click on the image to show the next one. The previous one will be retained until the new image is ready.</string>

<string name="drawee_multi_uri_help">Example to load an image based on a set of URIs, either as "first available" or with "increasing quality".</string>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import com.facebook.datasource.FirstAvailableDataSourceSupplier
import com.facebook.datasource.IncreasingQualityDataSourceSupplier
import com.facebook.fresco.middleware.Extras
import com.facebook.fresco.vito.core.ImagePipelineUtils
import com.facebook.fresco.vito.core.impl.source.DataSourceImageSource
import com.facebook.fresco.vito.core.impl.source.ImagePipelineImageSource
import com.facebook.fresco.vito.options.ImageOptions
import com.facebook.fresco.vito.source.EmptyImageSource
Expand Down Expand Up @@ -141,6 +142,7 @@ object ImageSourceToImagePipelineAdapter {
requestListener,
uiComponentId,
extras)))
is DataSourceImageSource -> imageSource.dataSourceSupplier
else -> NO_REQUEST_SUPPLIER
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.fresco.vito.core.impl.source

import com.facebook.common.internal.Supplier
import com.facebook.common.references.CloseableReference
import com.facebook.datasource.DataSource
import com.facebook.fresco.vito.source.ImageSource
import com.facebook.imagepipeline.image.CloseableImage

/** ImageSource that directly supplies a DataSource */
class DataSourceImageSource(
val dataSourceSupplier: Supplier<DataSource<CloseableReference<CloseableImage>>>
) : ImageSource

0 comments on commit b7daf22

Please sign in to comment.