Merge pull request #2047 from Bnyro/master

improve slider dialog accessability
This commit is contained in:
Bnyro 2022-11-25 18:36:46 +01:00 committed by GitHub
commit 6419d13b00
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 107 additions and 20 deletions

View File

@ -6,6 +6,7 @@ import android.view.LayoutInflater
import androidx.preference.Preference import androidx.preference.Preference
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.databinding.DialogSliderBinding import com.github.libretube.databinding.DialogSliderBinding
import com.github.libretube.extensions.round
import com.github.libretube.util.PreferenceHelper import com.github.libretube.util.PreferenceHelper
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
@ -20,11 +21,33 @@ class SliderPreference(
attributeSet attributeSet
) { ) {
private lateinit var sliderBinding: DialogSliderBinding private lateinit var sliderBinding: DialogSliderBinding
private var defValue = 0f
val typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.SliderPreference) private var prefValue: Float
get() = PreferenceHelper.getString(
key,
defValue.toString()
).toFloat()
set(value) {
PreferenceHelper.putString(
key,
value.toString()
)
}
private val typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.SliderPreference)
override fun onAttached() {
super.onAttached()
defValue = typedArray.getFloat(R.styleable.SliderPreference_defValue, 1.0f)
}
override fun getSummary(): CharSequence {
return prefValue.toString()
}
override fun onClick() { override fun onClick() {
val defValue = typedArray.getFloat(R.styleable.SliderPreference_defValue, 1.0f)
val valueFrom = typedArray.getFloat(R.styleable.SliderPreference_valueFrom, 1.0f) val valueFrom = typedArray.getFloat(R.styleable.SliderPreference_valueFrom, 1.0f)
val valueTo = typedArray.getFloat(R.styleable.SliderPreference_valueTo, 10.0f) val valueTo = typedArray.getFloat(R.styleable.SliderPreference_valueTo, 10.0f)
val stepSize = typedArray.getFloat(R.styleable.SliderPreference_stepSize, 1.0f) val stepSize = typedArray.getFloat(R.styleable.SliderPreference_stepSize, 1.0f)
@ -34,26 +57,46 @@ class SliderPreference(
) )
sliderBinding.slider.apply { sliderBinding.slider.apply {
value = PreferenceHelper.getString( this.value = prefValue
key,
defValue.toString()
).toFloat()
this.valueFrom = valueFrom this.valueFrom = valueFrom
this.valueTo = valueTo this.valueTo = valueTo
this.stepSize = stepSize this.stepSize = stepSize
} }
sliderBinding.minus.setOnClickListener {
sliderBinding.slider.value = maxOf(valueFrom, sliderBinding.slider.value - stepSize)
}
sliderBinding.plus.setOnClickListener {
sliderBinding.slider.value = minOf(valueTo, sliderBinding.slider.value + stepSize)
}
sliderBinding.slider.addOnChangeListener { slider, _, _ ->
listOf(sliderBinding.minus, sliderBinding.plus).forEach {
it.alpha = 1f
}
when (slider.value) {
slider.valueFrom -> sliderBinding.minus.alpha = 0.5f
slider.valueTo -> sliderBinding.plus.alpha = 0.5f
}
updateCurrentValueText()
}
updateCurrentValueText()
MaterialAlertDialogBuilder(context) MaterialAlertDialogBuilder(context)
.setTitle(title) .setTitle(title)
.setView(sliderBinding.root) .setView(sliderBinding.root)
.setNegativeButton(R.string.cancel, null) .setNegativeButton(R.string.cancel, null)
.setPositiveButton(R.string.okay) { _, _ -> .setPositiveButton(R.string.okay) { _, _ ->
PreferenceHelper.putString( prefValue = sliderBinding.slider.value
key, summary = sliderBinding.slider.value.toString()
sliderBinding.slider.value.toString()
)
} }
.show() .show()
super.onClick() super.onClick()
} }
private fun updateCurrentValueText() {
sliderBinding.currentValue.text = sliderBinding.slider.value.round(2).toString()
}
} }

View File

@ -1,7 +1,7 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" <vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp" android:width="24dp"
android:height="24dp" android:height="24dp"
android:tint="?attr/colorSurface" android:tint="?attr/colorControlNormal"
android:viewportWidth="24" android:viewportWidth="24"
android:viewportHeight="24"> android:viewportHeight="24">
<path <path

View File

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="?attr/colorControlNormal"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M19,13H5v-2h14v2z" />
</vector>

View File

@ -1,17 +1,51 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingHorizontal="10dp"
android:paddingVertical="10dp">
<com.google.android.material.slider.Slider <TextView
android:id="@+id/slider" android:id="@+id/currentValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginVertical="10dp"
android:textSize="18sp" />
<LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp" android:orientation="horizontal">
android:layout_marginTop="10dp"
android:stepSize="0.1" <ImageView
android:value="1" android:id="@+id/minus"
android:valueFrom="0" android:layout_width="wrap_content"
android:valueTo="5" /> android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="@drawable/ic_remove" />
<com.google.android.material.slider.Slider
android:id="@+id/slider"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:stepSize="0.1"
android:value="1"
android:valueFrom="0"
android:valueTo="5" />
<ImageView
android:id="@+id/plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="?attr/selectableItemBackgroundBorderless"
android:src="@drawable/ic_add" />
</LinearLayout>
</LinearLayout> </LinearLayout>