From 174084a2564d631105215f48628f97e617bc5874 Mon Sep 17 00:00:00 2001 From: Valere Date: Thu, 25 Jun 2020 18:12:34 +0200 Subject: [PATCH] added download animation --- .../ContentDownloadStateTrackerBinder.kt | 30 ++++-- .../main/res/drawable/ic_download_anim.xml | 102 ++++++++++++++++++ 2 files changed, 126 insertions(+), 6 deletions(-) create mode 100644 vector/src/main/res/drawable/ic_download_anim.xml diff --git a/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/helper/ContentDownloadStateTrackerBinder.kt b/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/helper/ContentDownloadStateTrackerBinder.kt index 47ffe47132..68707be5bb 100644 --- a/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/helper/ContentDownloadStateTrackerBinder.kt +++ b/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/helper/ContentDownloadStateTrackerBinder.kt @@ -16,6 +16,9 @@ package im.vector.riotx.features.home.room.detail.timeline.helper +import android.graphics.drawable.Drawable +import androidx.vectordrawable.graphics.drawable.Animatable2Compat +import androidx.vectordrawable.graphics.drawable.AnimatedVectorDrawableCompat import im.vector.matrix.android.internal.session.download.ContentDownloadStateTracker import im.vector.riotx.R import im.vector.riotx.core.di.ActiveSessionHolder @@ -30,7 +33,7 @@ class ContentDownloadStateTrackerBinder @Inject constructor(private val activeSe private val messageColorProvider: MessageColorProvider, private val errorFormatter: ErrorFormatter) { - private val updateListeners = mutableMapOf() + private val updateListeners = mutableMapOf() fun bind(mxcUrl: String, holder: MessageFileItem.Holder) { @@ -46,6 +49,7 @@ class ContentDownloadStateTrackerBinder @Inject constructor(private val activeSe activeSessionHolder.getSafeActiveSession()?.also { session -> val downloadStateTracker = session.contentDownloadProgressTracker() updateListeners[mxcUrl]?.also { + it.stop() downloadStateTracker.unTrack(mxcUrl, it) } } @@ -72,8 +76,18 @@ private class ContentDownloadUpdater(private val holder: MessageFileItem.Holder, } } - // avoid blink effect when setting icon - private var hasDLResource = false + private var animatedDrawable: AnimatedVectorDrawableCompat? = null + private var animationLoopCallback = object : Animatable2Compat.AnimationCallback() { + override fun onAnimationEnd(drawable: Drawable?) { + animatedDrawable?.start() + } + } + + fun stop() { + animatedDrawable?.unregisterAnimationCallback(animationLoopCallback) + animatedDrawable?.stop() + animatedDrawable = null + } private fun handleIdle() { holder.fileDownloadProgress.progress = 0 @@ -92,19 +106,23 @@ private class ContentDownloadUpdater(private val holder: MessageFileItem.Holder, val percent = 100L * (current.toFloat() / total.toFloat()) holder.fileDownloadProgress.isIndeterminate = false holder.fileDownloadProgress.progress = percent.toInt() - if (!hasDLResource) { - holder.fileImageView.setImageResource(R.drawable.ic_download) - hasDLResource = true + if (animatedDrawable == null) { + animatedDrawable = AnimatedVectorDrawableCompat.create(holder.view.context, R.drawable.ic_download_anim) + holder.fileImageView.setImageDrawable(animatedDrawable) + animatedDrawable?.start() + animatedDrawable?.registerAnimationCallback(animationLoopCallback) } } private fun handleFailure() { + stop() holder.fileDownloadProgress.isIndeterminate = false holder.fileDownloadProgress.progress = 0 holder.fileImageView.setImageResource(R.drawable.ic_close_round) } private fun handleSuccess() { + stop() holder.fileDownloadProgress.isIndeterminate = false holder.fileDownloadProgress.progress = 100 holder.fileImageView.setImageResource(R.drawable.ic_paperclip) diff --git a/vector/src/main/res/drawable/ic_download_anim.xml b/vector/src/main/res/drawable/ic_download_anim.xml new file mode 100644 index 0000000000..5d1b80a16f --- /dev/null +++ b/vector/src/main/res/drawable/ic_download_anim.xml @@ -0,0 +1,102 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +