mihon/app/src/main/java/eu/kanade/tachiyomi/widget/StateImageViewTarget.kt

65 lines
2.3 KiB
Kotlin
Raw Normal View History

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