From b46ccdfe5306a2746966da383c7d859d3b76a27f Mon Sep 17 00:00:00 2001 From: skydoves Date: Sat, 17 Jun 2023 23:02:27 +0900 Subject: [PATCH] Add clearTarget parameter and make it optional --- glide/api/glide.api | 2 +- .../com/skydoves/landscapist/glide/GlideImage.kt | 6 ++++++ .../skydoves/landscapist/glide/RememberTarget.kt | 16 ++++++++++++---- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/glide/api/glide.api b/glide/api/glide.api index 94a17fb5..86f09d81 100644 --- a/glide/api/glide.api +++ b/glide/api/glide.api @@ -6,7 +6,7 @@ public final class com/skydoves/landscapist/glide/ComposableSingletons$GlideImag } public final class com/skydoves/landscapist/glide/GlideImage { - public static final fun GlideImage (Lkotlin/jvm/functions/Function0;Landroidx/compose/ui/Modifier;Lcom/skydoves/landscapist/glide/GlideRequestType;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function0;Lcom/skydoves/landscapist/components/ImageComponent;Lcom/skydoves/landscapist/ImageOptions;Lkotlin/jvm/functions/Function1;ILkotlin/jvm/functions/Function4;Lkotlin/jvm/functions/Function5;Lkotlin/jvm/functions/Function4;Landroidx/compose/runtime/Composer;III)V + public static final fun GlideImage (Lkotlin/jvm/functions/Function0;Landroidx/compose/ui/Modifier;Lcom/skydoves/landscapist/glide/GlideRequestType;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function0;Lcom/skydoves/landscapist/components/ImageComponent;Lcom/skydoves/landscapist/ImageOptions;ZLkotlin/jvm/functions/Function1;ILkotlin/jvm/functions/Function4;Lkotlin/jvm/functions/Function5;Lkotlin/jvm/functions/Function4;Landroidx/compose/runtime/Composer;III)V } public abstract class com/skydoves/landscapist/glide/GlideImageState : com/skydoves/landscapist/ImageState { diff --git a/glide/src/main/kotlin/com/skydoves/landscapist/glide/GlideImage.kt b/glide/src/main/kotlin/com/skydoves/landscapist/glide/GlideImage.kt index a7bd182d..a3458703 100644 --- a/glide/src/main/kotlin/com/skydoves/landscapist/glide/GlideImage.kt +++ b/glide/src/main/kotlin/com/skydoves/landscapist/glide/GlideImage.kt @@ -90,6 +90,7 @@ import kotlinx.coroutines.flow.callbackFlow * @param requestListener A class for monitoring the status of a request while images load. * @param component An image component that conjuncts pluggable [ImagePlugin]s. * @param imageOptions Represents parameters to load generic [Image] Composable. + * @param clearTarget Whether clear the target or not. * @param onImageStateChanged An image state change listener will be triggered whenever the image state is changed. * @param previewPlaceholder Drawable resource ID which will be displayed when this function is ran in preview mode. * @param loading Content to be displayed when the request is in progress. @@ -110,6 +111,7 @@ public fun GlideImage( requestListener: (() -> RequestListener)? = null, component: ImageComponent = rememberImageComponent {}, imageOptions: ImageOptions = ImageOptions(), + clearTarget: Boolean = false, onImageStateChanged: (GlideImageState) -> Unit = {}, @DrawableRes previewPlaceholder: Int = 0, loading: @Composable (BoxScope.(imageState: GlideImageState.Loading) -> Unit)? = null, @@ -146,6 +148,7 @@ public fun GlideImage( ), glideRequestType = glideRequestType, requestListener = StableHolder(requestListener?.invoke()), + clearTarget = clearTarget, modifier = modifier, ) ImageRequest@{ imageState -> when ( @@ -246,6 +249,7 @@ public fun GlideImage( * @param modifier [Modifier] used to adjust the layout or drawing content. * @param imageOptions Represents parameters to load generic [Image] Composable. * @param glideRequestType Glide image request type, which decides the result of image data. + * @param clearTarget Whether clear the target or not. * @param builder The request to execute. * @param requestListener A class for monitoring the status of a request while images load. * @param content Content to be displayed for the given state. @@ -256,6 +260,7 @@ private fun GlideImage( modifier: Modifier = Modifier, imageOptions: ImageOptions, glideRequestType: GlideRequestType, + clearTarget: Boolean = false, builder: StableHolder>, requestListener: StableHolder?> = StableHolder(null), content: @Composable BoxWithConstraintsScope.(imageState: ImageLoadState) -> Unit, @@ -264,6 +269,7 @@ private fun GlideImage( val target = rememberTarget( target = FlowCustomTarget(imageOptions = imageOptions), imageOptions = imageOptions, + clearTarget = clearTarget, ) ImageLoad( diff --git a/glide/src/main/kotlin/com/skydoves/landscapist/glide/RememberTarget.kt b/glide/src/main/kotlin/com/skydoves/landscapist/glide/RememberTarget.kt index 9e5f2603..2afb3479 100644 --- a/glide/src/main/kotlin/com/skydoves/landscapist/glide/RememberTarget.kt +++ b/glide/src/main/kotlin/com/skydoves/landscapist/glide/RememberTarget.kt @@ -30,17 +30,20 @@ import com.skydoves.landscapist.ImageOptions * * @param target the [FlowCustomTarget] to be remembered * @param imageOptions the [ImageOptions] to be used a key. + * @param clearTarget Whether clear the target or not. */ @Composable internal fun rememberTarget( target: FlowCustomTarget, imageOptions: ImageOptions, + clearTarget: Boolean, ): FlowCustomTarget { val context = LocalContext.current return remember(target, imageOptions) { RememberableTarget( - context.applicationContext, - target, + context = context.applicationContext, + target = target, + clearTarget = clearTarget, ) }.value } @@ -48,6 +51,7 @@ internal fun rememberTarget( internal class RememberableTarget( private val context: Context, private val target: FlowCustomTarget, + private val clearTarget: Boolean, ) : RememberObserver { internal val value: FlowCustomTarget @@ -58,10 +62,14 @@ internal class RememberableTarget( } override fun onAbandoned() { - Glide.with(context).clear(target) + if (clearTarget) { + Glide.with(context).clear(target) + } } override fun onForgotten() { - Glide.with(context).clear(target) + if (clearTarget) { + Glide.with(context).clear(target) + } } }