mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-01-06 17:40:30 +05:30
Merge pull request #4848 from Bnyro/master
fix: max concurrent download speed preference shows decimals
This commit is contained in:
commit
906842d3e5
@ -9,6 +9,7 @@ import com.github.libretube.databinding.DialogSliderBinding
|
|||||||
import com.github.libretube.extensions.round
|
import com.github.libretube.extensions.round
|
||||||
import com.github.libretube.helpers.PreferenceHelper
|
import com.github.libretube.helpers.PreferenceHelper
|
||||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||||
|
import kotlin.math.roundToInt
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Preference that includes a slider
|
* Preference that includes a slider
|
||||||
@ -22,6 +23,9 @@ class SliderPreference(
|
|||||||
) {
|
) {
|
||||||
private lateinit var sliderBinding: DialogSliderBinding
|
private lateinit var sliderBinding: DialogSliderBinding
|
||||||
private var defValue = 0f
|
private var defValue = 0f
|
||||||
|
private var valueFrom = 1f
|
||||||
|
private var valueTo = 10f
|
||||||
|
private var stepSize = 1f
|
||||||
|
|
||||||
private var prefValue: Float
|
private var prefValue: Float
|
||||||
get() = PreferenceHelper.getString(
|
get() = PreferenceHelper.getString(
|
||||||
@ -43,27 +47,24 @@ class SliderPreference(
|
|||||||
override fun onAttached() {
|
override fun onAttached() {
|
||||||
super.onAttached()
|
super.onAttached()
|
||||||
|
|
||||||
defValue = typedArray.getFloat(R.styleable.SliderPreference_defValue, 1.0f)
|
defValue = typedArray.getFloat(R.styleable.SliderPreference_defValue, defValue)
|
||||||
|
valueFrom = typedArray.getFloat(R.styleable.SliderPreference_valueFrom, valueFrom)
|
||||||
|
valueTo = typedArray.getFloat(R.styleable.SliderPreference_valueTo, valueTo)
|
||||||
|
stepSize = typedArray.getFloat(R.styleable.SliderPreference_stepSize, stepSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getSummary(): CharSequence {
|
override fun getSummary(): CharSequence = getDisplayedCurrentValue(prefValue)
|
||||||
return prefValue.round(2).toString()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onClick() {
|
override fun onClick() {
|
||||||
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)
|
|
||||||
|
|
||||||
sliderBinding = DialogSliderBinding.inflate(
|
sliderBinding = DialogSliderBinding.inflate(
|
||||||
LayoutInflater.from(context)
|
LayoutInflater.from(context)
|
||||||
)
|
)
|
||||||
|
|
||||||
sliderBinding.slider.apply {
|
sliderBinding.slider.apply {
|
||||||
this.value = prefValue
|
value = prefValue
|
||||||
this.valueFrom = valueFrom
|
valueFrom = this@SliderPreference.valueFrom
|
||||||
this.valueTo = valueTo
|
valueTo = this@SliderPreference.valueTo
|
||||||
this.stepSize = stepSize
|
stepSize = this@SliderPreference.stepSize
|
||||||
}
|
}
|
||||||
|
|
||||||
sliderBinding.minus.setOnClickListener {
|
sliderBinding.minus.setOnClickListener {
|
||||||
@ -100,6 +101,15 @@ class SliderPreference(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun updateCurrentValueText() {
|
private fun updateCurrentValueText() {
|
||||||
sliderBinding.currentValue.text = sliderBinding.slider.value.round(2).toString()
|
sliderBinding.currentValue.text = getDisplayedCurrentValue(sliderBinding.slider.value)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getDisplayedCurrentValue(currentValue: Float): String {
|
||||||
|
// if the preference only accepts integer steps, we don't need to show the decimals,
|
||||||
|
// as these decimals are just zero and hence not useful for the user
|
||||||
|
if (valueTo % 1 == 0f && valueFrom % 1 == 0f && stepSize % 1 == 0f) {
|
||||||
|
return currentValue.roundToInt().toString()
|
||||||
|
}
|
||||||
|
return currentValue.round(2).toString()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user