From e7d60a5fe9fbbbe88d926e7762a3e3363bb39326 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Thu, 29 Sep 2022 20:24:39 +0200 Subject: [PATCH] slider preference cleanup --- .../libretube/constants/PreferenceRanges.kt | 22 ------------- .../extensions/SetSliderRangeAndValue.kt | 14 -------- .../com/github/libretube/obj/SliderRange.kt | 8 ----- .../libretube/ui/views/CustomExoPlayerView.kt | 12 +++---- .../libretube/ui/views/SliderPreference.kt | 33 +++++++++---------- app/src/main/res/values/attrs.xml | 11 +++++++ app/src/main/res/xml/audio_video_settings.xml | 7 ++-- app/src/main/res/xml/player_settings.xml | 14 +++++--- 8 files changed, 48 insertions(+), 73 deletions(-) delete mode 100644 app/src/main/java/com/github/libretube/constants/PreferenceRanges.kt delete mode 100644 app/src/main/java/com/github/libretube/extensions/SetSliderRangeAndValue.kt delete mode 100644 app/src/main/java/com/github/libretube/obj/SliderRange.kt create mode 100644 app/src/main/res/values/attrs.xml diff --git a/app/src/main/java/com/github/libretube/constants/PreferenceRanges.kt b/app/src/main/java/com/github/libretube/constants/PreferenceRanges.kt deleted file mode 100644 index abffb31d5..000000000 --- a/app/src/main/java/com/github/libretube/constants/PreferenceRanges.kt +++ /dev/null @@ -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 - ) -} diff --git a/app/src/main/java/com/github/libretube/extensions/SetSliderRangeAndValue.kt b/app/src/main/java/com/github/libretube/extensions/SetSliderRangeAndValue.kt deleted file mode 100644 index 2ff773c82..000000000 --- a/app/src/main/java/com/github/libretube/extensions/SetSliderRangeAndValue.kt +++ /dev/null @@ -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 -} diff --git a/app/src/main/java/com/github/libretube/obj/SliderRange.kt b/app/src/main/java/com/github/libretube/obj/SliderRange.kt deleted file mode 100644 index 3802ebecf..000000000 --- a/app/src/main/java/com/github/libretube/obj/SliderRange.kt +++ /dev/null @@ -1,8 +0,0 @@ -package com.github.libretube.obj - -data class SliderRange( - val valueFrom: Float, - val valueTo: Float, - val stepSize: Float, - val defaultValue: Float -) diff --git a/app/src/main/java/com/github/libretube/ui/views/CustomExoPlayerView.kt b/app/src/main/java/com/github/libretube/ui/views/CustomExoPlayerView.kt index a56187838..4bcc4b402 100644 --- a/app/src/main/java/com/github/libretube/ui/views/CustomExoPlayerView.kt +++ b/app/src/main/java/com/github/libretube/ui/views/CustomExoPlayerView.kt @@ -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) diff --git a/app/src/main/java/com/github/libretube/ui/views/SliderPreference.kt b/app/src/main/java/com/github/libretube/ui/views/SliderPreference.kt index c965d04af..239df8dd6 100644 --- a/app/src/main/java/com/github/libretube/ui/views/SliderPreference.kt +++ b/app/src/main/java/com/github/libretube/ui/views/SliderPreference.kt @@ -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,26 +21,28 @@ 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 + + sliderBinding.slider.apply { + value = PreferenceHelper.getString( + key, + defValue.toString() + ).toFloat() + this.valueFrom = valueFrom + this.valueTo = valueTo + this.stepSize = stepSize } - if (range == null) return - - sliderBinding.slider.setSliderRangeAndValue(range) - - sliderBinding.slider.value = PreferenceHelper.getString( - key, - range.defaultValue.toString() - ).toFloat() - MaterialAlertDialogBuilder(context) .setTitle(title) .setView(sliderBinding.root) diff --git a/app/src/main/res/values/attrs.xml b/app/src/main/res/values/attrs.xml new file mode 100644 index 000000000..58159c406 --- /dev/null +++ b/app/src/main/res/values/attrs.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/app/src/main/res/xml/audio_video_settings.xml b/app/src/main/res/xml/audio_video_settings.xml index 80eb31c7c..c93d0afed 100644 --- a/app/src/main/res/xml/audio_video_settings.xml +++ b/app/src/main/res/xml/audio_video_settings.xml @@ -78,9 +78,12 @@ + app:stepSize="0.25" + app:title="@string/playback_speed" + app:valueFrom="0.25" + app:valueTo="4.0" /> diff --git a/app/src/main/res/xml/player_settings.xml b/app/src/main/res/xml/player_settings.xml index 0777d740e..0ad4a27c9 100644 --- a/app/src/main/res/xml/player_settings.xml +++ b/app/src/main/res/xml/player_settings.xml @@ -6,15 +6,21 @@ + app:stepSize="0.25" + app:title="@string/playback_speed" + app:valueFrom="0.25" + app:valueTo="4.0" /> + app:stepSize="5.0" + app:title="@string/seek_increment" + app:valueFrom="5.0" + app:valueTo="60.0" />