|
|
|
@ -11,8 +11,14 @@ import android.view.animation.AccelerateDecelerateInterpolator
|
|
|
|
import androidx.annotation.AttrRes
|
|
|
|
import androidx.annotation.AttrRes
|
|
|
|
import androidx.annotation.StyleRes
|
|
|
|
import androidx.annotation.StyleRes
|
|
|
|
import org.koitharu.kotatsu.R
|
|
|
|
import org.koitharu.kotatsu.R
|
|
|
|
|
|
|
|
import org.koitharu.kotatsu.core.prefs.ProgressIndicatorMode.CHAPTERS_LEFT
|
|
|
|
|
|
|
|
import org.koitharu.kotatsu.core.prefs.ProgressIndicatorMode.CHAPTERS_READ
|
|
|
|
|
|
|
|
import org.koitharu.kotatsu.core.prefs.ProgressIndicatorMode.NONE
|
|
|
|
|
|
|
|
import org.koitharu.kotatsu.core.prefs.ProgressIndicatorMode.PERCENT_LEFT
|
|
|
|
|
|
|
|
import org.koitharu.kotatsu.core.prefs.ProgressIndicatorMode.PERCENT_READ
|
|
|
|
import org.koitharu.kotatsu.core.util.ext.getAnimationDuration
|
|
|
|
import org.koitharu.kotatsu.core.util.ext.getAnimationDuration
|
|
|
|
import org.koitharu.kotatsu.history.data.PROGRESS_NONE
|
|
|
|
import org.koitharu.kotatsu.history.data.PROGRESS_NONE
|
|
|
|
|
|
|
|
import org.koitharu.kotatsu.list.domain.ReadingProgress
|
|
|
|
|
|
|
|
|
|
|
|
class ReadingProgressView @JvmOverloads constructor(
|
|
|
|
class ReadingProgressView @JvmOverloads constructor(
|
|
|
|
context: Context,
|
|
|
|
context: Context,
|
|
|
|
@ -20,17 +26,30 @@ class ReadingProgressView @JvmOverloads constructor(
|
|
|
|
@AttrRes defStyleAttr: Int = 0,
|
|
|
|
@AttrRes defStyleAttr: Int = 0,
|
|
|
|
) : View(context, attrs, defStyleAttr), ValueAnimator.AnimatorUpdateListener, Animator.AnimatorListener {
|
|
|
|
) : View(context, attrs, defStyleAttr), ValueAnimator.AnimatorUpdateListener, Animator.AnimatorListener {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private val percentPattern = context.getString(R.string.percent_string_pattern)
|
|
|
|
private var percentAnimator: ValueAnimator? = null
|
|
|
|
private var percentAnimator: ValueAnimator? = null
|
|
|
|
private val animationDuration = context.getAnimationDuration(android.R.integer.config_shortAnimTime)
|
|
|
|
private val animationDuration = context.getAnimationDuration(android.R.integer.config_shortAnimTime)
|
|
|
|
|
|
|
|
|
|
|
|
@StyleRes
|
|
|
|
@StyleRes
|
|
|
|
private val drawableStyle: Int
|
|
|
|
private val drawableStyle: Int
|
|
|
|
|
|
|
|
|
|
|
|
var percent: Float
|
|
|
|
var progress: ReadingProgress? = null
|
|
|
|
get() = peekProgressDrawable()?.progress ?: PROGRESS_NONE
|
|
|
|
|
|
|
|
set(value) {
|
|
|
|
set(value) {
|
|
|
|
|
|
|
|
field = value
|
|
|
|
cancelAnimation()
|
|
|
|
cancelAnimation()
|
|
|
|
getProgressDrawable().progress = value
|
|
|
|
getProgressDrawable().also {
|
|
|
|
|
|
|
|
it.percent = value?.percent ?: PROGRESS_NONE
|
|
|
|
|
|
|
|
it.text = when (value?.mode) {
|
|
|
|
|
|
|
|
null,
|
|
|
|
|
|
|
|
NONE -> ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PERCENT_READ -> percentPattern.format((value.percent * 100f).toInt().toString())
|
|
|
|
|
|
|
|
PERCENT_LEFT -> "-" + percentPattern.format((value.percentLeft * 100f).toInt().toString())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CHAPTERS_READ -> value.chapters.toString()
|
|
|
|
|
|
|
|
CHAPTERS_LEFT -> "-" + value.chaptersLeft.toString()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
init {
|
|
|
|
init {
|
|
|
|
@ -39,7 +58,11 @@ class ReadingProgressView @JvmOverloads constructor(
|
|
|
|
ta.recycle()
|
|
|
|
ta.recycle()
|
|
|
|
outlineProvider = OutlineProvider()
|
|
|
|
outlineProvider = OutlineProvider()
|
|
|
|
if (isInEditMode) {
|
|
|
|
if (isInEditMode) {
|
|
|
|
percent = 0.27f
|
|
|
|
progress = ReadingProgress(
|
|
|
|
|
|
|
|
percent = 0.27f,
|
|
|
|
|
|
|
|
totalChapters = 20,
|
|
|
|
|
|
|
|
mode = PERCENT_READ,
|
|
|
|
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@ -53,7 +76,7 @@ class ReadingProgressView @JvmOverloads constructor(
|
|
|
|
|
|
|
|
|
|
|
|
override fun onAnimationUpdate(animation: ValueAnimator) {
|
|
|
|
override fun onAnimationUpdate(animation: ValueAnimator) {
|
|
|
|
val p = animation.animatedValue as Float
|
|
|
|
val p = animation.animatedValue as Float
|
|
|
|
getProgressDrawable().progress = p
|
|
|
|
getProgressDrawable().percent = p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
override fun onAnimationStart(animation: Animator) = Unit
|
|
|
|
override fun onAnimationStart(animation: Animator) = Unit
|
|
|
|
@ -68,16 +91,25 @@ class ReadingProgressView @JvmOverloads constructor(
|
|
|
|
|
|
|
|
|
|
|
|
override fun onAnimationRepeat(animation: Animator) = Unit
|
|
|
|
override fun onAnimationRepeat(animation: Animator) = Unit
|
|
|
|
|
|
|
|
|
|
|
|
fun setPercent(value: Float, animate: Boolean) {
|
|
|
|
fun setProgress(percent: Float, animate: Boolean) {
|
|
|
|
|
|
|
|
setProgress(
|
|
|
|
|
|
|
|
value = ReadingProgress(percent, 1, PERCENT_READ),
|
|
|
|
|
|
|
|
animate = animate,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun setProgress(value: ReadingProgress?, animate: Boolean) {
|
|
|
|
val currentDrawable = peekProgressDrawable()
|
|
|
|
val currentDrawable = peekProgressDrawable()
|
|
|
|
if (!animate || currentDrawable == null || value == PROGRESS_NONE) {
|
|
|
|
if (!animate || currentDrawable == null || value == null) {
|
|
|
|
percent = value
|
|
|
|
progress = value
|
|
|
|
return
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
percentAnimator?.cancel()
|
|
|
|
percentAnimator?.cancel()
|
|
|
|
|
|
|
|
val currentPercent = currentDrawable.percent.coerceAtLeast(0f)
|
|
|
|
|
|
|
|
progress = value.copy(percent = currentPercent)
|
|
|
|
percentAnimator = ValueAnimator.ofFloat(
|
|
|
|
percentAnimator = ValueAnimator.ofFloat(
|
|
|
|
currentDrawable.progress.coerceAtLeast(0f),
|
|
|
|
currentDrawable.percent.coerceAtLeast(0f),
|
|
|
|
value,
|
|
|
|
value.percent,
|
|
|
|
).apply {
|
|
|
|
).apply {
|
|
|
|
duration = animationDuration
|
|
|
|
duration = animationDuration
|
|
|
|
interpolator = AccelerateDecelerateInterpolator()
|
|
|
|
interpolator = AccelerateDecelerateInterpolator()
|
|
|
|
|