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
|
2020-07-25 11:55:47 -04:00
|
|
|
import androidx.core.view.isVisible
|
2021-04-28 08:32:00 -04:00
|
|
|
import coil.drawable.CrossfadeDrawable
|
|
|
|
import coil.target.ImageViewTarget
|
2017-02-04 13:07:06 -05:00
|
|
|
|
|
|
|
/**
|
2021-04-28 08:32:00 -04:00
|
|
|
* A Coil target to display an image with an optional view to show while loading.
|
2017-02-04 13:07:06 -05:00
|
|
|
*
|
2021-04-28 08:32:00 -04:00
|
|
|
* @param target the view where the image will be loaded
|
|
|
|
* @param progress the view to show when the image is loading.
|
|
|
|
* @param crossfadeDuration duration in millisecond to crossfade the result drawable
|
2017-02-04 13:07:06 -05:00
|
|
|
*/
|
2020-02-26 18:03:34 -05:00
|
|
|
class StateImageViewTarget(
|
2021-04-28 08:32:00 -04:00
|
|
|
private val target: ImageView,
|
|
|
|
private val progress: View,
|
|
|
|
private val crossfadeDuration: Int = 0
|
|
|
|
) : ImageViewTarget(target) {
|
|
|
|
override fun onStart(placeholder: Drawable?) {
|
|
|
|
progress.isVisible = true
|
2017-02-04 13:07:06 -05:00
|
|
|
}
|
|
|
|
|
2021-04-28 08:32:00 -04:00
|
|
|
override fun onSuccess(result: Drawable) {
|
|
|
|
progress.isVisible = false
|
|
|
|
if (crossfadeDuration > 0) {
|
|
|
|
val crossfadeResult = CrossfadeDrawable(target.drawable, result, durationMillis = crossfadeDuration)
|
|
|
|
target.setImageDrawable(crossfadeResult)
|
|
|
|
crossfadeResult.start()
|
|
|
|
} else {
|
|
|
|
target.setImageDrawable(result)
|
|
|
|
}
|
2017-02-04 13:07:06 -05:00
|
|
|
}
|
|
|
|
|
2021-04-28 08:32:00 -04:00
|
|
|
override fun onError(error: Drawable?) {
|
|
|
|
progress.isVisible = false
|
|
|
|
if (error != null) {
|
|
|
|
target.setImageDrawable(error)
|
|
|
|
}
|
2017-02-04 13:07:06 -05:00
|
|
|
}
|
2018-02-18 14:02:31 -05:00
|
|
|
}
|