|
|
|
|
@ -1,17 +1,18 @@
|
|
|
|
|
package org.koitharu.kotatsu.history.ui.util
|
|
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
|
import android.content.res.ColorStateList
|
|
|
|
|
import android.graphics.Canvas
|
|
|
|
|
import android.graphics.Color
|
|
|
|
|
import android.graphics.ColorFilter
|
|
|
|
|
import android.graphics.Paint
|
|
|
|
|
import android.graphics.PixelFormat
|
|
|
|
|
import android.graphics.Rect
|
|
|
|
|
import android.graphics.drawable.Drawable
|
|
|
|
|
import android.os.Build
|
|
|
|
|
import androidx.annotation.RequiresApi
|
|
|
|
|
import androidx.annotation.StyleRes
|
|
|
|
|
import androidx.appcompat.content.res.AppCompatResources
|
|
|
|
|
import androidx.core.graphics.ColorUtils
|
|
|
|
|
import org.koitharu.kotatsu.R
|
|
|
|
|
import org.koitharu.kotatsu.core.ui.image.PaintDrawable
|
|
|
|
|
import org.koitharu.kotatsu.core.util.ext.hasFocusStateSpecified
|
|
|
|
|
import org.koitharu.kotatsu.core.util.ext.scale
|
|
|
|
|
import org.koitharu.kotatsu.list.domain.ReadingProgress
|
|
|
|
|
import org.koitharu.kotatsu.list.domain.ReadingProgress.Companion.PROGRESS_NONE
|
|
|
|
|
@ -19,23 +20,29 @@ import org.koitharu.kotatsu.list.domain.ReadingProgress.Companion.PROGRESS_NONE
|
|
|
|
|
class ReadingProgressDrawable(
|
|
|
|
|
context: Context,
|
|
|
|
|
@StyleRes styleResId: Int,
|
|
|
|
|
) : Drawable() {
|
|
|
|
|
) : PaintDrawable() {
|
|
|
|
|
|
|
|
|
|
private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
|
|
|
|
|
override val paint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.SUBPIXEL_TEXT_FLAG)
|
|
|
|
|
private val checkDrawable = AppCompatResources.getDrawable(context, R.drawable.ic_check)
|
|
|
|
|
private val lineColor: Int
|
|
|
|
|
private val outlineColor: Int
|
|
|
|
|
private val backgroundColor: Int
|
|
|
|
|
private val textColor: Int
|
|
|
|
|
private val lineColor: ColorStateList
|
|
|
|
|
private val outlineColor: ColorStateList
|
|
|
|
|
private val backgroundColor: ColorStateList
|
|
|
|
|
private val textColor: ColorStateList
|
|
|
|
|
private val textBounds = Rect()
|
|
|
|
|
private val tempRect = Rect()
|
|
|
|
|
private val hasBackground: Boolean
|
|
|
|
|
private val hasOutline: Boolean
|
|
|
|
|
private val hasText: Boolean
|
|
|
|
|
private val desiredHeight: Int
|
|
|
|
|
private val desiredWidth: Int
|
|
|
|
|
private val autoFitTextSize: Boolean
|
|
|
|
|
|
|
|
|
|
private var currentLineColor: Int = Color.TRANSPARENT
|
|
|
|
|
private var currentOutlineColor: Int = Color.TRANSPARENT
|
|
|
|
|
private var currentBackgroundColor: Int = Color.TRANSPARENT
|
|
|
|
|
private var currentTextColor: Int = Color.TRANSPARENT
|
|
|
|
|
private var hasBackground: Boolean = false
|
|
|
|
|
private var hasOutline: Boolean = false
|
|
|
|
|
private var hasText: Boolean = false
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var percent: Float = PROGRESS_NONE
|
|
|
|
|
set(value) {
|
|
|
|
|
field = value
|
|
|
|
|
@ -54,22 +61,21 @@ class ReadingProgressDrawable(
|
|
|
|
|
desiredHeight = ta.getDimensionPixelSize(R.styleable.ProgressDrawable_android_height, -1)
|
|
|
|
|
desiredWidth = ta.getDimensionPixelSize(R.styleable.ProgressDrawable_android_width, -1)
|
|
|
|
|
autoFitTextSize = ta.getBoolean(R.styleable.ProgressDrawable_autoFitTextSize, false)
|
|
|
|
|
lineColor = ta.getColor(R.styleable.ProgressDrawable_android_strokeColor, Color.BLACK)
|
|
|
|
|
outlineColor = ta.getColor(R.styleable.ProgressDrawable_outlineColor, Color.TRANSPARENT)
|
|
|
|
|
backgroundColor = ColorUtils.setAlphaComponent(
|
|
|
|
|
ta.getColor(R.styleable.ProgressDrawable_android_fillColor, Color.TRANSPARENT),
|
|
|
|
|
(255 * ta.getFloat(R.styleable.ProgressDrawable_android_fillAlpha, 0f)).toInt(),
|
|
|
|
|
lineColor = ta.getColorStateList(R.styleable.ProgressDrawable_android_strokeColor) ?: ColorStateList.valueOf(
|
|
|
|
|
Color.BLACK,
|
|
|
|
|
)
|
|
|
|
|
textColor = ta.getColor(R.styleable.ProgressDrawable_android_textColor, lineColor)
|
|
|
|
|
outlineColor =
|
|
|
|
|
ta.getColorStateList(R.styleable.ProgressDrawable_outlineColor) ?: ColorStateList.valueOf(Color.TRANSPARENT)
|
|
|
|
|
backgroundColor = ta.getColorStateList(R.styleable.ProgressDrawable_android_fillColor)?.withAlpha(
|
|
|
|
|
(255 * ta.getFloat(R.styleable.ProgressDrawable_android_fillAlpha, 0f)).toInt(),
|
|
|
|
|
) ?: ColorStateList.valueOf(Color.TRANSPARENT)
|
|
|
|
|
textColor = ta.getColorStateList(R.styleable.ProgressDrawable_android_textColor) ?: lineColor
|
|
|
|
|
paint.strokeCap = Paint.Cap.ROUND
|
|
|
|
|
paint.textAlign = Paint.Align.CENTER
|
|
|
|
|
paint.textSize = ta.getDimension(R.styleable.ProgressDrawable_android_textSize, paint.textSize)
|
|
|
|
|
paint.strokeWidth = ta.getDimension(R.styleable.ProgressDrawable_strokeWidth, 1f)
|
|
|
|
|
ta.recycle()
|
|
|
|
|
hasBackground = Color.alpha(backgroundColor) != 0
|
|
|
|
|
hasOutline = Color.alpha(outlineColor) != 0
|
|
|
|
|
hasText = Color.alpha(textColor) != 0 && paint.textSize > 0
|
|
|
|
|
checkDrawable?.setTint(textColor)
|
|
|
|
|
checkDrawable?.setTintList(textColor)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onBoundsChange(bounds: Rect) {
|
|
|
|
|
@ -91,16 +97,16 @@ class ReadingProgressDrawable(
|
|
|
|
|
val radius = minOf(bounds.width(), bounds.height()) / 2f
|
|
|
|
|
if (hasBackground) {
|
|
|
|
|
paint.style = Paint.Style.FILL
|
|
|
|
|
paint.color = backgroundColor
|
|
|
|
|
paint.color = currentBackgroundColor
|
|
|
|
|
canvas.drawCircle(cx, cy, radius, paint)
|
|
|
|
|
}
|
|
|
|
|
val innerRadius = radius - paint.strokeWidth / 2f
|
|
|
|
|
paint.style = Paint.Style.STROKE
|
|
|
|
|
if (hasOutline) {
|
|
|
|
|
paint.color = outlineColor
|
|
|
|
|
paint.color = currentOutlineColor
|
|
|
|
|
canvas.drawCircle(cx, cy, innerRadius, paint)
|
|
|
|
|
}
|
|
|
|
|
paint.color = lineColor
|
|
|
|
|
paint.color = currentLineColor
|
|
|
|
|
canvas.drawArc(
|
|
|
|
|
cx - innerRadius,
|
|
|
|
|
cy - innerRadius,
|
|
|
|
|
@ -119,28 +125,48 @@ class ReadingProgressDrawable(
|
|
|
|
|
checkDrawable.draw(canvas)
|
|
|
|
|
} else {
|
|
|
|
|
paint.style = Paint.Style.FILL
|
|
|
|
|
paint.color = textColor
|
|
|
|
|
paint.color = currentTextColor
|
|
|
|
|
val ty = bounds.height() / 2f + textBounds.height() / 2f - textBounds.bottom
|
|
|
|
|
canvas.drawText(text, cx, ty, paint)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun setAlpha(alpha: Int) {
|
|
|
|
|
paint.alpha = alpha
|
|
|
|
|
}
|
|
|
|
|
override fun getIntrinsicHeight() = desiredHeight
|
|
|
|
|
|
|
|
|
|
override fun setColorFilter(colorFilter: ColorFilter?) {
|
|
|
|
|
paint.colorFilter = colorFilter
|
|
|
|
|
}
|
|
|
|
|
override fun getIntrinsicWidth() = desiredWidth
|
|
|
|
|
|
|
|
|
|
@Suppress("DeprecatedCallableAddReplaceWith")
|
|
|
|
|
@Deprecated("Deprecated in Java")
|
|
|
|
|
override fun getOpacity() = PixelFormat.TRANSLUCENT
|
|
|
|
|
override fun isStateful(): Boolean = lineColor.isStateful ||
|
|
|
|
|
outlineColor.isStateful ||
|
|
|
|
|
backgroundColor.isStateful ||
|
|
|
|
|
textColor.isStateful ||
|
|
|
|
|
checkDrawable?.isStateful == true
|
|
|
|
|
|
|
|
|
|
override fun getIntrinsicHeight() = desiredHeight
|
|
|
|
|
@RequiresApi(Build.VERSION_CODES.S)
|
|
|
|
|
override fun hasFocusStateSpecified(): Boolean = lineColor.hasFocusStateSpecified() ||
|
|
|
|
|
outlineColor.hasFocusStateSpecified() ||
|
|
|
|
|
backgroundColor.hasFocusStateSpecified() ||
|
|
|
|
|
textColor.hasFocusStateSpecified() ||
|
|
|
|
|
checkDrawable?.hasFocusStateSpecified() == true
|
|
|
|
|
|
|
|
|
|
override fun getIntrinsicWidth() = desiredWidth
|
|
|
|
|
override fun onStateChange(state: IntArray): Boolean {
|
|
|
|
|
val prevLineColor = currentLineColor
|
|
|
|
|
currentLineColor = lineColor.getColorForState(state, lineColor.defaultColor)
|
|
|
|
|
val prevOutlineColor = currentOutlineColor
|
|
|
|
|
currentOutlineColor = outlineColor.getColorForState(state, outlineColor.defaultColor)
|
|
|
|
|
val prevBackgroundColor = currentBackgroundColor
|
|
|
|
|
currentBackgroundColor = backgroundColor.getColorForState(state, backgroundColor.defaultColor)
|
|
|
|
|
val prevTextColor = currentTextColor
|
|
|
|
|
currentTextColor = textColor.getColorForState(state, textColor.defaultColor)
|
|
|
|
|
hasBackground = Color.alpha(currentBackgroundColor) != 0
|
|
|
|
|
hasOutline = Color.alpha(currentOutlineColor) != 0
|
|
|
|
|
hasText = Color.alpha(currentTextColor) != 0 && paint.textSize > 0
|
|
|
|
|
return checkDrawable?.setState(state) == true ||
|
|
|
|
|
prevLineColor != currentLineColor ||
|
|
|
|
|
prevOutlineColor != currentOutlineColor ||
|
|
|
|
|
prevBackgroundColor != currentBackgroundColor ||
|
|
|
|
|
prevTextColor != currentTextColor
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun getTextSizeForWidth(width: Float, text: String): Float {
|
|
|
|
|
val testTextSize = 48f
|
|
|
|
|
|