Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Get the Drawable from a success request on Compose #858

Closed
metehus opened this issue Aug 11, 2021 · 7 comments · Fixed by #887
Closed

Get the Drawable from a success request on Compose #858

metehus opened this issue Aug 11, 2021 · 7 comments · Fixed by #887
Labels
enhancement New feature or request

Comments

@metehus
Copy link

metehus commented Aug 11, 2021

Is your feature request related to a problem? Please describe.

There's a discussion on #851 about getting the drawable from the rememberImagePainter in order to use on other methods (like getting the color palette) without making another request externally.

The best way of doing that would be using the target from the ImageRequest. But, as seen on the discussion, it is set internally so it's not possible to use that on the image request builder inside the rememberImagePainter

    val painter = rememberImagePainter(
        imageURL,
        builder = {
            target( // target must be null
                onSuccess = {
                    doStuffWithDrawable(it)
                }
            )
        },
    )

Describe the solution you'd like

A solution i see to that would be a callback like the ImageRequest's target on the compose function, like so:

    val painter = rememberImagePainter(
        imageURL,
        callbacks = {
            onSuccess { drawable ->
                doStuffWithDrawable(drawable)
            }
        }
    )

The internal target would pipe the the onStart, onError and onSuccess from the internal target to the callbacks on the remember function.

@metehus metehus added the enhancement New feature or request label Aug 11, 2021
@metehus metehus changed the title Get the Bitmap from a success request on Compose Get the Drawable from a success request on Compose Aug 11, 2021
@brettmcginnis
Copy link

I was able to leverage the transform to grab the bitmap https://stackoverflow.com/q/68094647

@colinrtwhite
Copy link
Member

This should be easier in 2.0 since listener has the image result. The best way to grab the bitmap currently is to use ((rememberImagePainter(data).state.painter) as? BitmapPainter).bitmap

@metehus
Copy link
Author

metehus commented Aug 19, 2021

This should be easier in 2.0 since listener has the image result. The best way to grab the bitmap currently is to use ((rememberImagePainter(data).state.painter) as? BitmapPainter).bitmap

Oh, thats cool and much easier. Is there a way to check when the request is done? maybe checking for changes on the painter and check if it has a successful result?

@colinrtwhite
Copy link
Member

@metehus You can use listener(onSuccess = {}) to listen for when the request completes successfully. ImagePainter.state is also a compose state property so your composition will recompose automatically when it changes.

@tipialican
Copy link

@colinrtwhite is it possible to access the drawable in onSuccess?

@univeous
Copy link

This should be easier in 2.0 since has the image result. The best way to grab the bitmap currently is to use listener``((rememberImagePainter(data).state.painter) as? BitmapPainter).bitmap

Maybe I missed something. Is BitmapPainter have a bitmap property? I can't make this work properly.

@colinrtwhite
Copy link
Member

colinrtwhite commented Sep 12, 2021

@univeous Ah! Sorry I totally got this wrong and it's not possible to access the bitmap in BitmapPainter. Given the other solutions are hacky, I'm going to push 1.3.3 today which expose the Drawable in ImagePainter.state. That way you'll be able to get the drawable using:

val painter = rememberImagePainter("https://www.example.com/image.jpg")
val drawable = (painter.state.result as? SuccessResult)?.drawable
Image(painter = painter, contentDescription = null)

EDIT: Sorry for the delay. I've held of rolling out the update temporarily to verify that adding this attribute doesn't increase recompositions.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants