Browse Source

Add QuadStateCheckBox view

arkon 4 years ago
parent
commit
940d8389b5

+ 44 - 0
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()
+    }
+}

+ 9 - 0
app/src/main/res/drawable/ic_indeterminate_check_box_24dp.xml

@@ -0,0 +1,9 @@
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+    android:width="24dp"
+    android:height="24dp"
+    android:viewportWidth="24"
+    android:viewportHeight="24">
+  <path
+      android:fillColor="@android:color/black"
+      android:pathData="M19,3H5C3.9,3 3,3.9 3,5v14c0,1.1 0.9,2 2,2h14c1.1,0 2,-0.9 2,-2V5C21,3.9 20.1,3 19,3zM17,13H7v-2h10V13z"/>
+</vector>