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.support.graphics.drawable.VectorDrawableCompat
|
|
|
|
import android.view.View
|
2017-02-04 19:07:06 +01:00
|
|
|
import android.widget.ImageView
|
|
|
|
import android.widget.ImageView.ScaleType
|
|
|
|
import com.bumptech.glide.load.resource.drawable.GlideDrawable
|
|
|
|
import com.bumptech.glide.request.animation.GlideAnimation
|
|
|
|
import com.bumptech.glide.request.target.GlideDrawableImageViewTarget
|
2017-02-04 20:49:07 +01:00
|
|
|
import eu.kanade.tachiyomi.R
|
|
|
|
import eu.kanade.tachiyomi.util.getResourceColor
|
2017-02-04 19:07:06 +01:00
|
|
|
import eu.kanade.tachiyomi.util.gone
|
|
|
|
import eu.kanade.tachiyomi.util.visible
|
|
|
|
|
|
|
|
/**
|
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.
|
|
|
|
*/
|
|
|
|
class StateImageViewTarget(view: ImageView,
|
2017-02-04 20:49:07 +01:00
|
|
|
val progress: View? = null,
|
|
|
|
val errorDrawableRes: Int = R.drawable.ic_broken_image_grey_24dp,
|
2017-02-04 19:07:06 +01:00
|
|
|
val errorScaleType: ScaleType = ScaleType.CENTER) :
|
|
|
|
GlideDrawableImageViewTarget(view) {
|
|
|
|
|
|
|
|
private val imageScaleType = view.scaleType
|
|
|
|
|
|
|
|
override fun onLoadStarted(placeholder: Drawable?) {
|
|
|
|
progress?.visible()
|
|
|
|
super.onLoadStarted(placeholder)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onLoadFailed(e: Exception?, errorDrawable: Drawable?) {
|
|
|
|
progress?.gone()
|
|
|
|
view.scaleType = errorScaleType
|
2017-02-04 20:49:07 +01: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 19:07:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onLoadCleared(placeholder: Drawable?) {
|
|
|
|
progress?.gone()
|
|
|
|
super.onLoadCleared(placeholder)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onResourceReady(resource: GlideDrawable?, animation: GlideAnimation<in GlideDrawable>?) {
|
|
|
|
progress?.gone()
|
|
|
|
view.scaleType = imageScaleType
|
|
|
|
super.onResourceReady(resource, animation)
|
|
|
|
}
|
|
|
|
}
|