airbnb / epoxy

Epoxy is an Android library for building complex screens in a RecyclerView
https://goo.gl/eIK82p
Apache License 2.0
8.51k stars 728 forks source link

Working with animators inside of holders/models #1239

Open youlovejohnny opened 3 years ago

youlovejohnny commented 3 years ago

I have an view property animator and when i update controller with new data, animator is recreated. ` @EpoxyModelClass(layout = R.layout.item) abstract class MyEpoxyModel : ViewBindingEpoxyModelWithHolder() {

// other fields
var breatheAnimator: Animator? = null
var isAnimateRunning = false

override fun MyBinding.bind() {
// other binding
    maybeCreateAnimator(button)
    maybeStartAnimator()
}

private fun maybeCreateAnimator(view: View) {
    if (breatheAnimator == null) { // always null
        breatheAnimator = BreathInOutFactory().create(view, BREATHE_ANIMATION_DURATION)
    }
}

private fun maybeStartAnimator() {
    if (breatheAnimator == null) return
    if (!isAnimateRunning) {
        isAnimateRunning = true
        breatheAnimator?.start()
    }
}

override fun unbind(holder: ViewBindingHolder) {
    breatheAnimator?.cancel()
    isAnimateRunning = false
    super.unbind(holder)
}

companion object {
    const val BREATHE_ANIMATION_DURATION = 800L

}`

How can i keep animator from recreating and save animator state?