Skip to content

Commit

Permalink
Merge pull request #303 from skydoves/feature/clearTarget
Browse files Browse the repository at this point in the history
Add clearTarget parameter and make it optional
  • Loading branch information
skydoves authored Jun 17, 2023
2 parents 5eeb528 + b46ccdf commit 7596aad
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion glide/api/glide.api
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -110,6 +111,7 @@ public fun GlideImage(
requestListener: (() -> RequestListener<Any>)? = 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,
Expand Down Expand Up @@ -146,6 +148,7 @@ public fun GlideImage(
),
glideRequestType = glideRequestType,
requestListener = StableHolder(requestListener?.invoke()),
clearTarget = clearTarget,
modifier = modifier,
) ImageRequest@{ imageState ->
when (
Expand Down Expand Up @@ -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.
Expand All @@ -256,6 +260,7 @@ private fun GlideImage(
modifier: Modifier = Modifier,
imageOptions: ImageOptions,
glideRequestType: GlideRequestType,
clearTarget: Boolean = false,
builder: StableHolder<RequestBuilder<Any>>,
requestListener: StableHolder<RequestListener<Any>?> = StableHolder(null),
content: @Composable BoxWithConstraintsScope.(imageState: ImageLoadState) -> Unit,
Expand All @@ -264,6 +269,7 @@ private fun GlideImage(
val target = rememberTarget(
target = FlowCustomTarget(imageOptions = imageOptions),
imageOptions = imageOptions,
clearTarget = clearTarget,
)

ImageLoad(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,28 @@ 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
}

internal class RememberableTarget(
private val context: Context,
private val target: FlowCustomTarget,
private val clearTarget: Boolean,
) : RememberObserver {

internal val value: FlowCustomTarget
Expand All @@ -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)
}
}
}

0 comments on commit 7596aad

Please sign in to comment.