Merge pull request #1413 from Bnyro/master

slider preference cleanup
This commit is contained in:
Bnyro 2022-09-29 20:25:06 +02:00 committed by GitHub
commit c10b0b55ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 48 additions and 73 deletions

View File

@ -1,22 +0,0 @@
package com.github.libretube.constants
import com.github.libretube.obj.SliderRange
/**
* Stores the ranges for the [SliderPreference]
*/
object PreferenceRanges {
val playbackSpeed = SliderRange(
0.25f,
6.0f,
0.25f,
1.0f
)
val seekIncrement = SliderRange(
5f,
60f,
5f,
10f
)
}

View File

@ -1,14 +0,0 @@
package com.github.libretube.extensions
import com.github.libretube.obj.SliderRange
import com.google.android.material.slider.Slider
/**
* set the range of the slider preference
*/
fun Slider.setSliderRangeAndValue(range: SliderRange) {
this.valueFrom = range.valueFrom
this.valueTo = range.valueTo
this.stepSize = range.stepSize
this.value = range.defaultValue
}

View File

@ -1,8 +0,0 @@
package com.github.libretube.obj
data class SliderRange(
val valueFrom: Float,
val valueTo: Float,
val stepSize: Float,
val defaultValue: Float
)

View File

@ -11,11 +11,9 @@ import android.view.View
import androidx.fragment.app.FragmentManager
import com.github.libretube.R
import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.constants.PreferenceRanges
import com.github.libretube.databinding.DialogSliderBinding
import com.github.libretube.databinding.DoubleTapOverlayBinding
import com.github.libretube.databinding.ExoStyledPlayerControlViewBinding
import com.github.libretube.extensions.setSliderRangeAndValue
import com.github.libretube.models.interfaces.DoubleTapInterface
import com.github.libretube.models.interfaces.PlayerOptionsInterface
import com.github.libretube.obj.BottomSheetItem
@ -337,10 +335,12 @@ internal class CustomExoPlayerView(
val playbackSpeedBinding = DialogSliderBinding.inflate(
LayoutInflater.from(context)
)
playbackSpeedBinding.slider.setSliderRangeAndValue(
PreferenceRanges.playbackSpeed
)
playbackSpeedBinding.slider.value = player?.playbackParameters?.speed ?: 1f
playbackSpeedBinding.slider.apply {
valueFrom = 0.25f
valueTo = 4.0f
stepSize = 0.25f
value = player?.playbackParameters?.speed ?: 1f
}
// change playback speed dialog
MaterialAlertDialogBuilder(context)
.setTitle(R.string.change_playback_speed)

View File

@ -5,10 +5,7 @@ import android.util.AttributeSet
import android.view.LayoutInflater
import androidx.preference.Preference
import com.github.libretube.R
import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.constants.PreferenceRanges
import com.github.libretube.databinding.DialogSliderBinding
import com.github.libretube.extensions.setSliderRangeAndValue
import com.github.libretube.util.PreferenceHelper
import com.google.android.material.dialog.MaterialAlertDialogBuilder
@ -24,25 +21,27 @@ class SliderPreference(
) {
private lateinit var sliderBinding: DialogSliderBinding
val typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.SliderPreference)
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)
sliderBinding = DialogSliderBinding.inflate(
LayoutInflater.from(context)
)
val range = when (key) {
PreferenceKeys.PLAYBACK_SPEED -> PreferenceRanges.playbackSpeed
PreferenceKeys.BACKGROUND_PLAYBACK_SPEED -> PreferenceRanges.playbackSpeed
PreferenceKeys.SEEK_INCREMENT -> PreferenceRanges.seekIncrement
else -> null
}
if (range == null) return
sliderBinding.slider.setSliderRangeAndValue(range)
sliderBinding.slider.value = PreferenceHelper.getString(
sliderBinding.slider.apply {
value = PreferenceHelper.getString(
key,
range.defaultValue.toString()
defValue.toString()
).toFloat()
this.valueFrom = valueFrom
this.valueTo = valueTo
this.stepSize = stepSize
}
MaterialAlertDialogBuilder(context)
.setTitle(title)

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="SliderPreference">
<attr name="valueFrom" format="float" />
<attr name="valueTo" format="float" />
<attr name="stepSize" format="float" />
<attr name="defValue" format="float" />
</declare-styleable>
</resources>

View File

@ -78,9 +78,12 @@
<com.github.libretube.ui.views.SliderPreference
android:icon="@drawable/ic_speed"
app:defaultValue="1.0"
app:defValue="1.0"
app:key="background_playback_speed"
app:title="@string/playback_speed" />
app:stepSize="0.25"
app:title="@string/playback_speed"
app:valueFrom="0.25"
app:valueTo="4.0" />
</PreferenceCategory>

View File

@ -6,15 +6,21 @@
<com.github.libretube.ui.views.SliderPreference
android:icon="@drawable/ic_speed"
app:defaultValue="1.0"
app:defValue="1.0"
app:key="playback_speed"
app:title="@string/playback_speed" />
app:stepSize="0.25"
app:title="@string/playback_speed"
app:valueFrom="0.25"
app:valueTo="4.0" />
<com.github.libretube.ui.views.SliderPreference
android:icon="@drawable/ic_skip"
app:defaultValue="5"
app:defValue="10.0"
app:key="seek_increment"
app:title="@string/seek_increment" />
app:stepSize="5.0"
app:title="@string/seek_increment"
app:valueFrom="5.0"
app:valueTo="60.0" />
<ListPreference
android:icon="@drawable/ic_time"