Kamel-Media / Kamel

Kotlin asynchronous media loading and caching library for Compose.
Apache License 2.0
636 stars 24 forks source link

Expand onSuccess to function parameter #93

Open har-nick opened 7 months ago

har-nick commented 7 months ago

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.

luca992 commented 7 months ago

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

har-nick commented 7 months ago

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.