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

Expand onSuccess to function parameter #93

Open
har-nick opened this issue Feb 16, 2024 · 2 comments
Open

Expand onSuccess to function parameter #93

har-nick opened this issue Feb 16, 2024 · 2 comments

Comments

@har-nick
Copy link

har-nick commented Feb 16, 2024

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
Copy link
Member

luca992 commented Feb 16, 2024

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
Copy link
Author

har-nick commented Feb 16, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants