From 940d8389b5a9e177e2995817ba688dd689025d2b Mon Sep 17 00:00:00 2001 From: arkon Date: Wed, 31 Mar 2021 23:03:42 -0400 Subject: [PATCH] Add QuadStateCheckBox view --- .../tachiyomi/widget/QuadStateCheckBox.kt | 44 +++++++++++++++++++ .../ic_indeterminate_check_box_24dp.xml | 9 ++++ 2 files changed, 53 insertions(+) create mode 100644 app/src/main/java/eu/kanade/tachiyomi/widget/QuadStateCheckBox.kt create mode 100644 app/src/main/res/drawable/ic_indeterminate_check_box_24dp.xml diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/QuadStateCheckBox.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/QuadStateCheckBox.kt new file mode 100644 index 000000000..78b94aa1b --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/QuadStateCheckBox.kt @@ -0,0 +1,44 @@ +package eu.kanade.tachiyomi.widget + +import android.content.Context +import android.graphics.drawable.Drawable +import android.util.AttributeSet +import androidx.annotation.AttrRes +import androidx.appcompat.content.res.AppCompatResources +import androidx.appcompat.widget.AppCompatImageView +import eu.kanade.tachiyomi.R +import eu.kanade.tachiyomi.util.system.getResourceColor + +class QuadStateCheckBox @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : + AppCompatImageView(context, attrs) { + + var state: State = State.UNCHECKED + set(value) { + field = value + updateDrawable() + } + + private fun updateDrawable() { + val drawable = when (state) { + State.UNCHECKED -> tintVector(context, R.drawable.ic_check_box_outline_blank_24dp, R.attr.colorControlNormal) + State.INDETERMINATE -> tintVector(context, R.drawable.ic_indeterminate_check_box_24dp) + State.CHECKED -> tintVector(context, R.drawable.ic_check_box_24dp) + State.INVERSED -> tintVector(context, R.drawable.ic_check_box_x_24dp) + } + + setImageDrawable(drawable) + } + + private fun tintVector(context: Context, resId: Int, @AttrRes colorAttrRes: Int = R.attr.colorAccent): Drawable { + return AppCompatResources.getDrawable(context, resId)!!.apply { + setTint(context.getResourceColor(colorAttrRes)) + } + } + + sealed class State { + object UNCHECKED : State() + object INDETERMINATE : State() + object CHECKED : State() + object INVERSED : State() + } +} diff --git a/app/src/main/res/drawable/ic_indeterminate_check_box_24dp.xml b/app/src/main/res/drawable/ic_indeterminate_check_box_24dp.xml new file mode 100644 index 000000000..fd5f8ceed --- /dev/null +++ b/app/src/main/res/drawable/ic_indeterminate_check_box_24dp.xml @@ -0,0 +1,9 @@ + + +