2017-02-04 13:07:06 -05:00
|
|
|
package eu.kanade.tachiyomi.widget
|
|
|
|
|
|
|
|
import android.graphics.drawable.Drawable
|
2017-02-04 14:49:07 -05:00
|
|
|
import android.view.View
|
2017-02-04 13:07:06 -05:00
|
|
|
import android.widget.ImageView
|
|
|
|
import android.widget.ImageView.ScaleType
|
2020-01-28 22:47:57 -05:00
|
|
|
import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat
|
2017-10-14 12:16:11 -04:00
|
|
|
import com.bumptech.glide.request.target.ImageViewTarget
|
|
|
|
import com.bumptech.glide.request.transition.Transition
|
2017-02-04 14:49:07 -05:00
|
|
|
import eu.kanade.tachiyomi.R
|
2020-02-02 22:22:54 -05:00
|
|
|
import eu.kanade.tachiyomi.util.system.getResourceColor
|
|
|
|
import eu.kanade.tachiyomi.util.view.gone
|
|
|
|
import eu.kanade.tachiyomi.util.view.visible
|
2017-02-04 13:07:06 -05:00
|
|
|
|
|
|
|
/**
|
2017-02-04 14:49:07 -05:00
|
|
|
* A glide target to display an image with an optional view to show while loading and a configurable
|
|
|
|
* error drawable.
|
2017-02-04 13:07:06 -05:00
|
|
|
*
|
|
|
|
* @param view the view where the image will be loaded
|
2017-02-04 14:49:07 -05:00
|
|
|
* @param progress an optional view to show when the image is loading.
|
|
|
|
* @param errorDrawableRes the error drawable resource to show.
|
2017-02-04 13:07:06 -05:00
|
|
|
* @param errorScaleType the scale type for the error drawable, [ScaleType.CENTER] by default.
|
|
|
|
*/
|
|
|
|
class StateImageViewTarget(view: ImageView,
|
2017-02-04 14:49:07 -05:00
|
|
|
val progress: View? = null,
|
|
|
|
val errorDrawableRes: Int = R.drawable.ic_broken_image_grey_24dp,
|
2017-02-04 13:07:06 -05:00
|
|
|
val errorScaleType: ScaleType = ScaleType.CENTER) :
|
2017-10-14 12:16:11 -04:00
|
|
|
|
|
|
|
ImageViewTarget<Drawable>(view) {
|
|
|
|
|
|
|
|
private var resource: Drawable? = null
|
2017-02-04 13:07:06 -05:00
|
|
|
|
|
|
|
private val imageScaleType = view.scaleType
|
|
|
|
|
2017-10-14 12:16:11 -04:00
|
|
|
override fun setResource(resource: Drawable?) {
|
|
|
|
view.setImageDrawable(resource)
|
|
|
|
}
|
|
|
|
|
2017-02-04 13:07:06 -05:00
|
|
|
override fun onLoadStarted(placeholder: Drawable?) {
|
|
|
|
progress?.visible()
|
|
|
|
super.onLoadStarted(placeholder)
|
|
|
|
}
|
|
|
|
|
2017-10-14 12:16:11 -04:00
|
|
|
override fun onLoadFailed(errorDrawable: Drawable?) {
|
2017-02-04 13:07:06 -05:00
|
|
|
progress?.gone()
|
|
|
|
view.scaleType = errorScaleType
|
2017-02-04 14:49:07 -05:00
|
|
|
|
|
|
|
val vector = VectorDrawableCompat.create(view.context.resources, errorDrawableRes, null)
|
|
|
|
vector?.setTint(view.context.getResourceColor(android.R.attr.textColorSecondary))
|
|
|
|
view.setImageDrawable(vector)
|
2017-02-04 13:07:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onLoadCleared(placeholder: Drawable?) {
|
|
|
|
progress?.gone()
|
|
|
|
super.onLoadCleared(placeholder)
|
|
|
|
}
|
|
|
|
|
2018-02-18 14:02:31 -05:00
|
|
|
override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) {
|
2017-02-04 13:07:06 -05:00
|
|
|
progress?.gone()
|
|
|
|
view.scaleType = imageScaleType
|
2017-10-14 12:16:11 -04:00
|
|
|
super.onResourceReady(resource, transition)
|
|
|
|
this.resource = resource
|
2017-02-04 13:07:06 -05:00
|
|
|
}
|
2018-02-18 14:02:31 -05:00
|
|
|
}
|