From dc92ffed874c7aad4c266c0a26ee934470b2bc33 Mon Sep 17 00:00:00 2001 From: arkon Date: Sun, 3 Oct 2021 11:58:52 -0400 Subject: [PATCH] Switch to Material Slider in color filter settings --- .../setting/ReaderColorFilterSettings.kt | 102 ++++++------------ .../tachiyomi/widget/NegativeSeekBar.kt | 74 ------------- .../widget/listener/SimpleSeekBarListener.kt | 14 --- .../layout/reader_color_filter_settings.xml | 97 ++++++++++------- app/src/main/res/values/attrs.xml | 5 - 5 files changed, 91 insertions(+), 201 deletions(-) delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/widget/NegativeSeekBar.kt delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/widget/listener/SimpleSeekBarListener.kt diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/ReaderColorFilterSettings.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/ReaderColorFilterSettings.kt index 2537e89ee7..37396e1b07 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/ReaderColorFilterSettings.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/setting/ReaderColorFilterSettings.kt @@ -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,55 +67,32 @@ 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) { - if (fromUser) { - setColorValue(value, ALPHA_MASK, 24) - } - } + binding.sliderColorFilterAlpha.addOnChangeListener { _, value, fromUser -> + if (fromUser) { + 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) { - if (fromUser) { - setColorValue(value, RED_MASK, 16) - } - } + binding.sliderBrightness.addOnChangeListener { _, value, fromUser -> + if (fromUser) { + 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) - } - } - } - ) + } } /** @@ -125,10 +100,10 @@ class ReaderColorFilterSettings @JvmOverloads constructor(context: Context, attr * @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 { + private fun setValues(color: Int): Array { 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 diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/NegativeSeekBar.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/NegativeSeekBar.kt deleted file mode 100644 index 36f5a7a6cd..0000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/widget/NegativeSeekBar.kt +++ /dev/null @@ -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) - } -} diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/listener/SimpleSeekBarListener.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/listener/SimpleSeekBarListener.kt deleted file mode 100644 index 5eafe42d35..0000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/widget/listener/SimpleSeekBarListener.kt +++ /dev/null @@ -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) { - } -} diff --git a/app/src/main/res/layout/reader_color_filter_settings.xml b/app/src/main/res/layout/reader_color_filter_settings.xml index 56ef8c084b..695e856470 100644 --- a/app/src/main/res/layout/reader_color_filter_settings.xml +++ b/app/src/main/res/layout/reader_color_filter_settings.xml @@ -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" /> - @@ -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" /> @@ -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" /> - + app:layout_constraintTop_toBottomOf="@id/slider_color_filter_red" /> @@ -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" /> - + app:layout_constraintTop_toBottomOf="@id/slider_color_filter_green" /> @@ -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" /> - + app:layout_constraintTop_toBottomOf="@id/slider_color_filter_blue" /> @@ -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" /> @@ -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" /> - + app:layout_constraintTop_toBottomOf="@id/custom_brightness" /> - - - - -