ReaderProgressIndicator: Set indeterminate as default state (#5605)

Will also set to indeterminate if progress is 0.
This commit is contained in:
Ivan Iskandar 2021-07-28 04:30:35 +07:00 committed by GitHub
parent 6cab2427f5
commit cc55453076
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 36 deletions

View File

@ -8,7 +8,7 @@ import android.view.animation.LinearInterpolator
import android.view.animation.RotateAnimation import android.view.animation.RotateAnimation
import android.widget.FrameLayout import android.widget.FrameLayout
import androidx.annotation.IntRange import androidx.annotation.IntRange
import androidx.core.view.isVisible import androidx.dynamicanimation.animation.DynamicAnimation
import com.google.android.material.progressindicator.CircularProgressIndicator import com.google.android.material.progressindicator.CircularProgressIndicator
/** /**
@ -44,34 +44,71 @@ class ReaderProgressIndicator @JvmOverloads constructor(
layoutParams = LayoutParams(WRAP_CONTENT, WRAP_CONTENT) layoutParams = LayoutParams(WRAP_CONTENT, WRAP_CONTENT)
indicator = CircularProgressIndicator(context) indicator = CircularProgressIndicator(context)
indicator.max = 100 indicator.max = 100
indicator.isIndeterminate = true
addView(indicator) addView(indicator)
} }
override fun onAttachedToWindow() { override fun onAttachedToWindow() {
super.onAttachedToWindow() super.onAttachedToWindow()
if (indicator.isVisible && animation == null) { updateRotateAnimation()
startAnimation(rotateAnimation)
}
} }
override fun onDetachedFromWindow() { override fun onDetachedFromWindow() {
super.onDetachedFromWindow() super.onDetachedFromWindow()
clearAnimation() updateRotateAnimation()
} }
fun show() { fun show() {
indicator.show() indicator.show()
if (animation == null) { updateRotateAnimation()
startAnimation(rotateAnimation)
}
} }
fun hide() { fun hide() {
indicator.hide() indicator.hide()
clearAnimation() updateRotateAnimation()
} }
/**
* Sets the current indicator progress to the specified value.
*
* @param progress Indicator will be set indeterminate if this value is 0
*/
fun setProgress(@IntRange(from = 0, to = 100) progress: Int, animated: Boolean = true) { fun setProgress(@IntRange(from = 0, to = 100) progress: Int, animated: Boolean = true) {
if (progress > 0) {
indicator.setProgressCompat(progress, animated) indicator.setProgressCompat(progress, animated)
} else if (!indicator.isIndeterminate) {
indicator.hide()
indicator.isIndeterminate = true
indicator.show()
}
updateRotateAnimation()
}
fun setCompleteProgressAndHide() {
val listener = object : DynamicAnimation.OnAnimationEndListener {
override fun onAnimationEnd(
animation: DynamicAnimation<*>?,
canceled: Boolean,
value: Float,
velocity: Float
) {
hide()
indicator.progressDrawable?.removeSpringAnimationEndListener(this)
}
}
indicator.progressDrawable?.addSpringAnimationEndListener(listener)
indicator.setProgressCompat(100, true)
updateRotateAnimation(forceRotate = true)
}
private fun updateRotateAnimation(forceRotate: Boolean = false) {
if (forceRotate || (indicator.isShown && !indicator.isIndeterminate)) {
if (animation == null && isAttachedToWindow) {
startAnimation(rotateAnimation)
}
} else {
clearAnimation()
}
} }
} }

View File

@ -245,8 +245,7 @@ class PagerPageHolder(
* Called when the page is ready. * Called when the page is ready.
*/ */
private fun setImage() { private fun setImage() {
progressIndicator.setProgress(100) progressIndicator.setCompleteProgressAndHide()
progressIndicator.hide()
retryButton?.isVisible = false retryButton?.isVisible = false
decodeErrorLayout?.isVisible = false decodeErrorLayout?.isVisible = false
@ -332,13 +331,6 @@ class PagerPageHolder(
initRetryButton().isVisible = true initRetryButton().isVisible = true
} }
/**
* Called when the image is decoded and going to be displayed.
*/
private fun onImageDecoded() {
progressIndicator.hide()
}
/** /**
* Called when an image fails to decode. * Called when an image fails to decode.
*/ */
@ -373,7 +365,6 @@ class PagerPageHolder(
ZoomType.Right -> setScaleAndCenter(scale, PointF(sWidth.toFloat(), 0f)) ZoomType.Right -> setScaleAndCenter(scale, PointF(sWidth.toFloat(), 0f))
ZoomType.Center -> setScaleAndCenter(scale, center.also { it?.y = 0f }) ZoomType.Center -> setScaleAndCenter(scale, center.also { it?.y = 0f })
} }
onImageDecoded()
} }
override fun onImageLoadError(e: Exception) { override fun onImageLoadError(e: Exception) {
@ -504,7 +495,6 @@ class PagerPageHolder(
result.start() result.start()
} }
setImageDrawable(result) setImageDrawable(result)
onImageDecoded()
}, },
onError = { onError = {
onImageDecodeError() onImageDecodeError()

View File

@ -265,9 +265,7 @@ class WebtoonPageHolder(
* Called when the page is ready. * Called when the page is ready.
*/ */
private fun setImage() { private fun setImage() {
progressContainer.isVisible = true progressIndicator.setCompleteProgressAndHide()
progressIndicator.setProgress(100)
progressIndicator.hide()
retryContainer?.isVisible = false retryContainer?.isVisible = false
removeDecodeErrorLayout() removeDecodeErrorLayout()
@ -325,13 +323,6 @@ class WebtoonPageHolder(
initRetryLayout().isVisible = true initRetryLayout().isVisible = true
} }
/**
* Called when the image is decoded and going to be displayed.
*/
private fun onImageDecoded() {
progressContainer.isVisible = false
}
/** /**
* Called when the image fails to decode. * Called when the image fails to decode.
*/ */
@ -382,10 +373,6 @@ class WebtoonPageHolder(
setCropBorders(cropBorders) setCropBorders(cropBorders)
setOnImageEventListener( setOnImageEventListener(
object : SubsamplingScaleImageView.DefaultOnImageEventListener() { object : SubsamplingScaleImageView.DefaultOnImageEventListener() {
override fun onReady() {
onImageDecoded()
}
override fun onImageLoadError(e: Exception) { override fun onImageLoadError(e: Exception) {
onImageDecodeError() onImageDecodeError()
} }
@ -517,7 +504,6 @@ class WebtoonPageHolder(
result.start() result.start()
} }
setImageDrawable(result) setImageDrawable(result)
onImageDecoded()
}, },
onError = { onError = {
onImageDecodeError() onImageDecodeError()