Skip to content

Commit

Permalink
Merge pull request #114 from skydoves/glide/requestlistener
Browse files Browse the repository at this point in the history
Add requestListener parameter for GlideImage
  • Loading branch information
skydoves authored Mar 4, 2022
2 parents 4065c11 + 4f0aa63 commit 36c5a21
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,37 @@ GlideImage(
)
```

### Custom RequestListener
You can register your own [RequestListener](https://bumptech.github.io/glide/javadocs/440/com/bumptech/glide/request/RequestListener.html), which allows you to trace the status of a request while images load.

```kotlin
GlideImage(
imageModel = poster.poster,
requestListener = object: RequestListener<Drawable> {
override fun onLoadFailed(
e: GlideException?,
model: Any?,
target: Target<Drawable>?,
isFirstResource: Boolean
): Boolean {
// do something
return false
}

override fun onResourceReady(
resource: Drawable?,
model: Any?,
target: Target<Drawable>?,
dataSource: DataSource?,
isFirstResource: Boolean
): Boolean {
// do something
return true
}
}
)
```

### LocalGlideRequestOptions
You can pass the same instance of your `RequestOptions` down through the Composition in your composable hierarchy as following the example below:

Expand Down
8 changes: 4 additions & 4 deletions glide/api/glide.api
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
public final class com/skydoves/landscapist/glide/GlideImage {
public static final fun GlideImage (Ljava/lang/Object;Landroidx/compose/ui/Modifier;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Landroidx/compose/ui/Alignment;Landroidx/compose/ui/layout/ContentScale;Ljava/lang/String;FLandroidx/compose/ui/graphics/ColorFilter;Lcom/skydoves/landscapist/CircularReveal;Lcom/skydoves/landscapist/ShimmerParams;Lcom/skydoves/landscapist/palette/BitmapPalette;ILkotlin/jvm/functions/Function4;Lkotlin/jvm/functions/Function4;Landroidx/compose/runtime/Composer;III)V
public static final fun GlideImage (Ljava/lang/Object;Landroidx/compose/ui/Modifier;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Landroidx/compose/ui/Alignment;Landroidx/compose/ui/layout/ContentScale;Ljava/lang/String;FLandroidx/compose/ui/graphics/ColorFilter;Lcom/skydoves/landscapist/CircularReveal;Lcom/skydoves/landscapist/palette/BitmapPalette;ILkotlin/jvm/functions/Function4;Lkotlin/jvm/functions/Function4;Lkotlin/jvm/functions/Function4;Landroidx/compose/runtime/Composer;III)V
public static final fun GlideImage (Ljava/lang/Object;Landroidx/compose/ui/Modifier;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Landroidx/compose/ui/Alignment;Landroidx/compose/ui/layout/ContentScale;Ljava/lang/String;FLandroidx/compose/ui/graphics/ColorFilter;Lcom/skydoves/landscapist/CircularReveal;Lcom/skydoves/landscapist/palette/BitmapPalette;Lcom/skydoves/landscapist/ShimmerParams;Ljava/lang/Object;ILandroidx/compose/runtime/Composer;III)V
public static final fun GlideImage (Ljava/lang/Object;Landroidx/compose/ui/Modifier;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Landroidx/compose/ui/Alignment;Landroidx/compose/ui/layout/ContentScale;Ljava/lang/String;FLandroidx/compose/ui/graphics/ColorFilter;Lcom/skydoves/landscapist/CircularReveal;Lcom/skydoves/landscapist/palette/BitmapPalette;Ljava/lang/Object;Ljava/lang/Object;ILandroidx/compose/runtime/Composer;III)V
public static final fun GlideImage (Ljava/lang/Object;Landroidx/compose/ui/Modifier;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lcom/bumptech/glide/request/RequestListener;Landroidx/compose/ui/Alignment;Landroidx/compose/ui/layout/ContentScale;Ljava/lang/String;FLandroidx/compose/ui/graphics/ColorFilter;Lcom/skydoves/landscapist/CircularReveal;Lcom/skydoves/landscapist/ShimmerParams;Lcom/skydoves/landscapist/palette/BitmapPalette;ILkotlin/jvm/functions/Function4;Lkotlin/jvm/functions/Function4;Landroidx/compose/runtime/Composer;III)V
public static final fun GlideImage (Ljava/lang/Object;Landroidx/compose/ui/Modifier;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lcom/bumptech/glide/request/RequestListener;Landroidx/compose/ui/Alignment;Landroidx/compose/ui/layout/ContentScale;Ljava/lang/String;FLandroidx/compose/ui/graphics/ColorFilter;Lcom/skydoves/landscapist/CircularReveal;Lcom/skydoves/landscapist/palette/BitmapPalette;ILkotlin/jvm/functions/Function4;Lkotlin/jvm/functions/Function4;Lkotlin/jvm/functions/Function4;Landroidx/compose/runtime/Composer;III)V
public static final fun GlideImage (Ljava/lang/Object;Landroidx/compose/ui/Modifier;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lcom/bumptech/glide/request/RequestListener;Landroidx/compose/ui/Alignment;Landroidx/compose/ui/layout/ContentScale;Ljava/lang/String;FLandroidx/compose/ui/graphics/ColorFilter;Lcom/skydoves/landscapist/CircularReveal;Lcom/skydoves/landscapist/palette/BitmapPalette;Lcom/skydoves/landscapist/ShimmerParams;Ljava/lang/Object;ILandroidx/compose/runtime/Composer;III)V
public static final fun GlideImage (Ljava/lang/Object;Landroidx/compose/ui/Modifier;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lcom/bumptech/glide/request/RequestListener;Landroidx/compose/ui/Alignment;Landroidx/compose/ui/layout/ContentScale;Ljava/lang/String;FLandroidx/compose/ui/graphics/ColorFilter;Lcom/skydoves/landscapist/CircularReveal;Lcom/skydoves/landscapist/palette/BitmapPalette;Ljava/lang/Object;Ljava/lang/Object;ILandroidx/compose/runtime/Composer;III)V
}

public abstract class com/skydoves/landscapist/glide/GlideImageState {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import androidx.compose.ui.res.painterResource
import androidx.core.graphics.drawable.toBitmap
import androidx.palette.graphics.Palette
import com.bumptech.glide.RequestBuilder
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.RequestOptions
import com.skydoves.landscapist.CircularReveal
import com.skydoves.landscapist.CircularRevealImage
Expand Down Expand Up @@ -74,6 +75,7 @@ import kotlinx.coroutines.flow.callbackFlow
* @param modifier [Modifier] used to adjust the layout or drawing content.
* @param requestBuilder Most options in Glide can be applied directly on the RequestBuilder object returned by Glide.with().
* @param requestOptions Provides type independent options to customize loads with Glide.
* @param requestListener A class for monitoring the status of a request while images load.
* @param alignment The alignment parameter used to place the loaded [ImageBitmap] in the image container.
* @param alpha The alpha parameter used to apply for the image when it is rendered onscreen.
* @param contentScale The scale parameter used to determine the aspect ratio scaling to be
Expand All @@ -96,6 +98,7 @@ public fun GlideImage(
requestOptions: @Composable () -> RequestOptions = {
LocalGlideProvider.getGlideRequestOptions()
},
requestListener: RequestListener<Drawable>? = null,
alignment: Alignment = Alignment.Center,
contentScale: ContentScale = ContentScale.Crop,
contentDescription: String? = null,
Expand All @@ -111,6 +114,7 @@ public fun GlideImage(
imageModel = imageModel,
requestBuilder = requestBuilder,
requestOptions = requestOptions,
requestListener = requestListener,
modifier = modifier,
alignment = alignment,
contentScale = contentScale,
Expand Down Expand Up @@ -174,6 +178,7 @@ public fun GlideImage(
* @param modifier [Modifier] used to adjust the layout or drawing content.
* @param requestBuilder Most options in Glide can be applied directly on the RequestBuilder object returned by Glide.with().
* @param requestOptions Provides type independent options to customize loads with Glide.
* @param requestListener A class for monitoring the status of a request while images load.
* @param alignment The alignment parameter used to place the loaded [ImageBitmap] in the image container.
* @param alpha The alpha parameter used to apply for the image when it is rendered onscreen.
* @param contentScale The scale parameter used to determine the aspect ratio scaling to be
Expand All @@ -196,6 +201,7 @@ public fun GlideImage(
requestOptions: @Composable () -> RequestOptions = {
LocalGlideProvider.getGlideRequestOptions()
},
requestListener: RequestListener<Drawable>? = null,
alignment: Alignment = Alignment.Center,
contentScale: ContentScale = ContentScale.Crop,
contentDescription: String? = null,
Expand All @@ -211,6 +217,7 @@ public fun GlideImage(
imageModel = imageModel,
requestBuilder = requestBuilder,
requestOptions = requestOptions,
requestListener = requestListener,
modifier = modifier,
alignment = alignment,
contentScale = contentScale,
Expand Down Expand Up @@ -263,6 +270,7 @@ public fun GlideImage(
* @param modifier [Modifier] used to adjust the layout or drawing content.
* @param requestBuilder Most options in Glide can be applied directly on the RequestBuilder object returned by Glide.with().
* @param requestOptions Provides type independent options to customize loads with Glide.
* @param requestListener A class for monitoring the status of a request while images load.
* @param alignment The alignment parameter used to place the loaded [ImageBitmap] in the image container.
* @param alpha The alpha parameter used to apply for the image when it is rendered onscreen.
* @param contentScale The scale parameter used to determine the aspect ratio scaling to be
Expand All @@ -286,6 +294,7 @@ public fun GlideImage(
requestOptions: @Composable () -> RequestOptions = {
LocalGlideProvider.getGlideRequestOptions()
},
requestListener: RequestListener<Drawable>? = null,
alignment: Alignment = Alignment.Center,
contentScale: ContentScale = ContentScale.Crop,
contentDescription: String? = null,
Expand Down Expand Up @@ -316,6 +325,7 @@ public fun GlideImage(
builder = requestBuilder.invoke()
.apply(requestOptions.invoke())
.load(imageModel),
requestListener = requestListener,
bitmapPalette = bitmapPalette,
modifier = modifier,
) ImageRequest@{ imageState ->
Expand Down Expand Up @@ -383,6 +393,7 @@ public fun GlideImage(
* @param modifier [Modifier] used to adjust the layout or drawing content.
* @param requestBuilder Most options in Glide can be applied directly on the RequestBuilder object returned by Glide.with().
* @param requestOptions Provides type independent options to customize loads with Glide.
* @param requestListener A class for monitoring the status of a request while images load.
* @param alignment The alignment parameter used to place the loaded [ImageBitmap] in the image container.
* @param alpha The alpha parameter used to apply for the image when it is rendered onscreen.
* @param contentScale The scale parameter used to determine the aspect ratio scaling to be
Expand All @@ -406,6 +417,7 @@ public fun GlideImage(
requestOptions: @Composable () -> RequestOptions = {
LocalGlideProvider.getGlideRequestOptions()
},
requestListener: RequestListener<Drawable>? = null,
alignment: Alignment = Alignment.Center,
contentScale: ContentScale = ContentScale.Crop,
contentDescription: String? = null,
Expand Down Expand Up @@ -436,6 +448,7 @@ public fun GlideImage(
builder = requestBuilder.invoke()
.apply(requestOptions.invoke())
.load(imageModel),
requestListener = requestListener,
modifier = modifier,
bitmapPalette = bitmapPalette
) ImageRequest@{ imageState ->
Expand Down Expand Up @@ -492,13 +505,15 @@ public fun GlideImage(
* @param builder The request to execute.
* @param modifier [Modifier] used to adjust the layout or drawing content.
* @param bitmapPalette A [Palette] generator for extracting major (theme) colors from images.
* @param requestListener A class for monitoring the status of a request while images load.
* @param content Content to be displayed for the given state.
*/
@Composable
private fun GlideImage(
recomposeKey: Any?,
builder: RequestBuilder<Drawable>,
modifier: Modifier = Modifier,
requestListener: RequestListener<Drawable>? = null,
bitmapPalette: BitmapPalette? = null,
content: @Composable BoxScope.(imageState: ImageLoadState) -> Unit
) {
Expand All @@ -509,7 +524,7 @@ private fun GlideImage(
executeImageRequest = {
callbackFlow {
val target = FlowCustomTarget(this)
val requestListener =
val flowRequestListener =
FlowRequestListener(
this, bitmapPalette?.applyImageModel(recomposeKey)
)
Expand All @@ -518,6 +533,7 @@ private fun GlideImage(
requestManager
.load(recomposeKey)
.apply(builder)
.addListener(flowRequestListener)
.addListener(requestListener)
.into(target)

Expand Down

0 comments on commit 36c5a21

Please sign in to comment.