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

Implement rememberTarget and clear target for Glide #275

Merged
merged 1 commit into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,10 @@ import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.BoxWithConstraintsScope
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalInspectionMode
import androidx.compose.ui.res.painterResource
import com.bumptech.glide.Glide
import com.bumptech.glide.RequestBuilder
import com.bumptech.glide.RequestManager
import com.bumptech.glide.load.resource.gif.GifDrawable
Expand Down Expand Up @@ -251,9 +248,10 @@ private fun GlideImage(
content: @Composable BoxWithConstraintsScope.(imageState: ImageLoadState) -> Unit,
) {
val requestManager = LocalGlideProvider.getGlideRequestManager()
val target =
remember(recomposeKey, imageOptions) { FlowCustomTarget(imageOptions = imageOptions) }
val context = LocalContext.current
val target = rememberTarget(
target = FlowCustomTarget(imageOptions = imageOptions),
imageOptions = imageOptions,
)

ImageLoad(
recomposeKey = recomposeKey.value,
Expand Down Expand Up @@ -281,9 +279,6 @@ private fun GlideImage(
}
},
imageOptions = imageOptions,
disposable = {
Glide.with(context).clear(target)
},
modifier = modifier,
content = content,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Designed and developed by 2020-2023 skydoves (Jaewoong Eum)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.skydoves.landscapist.glide

import android.content.Context
import androidx.compose.runtime.Composable
import androidx.compose.runtime.RememberObserver
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import com.bumptech.glide.Glide
import com.skydoves.landscapist.ImageOptions

/**
* Remember the given [FlowCustomTarget] in the current composition.
*
* The [FlowCustomTarget] will be cleared when the object leaves the composition.
*
* @param target the [FlowCustomTarget] to be remembered
* @param imageOptions the [ImageOptions] to be used a key.
*/
@Composable
internal fun rememberTarget(
target: FlowCustomTarget,
imageOptions: ImageOptions,
): FlowCustomTarget {
val context = LocalContext.current
return remember(target, imageOptions) { RememberableTarget(context, target) }.value
}

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

internal val value: FlowCustomTarget
get() = target

override fun onRemembered() {
// no-op
}

override fun onAbandoned() {
Glide.with(context).clear(target)
}

override fun onForgotten() {
Glide.with(context).clear(target)
}
}
2 changes: 1 addition & 1 deletion landscapist/api/landscapist.api
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public final class com/skydoves/landscapist/DrawablePainterKt {
}

public final class com/skydoves/landscapist/ImageLoad {
public static final fun ImageLoad (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;Landroidx/compose/ui/Modifier;Lcom/skydoves/landscapist/ImageOptions;Lcom/skydoves/landscapist/constraints/Constrainable;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function4;Landroidx/compose/runtime/Composer;II)V
public static final fun ImageLoad (Ljava/lang/Object;Lkotlin/jvm/functions/Function1;Landroidx/compose/ui/Modifier;Lcom/skydoves/landscapist/ImageOptions;Lcom/skydoves/landscapist/constraints/Constrainable;Lkotlin/jvm/functions/Function4;Landroidx/compose/runtime/Composer;II)V
public static final fun getZeroConstraints ()J
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ package com.skydoves.landscapist
import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.foundation.layout.BoxWithConstraintsScope
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.Constraints
Expand All @@ -38,7 +36,6 @@ import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.emitAll
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.launch

/**
* A common image loading model for fetching an image asynchronously and
Expand All @@ -56,7 +53,6 @@ public fun <T : Any> ImageLoad(
modifier: Modifier = Modifier,
imageOptions: ImageOptions,
constrainable: Constrainable? = null,
disposable: (() -> Unit)? = null,
content: @Composable BoxWithConstraintsScope.(imageState: ImageLoadState) -> Unit,
) {
var state by remember(recomposeKey, imageOptions) {
Expand All @@ -73,14 +69,8 @@ public fun <T : Any> ImageLoad(
modifier = modifier.imageSemantics(imageOptions),
propagateMinConstraints = true,
) {
val coroutineScope = rememberCoroutineScope()
DisposableEffect(key1 = recomposeKey, key2 = imageOptions) {
coroutineScope.launch {
constrainable?.setConstraints(constraints)
}
onDispose {
disposable?.invoke()
}
LaunchedEffect(key1 = recomposeKey, key2 = imageOptions) {
constrainable?.setConstraints(constraints)
}

content(state)
Expand Down