mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-28 07:50:31 +05:30
improve slider dialog accessability
This commit is contained in:
parent
d8900fd125
commit
51cf08676f
@ -6,6 +6,7 @@ import android.view.LayoutInflater
|
||||
import androidx.preference.Preference
|
||||
import com.github.libretube.R
|
||||
import com.github.libretube.databinding.DialogSliderBinding
|
||||
import com.github.libretube.extensions.round
|
||||
import com.github.libretube.util.PreferenceHelper
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||
|
||||
@ -20,11 +21,33 @@ class SliderPreference(
|
||||
attributeSet
|
||||
) {
|
||||
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() {
|
||||
val defValue = typedArray.getFloat(R.styleable.SliderPreference_defValue, 1.0f)
|
||||
val valueFrom = typedArray.getFloat(R.styleable.SliderPreference_valueFrom, 1.0f)
|
||||
val valueTo = typedArray.getFloat(R.styleable.SliderPreference_valueTo, 10.0f)
|
||||
val stepSize = typedArray.getFloat(R.styleable.SliderPreference_stepSize, 1.0f)
|
||||
@ -34,26 +57,46 @@ class SliderPreference(
|
||||
)
|
||||
|
||||
sliderBinding.slider.apply {
|
||||
value = PreferenceHelper.getString(
|
||||
key,
|
||||
defValue.toString()
|
||||
).toFloat()
|
||||
this.value = prefValue
|
||||
this.valueFrom = valueFrom
|
||||
this.valueTo = valueTo
|
||||
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)
|
||||
.setTitle(title)
|
||||
.setView(sliderBinding.root)
|
||||
.setNegativeButton(R.string.cancel, null)
|
||||
.setPositiveButton(R.string.okay) { _, _ ->
|
||||
PreferenceHelper.putString(
|
||||
key,
|
||||
sliderBinding.slider.value.toString()
|
||||
)
|
||||
prefValue = sliderBinding.slider.value
|
||||
summary = sliderBinding.slider.value.toString()
|
||||
}
|
||||
.show()
|
||||
super.onClick()
|
||||
}
|
||||
|
||||
private fun updateCurrentValueText() {
|
||||
sliderBinding.currentValue.text = sliderBinding.slider.value.round(2).toString()
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:tint="?attr/colorSurface"
|
||||
android:tint="?attr/colorControlNormal"
|
||||
android:viewportWidth="24"
|
||||
android:viewportHeight="24">
|
||||
<path
|
||||
|
10
app/src/main/res/drawable/ic_remove.xml
Normal file
10
app/src/main/res/drawable/ic_remove.xml
Normal 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>
|
@ -1,17 +1,51 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
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
|
||||
android:id="@+id/slider"
|
||||
<TextView
|
||||
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_height="wrap_content"
|
||||
android:layout_marginHorizontal="10dp"
|
||||
android:layout_marginTop="10dp"
|
||||
android:stepSize="0.1"
|
||||
android:value="1"
|
||||
android:valueFrom="0"
|
||||
android:valueTo="5" />
|
||||
android:orientation="horizontal">
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/minus"
|
||||
android:layout_width="wrap_content"
|
||||
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>
|
Loading…
x
Reference in New Issue
Block a user