coil-kt / coil

Image loading for Android and Compose Multiplatform.

Home Page:https://coil-kt.github.io/coil/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ImageView.load no longer calls ImageRequest.Builder methods

dafi opened this issue · comments

Describe the bug
I updated my project from 1.4.0 to 2.0.0-rc03 and the following code stopped to work

        thumbImage.load("https://myurl.com") {
            placeholder(R.drawable.stub)
            target(object : ImageViewTarget(thumbImage) {
                override fun onStart(placeholder: Drawable?) {
                    System.err.println("dafi: PhotoGridViewHolder.onStart ")
                }
                override fun onSuccess(result: Drawable) {
                    System.err.println("dafi: PhotoGridViewHolder.onSuccess ")
                }
            })
        }

The methods onStart and onSuccess are correctly called under 1.4.0 but no longer called after the library update

After some investigation I copied the ImageView.load code discovering the call to target bypasses the calls to the ImageRequest.Builder passed to it, indeed the code below works fine but the lambda inside the apply is never called

        val viewContext = itemView.context
        val request = ImageRequest.Builder(viewContext)
            .apply {
                target { System.err.println("dafi: PhotoGridViewHolder.displayImage target from apply is not called") }
            }
            .data("https://myurl.com")
            .target(object : ImageViewTarget(thumbImage) {
                override fun onStart(placeholder: Drawable?) {
                    System.err.println("dafi: PhotoGridViewHolder.onStart " + placeholder)
                }
                override fun onSuccess(result: Drawable) {
                    System.err.println("dafi: PhotoGridViewHolder.onSuccess ")
                }
            })
            .build()
        viewContext.imageLoader.enqueue(request)

Is this a regression? I searched the documentation for some new behaviour but without success.

I repeat the first code snippet correctly works on 1.4.0.
To make the code working on 2.0.0-rc03 I need to patch it and pass ImageViewTarget directly to the request to enqueue,

This makes ImageView.load useless if not worse harmful

I think you're right that this change is unexpected in 2.x; fixed here. That said, I wouldn't recommend using load if you want to set a custom target. It's expected that your second code snippet won't have the first target called since you're overwriting it. It looks like your use case would be better suited by a listener.