Switch to Material Slider in color filter settings

This commit is contained in:
arkon 2021-10-03 11:58:52 -04:00
parent 4af578e310
commit dc92ffed87
5 changed files with 91 additions and 201 deletions

View File

@ -3,7 +3,6 @@ package eu.kanade.tachiyomi.ui.reader.setting
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.SeekBar
import androidx.annotation.ColorInt
import androidx.core.graphics.alpha
import androidx.core.graphics.blue
@ -15,7 +14,6 @@ import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.databinding.ReaderColorFilterSettingsBinding
import eu.kanade.tachiyomi.ui.reader.ReaderActivity
import eu.kanade.tachiyomi.util.preference.bindToPreference
import eu.kanade.tachiyomi.widget.listener.SimpleSeekBarListener
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.sample
@ -54,13 +52,13 @@ class ReaderColorFilterSettings @JvmOverloads constructor(context: Context, attr
// Set brightness value
binding.txtBrightnessSeekbarValue.text = brightness.toString()
binding.brightnessSeekbar.progress = brightness
binding.sliderBrightness.value = brightness.toFloat()
// Initialize seekBar progress
binding.seekbarColorFilterAlpha.progress = argb[0]
binding.seekbarColorFilterRed.progress = argb[1]
binding.seekbarColorFilterGreen.progress = argb[2]
binding.seekbarColorFilterBlue.progress = argb[3]
binding.sliderColorFilterAlpha.value = argb[0].toFloat()
binding.sliderColorFilterRed.value = argb[1].toFloat()
binding.sliderColorFilterGreen.value = argb[2].toFloat()
binding.sliderColorFilterBlue.value = argb[3].toFloat()
// Set listeners
binding.switchColorFilter.bindToPreference(preferences.colorFilter())
@ -69,66 +67,43 @@ class ReaderColorFilterSettings @JvmOverloads constructor(context: Context, attr
binding.grayscale.bindToPreference(preferences.grayscale())
binding.invertedColors.bindToPreference(preferences.invertedColors())
binding.seekbarColorFilterAlpha.setOnSeekBarChangeListener(
object : SimpleSeekBarListener() {
override fun onProgressChanged(seekBar: SeekBar, value: Int, fromUser: Boolean) {
binding.sliderColorFilterAlpha.addOnChangeListener { _, value, fromUser ->
if (fromUser) {
setColorValue(value, ALPHA_MASK, 24)
setColorValue(value.toInt(), ALPHA_MASK, 24)
}
}
binding.sliderColorFilterRed.addOnChangeListener { _, value, fromUser ->
if (fromUser) {
setColorValue(value.toInt(), RED_MASK, 16)
}
}
binding.sliderColorFilterGreen.addOnChangeListener { _, value, fromUser ->
if (fromUser) {
setColorValue(value.toInt(), GREEN_MASK, 8)
}
}
binding.sliderColorFilterBlue.addOnChangeListener { _, value, fromUser ->
if (fromUser) {
setColorValue(value.toInt(), BLUE_MASK, 0)
}
}
)
binding.seekbarColorFilterRed.setOnSeekBarChangeListener(
object : SimpleSeekBarListener() {
override fun onProgressChanged(seekBar: SeekBar, value: Int, fromUser: Boolean) {
binding.sliderBrightness.addOnChangeListener { _, value, fromUser ->
if (fromUser) {
setColorValue(value, RED_MASK, 16)
preferences.customBrightnessValue().set(value.toInt())
}
}
}
)
binding.seekbarColorFilterGreen.setOnSeekBarChangeListener(
object : SimpleSeekBarListener() {
override fun onProgressChanged(seekBar: SeekBar, value: Int, fromUser: Boolean) {
if (fromUser) {
setColorValue(value, GREEN_MASK, 8)
}
}
}
)
binding.seekbarColorFilterBlue.setOnSeekBarChangeListener(
object : SimpleSeekBarListener() {
override fun onProgressChanged(seekBar: SeekBar, value: Int, fromUser: Boolean) {
if (fromUser) {
setColorValue(value, BLUE_MASK, 0)
}
}
}
)
binding.brightnessSeekbar.setOnSeekBarChangeListener(
object : SimpleSeekBarListener() {
override fun onProgressChanged(seekBar: SeekBar, value: Int, fromUser: Boolean) {
if (fromUser) {
preferences.customBrightnessValue().set(value)
}
}
}
)
}
/**
* Set enabled status of seekBars belonging to color filter
* @param enabled determines if seekBar gets enabled
*/
private fun setColorFilterSeekBar(enabled: Boolean) {
binding.seekbarColorFilterRed.isEnabled = enabled
binding.seekbarColorFilterGreen.isEnabled = enabled
binding.seekbarColorFilterBlue.isEnabled = enabled
binding.seekbarColorFilterAlpha.isEnabled = enabled
binding.sliderColorFilterRed.isEnabled = enabled
binding.sliderColorFilterGreen.isEnabled = enabled
binding.sliderColorFilterBlue.isEnabled = enabled
binding.sliderColorFilterAlpha.isEnabled = enabled
}
/**
@ -136,14 +111,14 @@ class ReaderColorFilterSettings @JvmOverloads constructor(context: Context, attr
* @param enabled value which determines if seekBar gets enabled
*/
private fun setCustomBrightnessSeekBar(enabled: Boolean) {
binding.brightnessSeekbar.isEnabled = enabled
binding.sliderBrightness.isEnabled = enabled
}
/**
* Set the text value's of color filter
* @param color integer containing color information
*/
fun setValues(color: Int): Array<Int> {
private fun setValues(color: Int): Array<Int> {
val alpha = color.alpha
val red = color.red
val green = color.green
@ -214,21 +189,14 @@ class ReaderColorFilterSettings @JvmOverloads constructor(context: Context, attr
* @param mask contains hex mask of chosen color
* @param bitShift amounts of bits that gets shifted to receive value
*/
fun setColorValue(color: Int, mask: Long, bitShift: Int) {
private fun setColorValue(color: Int, mask: Long, bitShift: Int) {
val currentColor = preferences.colorFilterValue().get()
val updatedColor = (color shl bitShift) or (currentColor and mask.inv().toInt())
preferences.colorFilterValue().set(updatedColor)
}
}
/** Integer mask of alpha value **/
private const val ALPHA_MASK: Long = 0xFF000000
/** Integer mask of red value **/
private const val RED_MASK: Long = 0x00FF0000
/** Integer mask of green value **/
private const val GREEN_MASK: Long = 0x0000FF00
/** Integer mask of blue value **/
private const val BLUE_MASK: Long = 0x000000FF

View File

@ -1,74 +0,0 @@
package eu.kanade.tachiyomi.widget
import android.content.Context
import android.os.Parcelable
import android.util.AttributeSet
import android.widget.SeekBar
import androidx.appcompat.widget.AppCompatSeekBar
import eu.kanade.tachiyomi.R
import kotlin.math.abs
class NegativeSeekBar @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
AppCompatSeekBar(context, attrs) {
private var minValue: Int = 0
private var maxValue: Int = 0
private var listener: OnSeekBarChangeListener? = null
init {
val styledAttributes = context.obtainStyledAttributes(
attrs,
R.styleable.NegativeSeekBar,
0,
0
)
try {
setMinSeek(styledAttributes.getInt(R.styleable.NegativeSeekBar_min_seek, 0))
setMaxSeek(styledAttributes.getInt(R.styleable.NegativeSeekBar_max_seek, 0))
} finally {
styledAttributes.recycle()
}
super.setOnSeekBarChangeListener(
object : OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar?, value: Int, fromUser: Boolean) {
listener?.onProgressChanged(seekBar, minValue + value, fromUser)
}
override fun onStartTrackingTouch(p0: SeekBar?) {
listener?.onStartTrackingTouch(p0)
}
override fun onStopTrackingTouch(p0: SeekBar?) {
listener?.onStopTrackingTouch(p0)
}
}
)
}
override fun setProgress(progress: Int) {
super.setProgress(abs(minValue) + progress)
}
fun setMinSeek(minValue: Int) {
this.minValue = minValue
max = (this.maxValue - this.minValue)
}
fun setMaxSeek(maxValue: Int) {
this.maxValue = maxValue
max = (this.maxValue - this.minValue)
}
override fun setOnSeekBarChangeListener(listener: OnSeekBarChangeListener?) {
this.listener = listener
}
override fun onRestoreInstanceState(state: Parcelable?) {
// We can't restore the progress from the saved state because it gets shifted.
val origProgress = progress
super.onRestoreInstanceState(state)
super.setProgress(origProgress)
}
}

View File

@ -1,14 +0,0 @@
package eu.kanade.tachiyomi.widget.listener
import android.widget.SeekBar
open class SimpleSeekBarListener : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar, value: Int, fromUser: Boolean) {
}
override fun onStartTrackingTouch(seekBar: SeekBar) {
}
override fun onStopTrackingTouch(seekBar: SeekBar) {
}
}

View File

@ -31,18 +31,21 @@
android:paddingStart="16dp"
android:text="@string/color_filter_r_value"
android:textAppearance="@style/TextAppearance.Medium.SubHeading"
app:layout_constraintBottom_toBottomOf="@id/seekbar_color_filter_red"
app:layout_constraintBottom_toBottomOf="@id/slider_color_filter_red"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/seekbar_color_filter_red" />
app:layout_constraintTop_toTopOf="@id/slider_color_filter_red" />
<SeekBar
android:id="@+id/seekbar_color_filter_red"
<com.google.android.material.slider.Slider
android:id="@+id/slider_color_filter_red"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:max="255"
android:stepSize="1.0"
android:valueTo="255.0"
android:padding="8dp"
app:labelBehavior="gone"
app:tickVisible="false"
app:layout_constraintEnd_toStartOf="@id/txt_color_filter_red_value"
app:layout_constraintStart_toEndOf="@id/color_filter_symbols_barrier"
app:layout_constraintTop_toBottomOf="@id/switch_color_filter" />
@ -54,9 +57,9 @@
android:layout_alignParentEnd="true"
android:layout_marginEnd="16dp"
android:textAppearance="@style/TextAppearance.Medium.SubHeading"
app:layout_constraintBottom_toBottomOf="@id/seekbar_color_filter_red"
app:layout_constraintBottom_toBottomOf="@id/slider_color_filter_red"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/seekbar_color_filter_red"
app:layout_constraintTop_toTopOf="@id/slider_color_filter_red"
tools:text="255" />
<!-- Green filter -->
@ -68,21 +71,24 @@
android:paddingStart="16dp"
android:text="@string/color_filter_g_value"
android:textAppearance="@style/TextAppearance.Medium.SubHeading"
app:layout_constraintBottom_toBottomOf="@id/seekbar_color_filter_green"
app:layout_constraintBottom_toBottomOf="@id/slider_color_filter_green"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/seekbar_color_filter_green" />
app:layout_constraintTop_toTopOf="@id/slider_color_filter_green" />
<SeekBar
android:id="@+id/seekbar_color_filter_green"
<com.google.android.material.slider.Slider
android:id="@+id/slider_color_filter_green"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:max="255"
android:stepSize="1.0"
android:valueTo="255.0"
android:padding="8dp"
app:labelBehavior="gone"
app:tickVisible="false"
app:layout_constraintEnd_toStartOf="@id/txt_color_filter_green_value"
app:layout_constraintStart_toEndOf="@id/color_filter_symbols_barrier"
app:layout_constraintTop_toBottomOf="@id/seekbar_color_filter_red" />
app:layout_constraintTop_toBottomOf="@id/slider_color_filter_red" />
<TextView
android:id="@+id/txt_color_filter_green_value"
@ -91,9 +97,9 @@
android:layout_alignParentEnd="true"
android:layout_marginEnd="16dp"
android:textAppearance="@style/TextAppearance.Medium.SubHeading"
app:layout_constraintBottom_toBottomOf="@id/seekbar_color_filter_green"
app:layout_constraintBottom_toBottomOf="@id/slider_color_filter_green"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/seekbar_color_filter_green"
app:layout_constraintTop_toTopOf="@id/slider_color_filter_green"
tools:text="255" />
<!-- Blue filter -->
@ -105,21 +111,24 @@
android:paddingStart="16dp"
android:text="@string/color_filter_b_value"
android:textAppearance="@style/TextAppearance.Medium.SubHeading"
app:layout_constraintBottom_toBottomOf="@id/seekbar_color_filter_blue"
app:layout_constraintBottom_toBottomOf="@id/slider_color_filter_blue"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/seekbar_color_filter_blue" />
app:layout_constraintTop_toTopOf="@id/slider_color_filter_blue" />
<SeekBar
android:id="@+id/seekbar_color_filter_blue"
<com.google.android.material.slider.Slider
android:id="@+id/slider_color_filter_blue"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:max="255"
android:stepSize="1.0"
android:valueTo="255.0"
android:padding="8dp"
app:labelBehavior="gone"
app:tickVisible="false"
app:layout_constraintEnd_toStartOf="@id/txt_color_filter_blue_value"
app:layout_constraintStart_toEndOf="@id/color_filter_symbols_barrier"
app:layout_constraintTop_toBottomOf="@id/seekbar_color_filter_green" />
app:layout_constraintTop_toBottomOf="@id/slider_color_filter_green" />
<TextView
android:id="@+id/txt_color_filter_blue_value"
@ -128,9 +137,9 @@
android:layout_alignParentEnd="true"
android:layout_marginEnd="16dp"
android:textAppearance="@style/TextAppearance.Medium.SubHeading"
app:layout_constraintBottom_toBottomOf="@id/seekbar_color_filter_blue"
app:layout_constraintBottom_toBottomOf="@id/slider_color_filter_blue"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/seekbar_color_filter_blue"
app:layout_constraintTop_toTopOf="@id/slider_color_filter_blue"
tools:text="255" />
<!-- Alpha filter -->
@ -142,21 +151,24 @@
android:paddingStart="16dp"
android:text="@string/color_filter_a_value"
android:textAppearance="@style/TextAppearance.Medium.SubHeading"
app:layout_constraintBottom_toBottomOf="@id/seekbar_color_filter_alpha"
app:layout_constraintBottom_toBottomOf="@id/slider_color_filter_alpha"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/seekbar_color_filter_alpha" />
app:layout_constraintTop_toTopOf="@id/slider_color_filter_alpha" />
<SeekBar
android:id="@+id/seekbar_color_filter_alpha"
<com.google.android.material.slider.Slider
android:id="@+id/slider_color_filter_alpha"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:max="255"
android:stepSize="1.0"
android:valueTo="255.0"
android:padding="8dp"
app:labelBehavior="gone"
app:tickVisible="false"
app:layout_constraintEnd_toStartOf="@id/txt_color_filter_alpha_value"
app:layout_constraintStart_toEndOf="@id/color_filter_symbols_barrier"
app:layout_constraintTop_toBottomOf="@id/seekbar_color_filter_blue" />
app:layout_constraintTop_toBottomOf="@id/slider_color_filter_blue" />
<TextView
android:id="@+id/txt_color_filter_alpha_value"
@ -165,9 +177,9 @@
android:layout_alignParentEnd="true"
android:layout_marginEnd="16dp"
android:textAppearance="@style/TextAppearance.Medium.SubHeading"
app:layout_constraintBottom_toBottomOf="@id/seekbar_color_filter_alpha"
app:layout_constraintBottom_toBottomOf="@id/slider_color_filter_alpha"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/seekbar_color_filter_alpha"
app:layout_constraintTop_toTopOf="@id/slider_color_filter_alpha"
tools:text="255" />
<!-- Filter mode -->
@ -178,7 +190,7 @@
android:layout_height="wrap_content"
android:entries="@array/color_filter_modes"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/seekbar_color_filter_alpha"
app:layout_constraintTop_toBottomOf="@id/slider_color_filter_alpha"
app:title="@string/pref_color_filter_mode" />
<!-- Grayscale -->
@ -224,23 +236,26 @@
android:paddingStart="16dp"
android:textAppearance="@style/TextAppearance.Medium.SubHeading"
android:tint="?attr/colorOnBackground"
app:layout_constraintBottom_toBottomOf="@id/brightness_seekbar"
app:layout_constraintBottom_toBottomOf="@id/slider_brightness"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/brightness_seekbar"
app:layout_constraintTop_toTopOf="@id/slider_brightness"
app:srcCompat="@drawable/ic_brightness_5_24dp" />
<eu.kanade.tachiyomi.widget.NegativeSeekBar
android:id="@+id/brightness_seekbar"
<com.google.android.material.slider.Slider
android:id="@+id/slider_brightness"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:padding="8dp"
android:valueFrom="-75.0"
android:valueTo="100.0"
android:stepSize="1.0"
app:labelBehavior="gone"
app:tickVisible="false"
app:layout_constraintEnd_toStartOf="@id/txt_brightness_seekbar_value"
app:layout_constraintStart_toEndOf="@id/txt_brightness_seekbar_icon"
app:layout_constraintTop_toBottomOf="@id/custom_brightness"
app:max_seek="100"
app:min_seek="-75" />
app:layout_constraintTop_toBottomOf="@id/custom_brightness" />
<TextView
android:id="@+id/txt_brightness_seekbar_value"
@ -248,9 +263,9 @@
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:textAppearance="@style/TextAppearance.Medium.SubHeading"
app:layout_constraintBottom_toBottomOf="@id/brightness_seekbar"
app:layout_constraintBottom_toBottomOf="@id/slider_brightness"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/brightness_seekbar"
app:layout_constraintTop_toTopOf="@id/slider_brightness"
tools:text="50" />
<androidx.constraintlayout.widget.Barrier

View File

@ -6,11 +6,6 @@
<attr name="max" format="integer"/>
</declare-styleable>
<declare-styleable name="NegativeSeekBar">
<attr name="min_seek" format="integer"/>
<attr name="max_seek" format="integer"/>
</declare-styleable>
<declare-styleable name="MaterialSpinnerView">
<attr name="title" format="reference|string"/>
<attr name="android:entries"/>