2017-02-04 19:07:06 +01:00
|
|
|
package eu.kanade.tachiyomi.widget
|
|
|
|
|
|
|
|
import android.graphics.drawable.Drawable
|
2017-02-04 20:49:07 +01:00
|
|
|
import android.view.View
|
2017-02-04 19:07:06 +01:00
|
|
|
import android.widget.ImageView
|
|
|
|
import android.widget.ImageView.ScaleType
|
2020-07-30 00:36:28 +02:00
|
|
|
import androidx.appcompat.content.res.AppCompatResources
|
2020-07-25 17:55:47 +02:00
|
|
|
import androidx.core.view.isVisible
|
2017-10-14 18:16:11 +02:00
|
|
|
import com.bumptech.glide.request.target.ImageViewTarget
|
|
|
|
import com.bumptech.glide.request.transition.Transition
|
2017-02-04 20:49:07 +01:00
|
|
|
import eu.kanade.tachiyomi.R
|
2020-04-26 23:24:31 +02:00
|
|
|
import eu.kanade.tachiyomi.util.system.getResourceColor
|
2017-02-04 19:07:06 +01:00
|
|
|
|
|
|
|
/**
|
2017-02-04 20:49:07 +01:00
|
|
|
* A glide target to display an image with an optional view to show while loading and a configurable
|
|
|
|
* error drawable.
|
2017-02-04 19:07:06 +01:00
|
|
|
*
|
|
|
|
* @param view the view where the image will be loaded
|
2017-02-04 20:49:07 +01:00
|
|
|
* @param progress an optional view to show when the image is loading.
|
|
|
|
* @param errorDrawableRes the error drawable resource to show.
|
2017-02-04 19:07:06 +01:00
|
|
|
* @param errorScaleType the scale type for the error drawable, [ScaleType.CENTER] by default.
|
|
|
|
*/
|
2020-02-27 00:03:34 +01:00
|
|
|
class StateImageViewTarget(
|
|
|
|
view: ImageView,
|
|
|
|
val progress: View? = null,
|
2020-04-19 17:13:04 +02:00
|
|
|
private val errorDrawableRes: Int = R.drawable.ic_broken_image_grey_24dp,
|
|
|
|
private val errorScaleType: ScaleType = ScaleType.CENTER
|
|
|
|
) : ImageViewTarget<Drawable>(view) {
|
2017-10-14 18:16:11 +02:00
|
|
|
|
|
|
|
private var resource: Drawable? = null
|
2017-02-04 19:07:06 +01:00
|
|
|
|
|
|
|
private val imageScaleType = view.scaleType
|
|
|
|
|
2017-10-14 18:16:11 +02:00
|
|
|
override fun setResource(resource: Drawable?) {
|
|
|
|
view.setImageDrawable(resource)
|
|
|
|
}
|
|
|
|
|
2017-02-04 19:07:06 +01:00
|
|
|
override fun onLoadStarted(placeholder: Drawable?) {
|
2020-07-25 17:55:47 +02:00
|
|
|
progress?.isVisible = true
|
2017-02-04 19:07:06 +01:00
|
|
|
super.onLoadStarted(placeholder)
|
|
|
|
}
|
|
|
|
|
2017-10-14 18:16:11 +02:00
|
|
|
override fun onLoadFailed(errorDrawable: Drawable?) {
|
2020-07-25 17:55:47 +02:00
|
|
|
progress?.isVisible = false
|
2017-02-04 19:07:06 +01:00
|
|
|
view.scaleType = errorScaleType
|
2017-02-04 20:49:07 +01:00
|
|
|
|
2020-07-30 00:36:28 +02:00
|
|
|
val vector = AppCompatResources.getDrawable(view.context, errorDrawableRes)
|
2020-04-26 23:24:31 +02:00
|
|
|
vector?.setTint(view.context.getResourceColor(R.attr.colorOnBackground, 0.38f))
|
2017-02-04 20:49:07 +01:00
|
|
|
view.setImageDrawable(vector)
|
2017-02-04 19:07:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onLoadCleared(placeholder: Drawable?) {
|
2020-07-25 17:55:47 +02:00
|
|
|
progress?.isVisible = false
|
2017-02-04 19:07:06 +01:00
|
|
|
super.onLoadCleared(placeholder)
|
|
|
|
}
|
|
|
|
|
2018-02-18 20:02:31 +01:00
|
|
|
override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) {
|
2020-07-25 17:55:47 +02:00
|
|
|
progress?.isVisible = false
|
2017-02-04 19:07:06 +01:00
|
|
|
view.scaleType = imageScaleType
|
2017-10-14 18:16:11 +02:00
|
|
|
super.onResourceReady(resource, transition)
|
|
|
|
this.resource = resource
|
2017-02-04 19:07:06 +01:00
|
|
|
}
|
2018-02-18 20:02:31 +01:00
|
|
|
}
|