-
Notifications
You must be signed in to change notification settings - Fork 637
/
GenericViewTarget.kt
55 lines (44 loc) · 1.75 KB
/
GenericViewTarget.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package coil.target
import android.graphics.drawable.Animatable
import android.graphics.drawable.Drawable
import android.view.View
import android.widget.ImageView
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import coil.transition.TransitionTarget
/**
* An opinionated [ViewTarget] that simplifies updating the [Drawable] attached to a [View]
* and supports automatically starting and stopping animated [Drawable]s.
*
* If you need custom behaviour that this class doesn't support it's recommended
* to implement [ViewTarget] directly.
*/
abstract class GenericViewTarget<T : View> : ViewTarget<T>, TransitionTarget, DefaultLifecycleObserver {
private var isStarted = false
/**
* The current [Drawable] attached to [view].
*/
abstract override var drawable: Drawable?
override fun onStart(placeholder: Drawable?) = updateDrawable(placeholder)
override fun onError(error: Drawable?) = updateDrawable(error)
override fun onSuccess(result: Drawable) = updateDrawable(result)
override fun onStart(owner: LifecycleOwner) {
isStarted = true
updateAnimation()
}
override fun onStop(owner: LifecycleOwner) {
isStarted = false
updateAnimation()
}
/** Replace the [ImageView]'s current drawable with [drawable]. */
protected fun updateDrawable(drawable: Drawable?) {
(this.drawable as? Animatable)?.stop()
this.drawable = drawable
updateAnimation()
}
/** Start/stop the current [Drawable]'s animation based on the current lifecycle state. */
protected fun updateAnimation() {
val animatable = drawable as? Animatable ?: return
if (isStarted) animatable.start() else animatable.stop()
}
}