Kamel-Media / Kamel

Kotlin asynchronous media loading and caching library for Compose.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Expand onSuccess to function parameter

har-nick opened this issue · comments

I noticed KamelImage already has an onSuccess function stored as a val. It'd be nice to append more code to it for cases like state management when images are loaded, instead of running multiple times in onLoading.

So something like:

@OptIn(ExperimentalKamelApi::class)
@Composable
public fun KamelImage(
    resource: Resource<Painter>,
    contentDescription: String?,
    modifier: Modifier = Modifier,
    alignment: Alignment = Alignment.Center,
    contentScale: ContentScale = ContentScale.Fit,
    alpha: Float = DefaultAlpha,
    colorFilter: ColorFilter? = null,
    onLoading: @Composable (BoxScope.(Float) -> Unit)? = null,
    onFailure: @Composable (BoxScope.(Throwable) -> Unit)? = null,
    onSuccess: (() -> Unit)? = null,
    contentAlignment: Alignment = Alignment.Center,
    animationSpec: FiniteAnimationSpec<Float>? = null,
) {
    val displayOnSuccess: @Composable (BoxScope.(Painter) -> Unit) = { painter ->
        Image(
            painter,
            contentDescription,
            Modifier.fillMaxSize(),
            alignment,
            contentScale,
            alpha,
            colorFilter
        )
        onSuccess?.invoke()
    }
    KamelImageBox(
        resource,
        modifier,
        contentAlignment,
        animationSpec,
        onLoading,
        onFailure,
        displayOnSuccess,
    )
}

?

I can't really think of a common use case where you want to trigger something when an image loads. Do you have any any example use cases?

Also, you can always just use KamelImageBox directly and make your own version of KamelImage

So something like [...]

Pretty much. I'd appreciate the opportunity to run a function only once when it's loaded.

Do you have any any example use cases?

One I'd use personally is a fade-in effect dependent on the KamelImage child component has loaded.

Also, you can always just use KamelImageBox directly

Hadn't thought of that in all honesty. Nontheless, I still think making onSuccess a parameter is worth the consideration.