add custom slider preference

This commit is contained in:
Bnyro 2022-08-25 23:05:04 +02:00
parent 207fe64b58
commit fbc6a14c58
6 changed files with 52 additions and 41 deletions

View File

@ -0,0 +1,44 @@
package com.github.libretube.extensions
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import androidx.preference.Preference
import com.github.libretube.R
import com.github.libretube.databinding.DialogSliderBinding
import com.github.libretube.preferences.PreferenceHelper
import com.google.android.material.dialog.MaterialAlertDialogBuilder
/**
* Preference that includes a slider
*/
class SliderPreference(
context: Context,
attributeSet: AttributeSet
) : Preference(
context,
attributeSet
) {
override fun onClick() {
val sliderBinding = DialogSliderBinding.inflate(
LayoutInflater.from(context)
)
sliderBinding.slider.value = PreferenceHelper.getString(
key,
"1.0"
).toFloat()
MaterialAlertDialogBuilder(context)
.setTitle(title)
.setView(sliderBinding.root)
.setNegativeButton(R.string.cancel, null)
.setPositiveButton(R.string.okay) { _, _ ->
PreferenceHelper.putString(
key,
sliderBinding.slider.value.toString()
)
}
.show()
super.onClick()
}
}

View File

@ -43,7 +43,7 @@ import com.github.libretube.adapters.TrendingAdapter
import com.github.libretube.api.CronetHelper
import com.github.libretube.api.RetrofitInstance
import com.github.libretube.api.SubscriptionHelper
import com.github.libretube.databinding.DialogPlaybackSpeedBinding
import com.github.libretube.databinding.DialogSliderBinding
import com.github.libretube.databinding.DoubleTapOverlayBinding
import com.github.libretube.databinding.ExoStyledPlayerControlViewBinding
import com.github.libretube.databinding.FragmentPlayerBinding
@ -510,7 +510,7 @@ class PlayerFragment : BaseFragment() {
}
override fun onPlaybackSpeedClicked() {
val playbackSpeedBinding = DialogPlaybackSpeedBinding.inflate(layoutInflater)
val playbackSpeedBinding = DialogSliderBinding.inflate(layoutInflater)
playbackSpeedBinding.slider.value = exoPlayer.playbackParameters.speed
// change playback speed dialog
MaterialAlertDialogBuilder(requireContext())
@ -599,11 +599,7 @@ class PlayerFragment : BaseFragment() {
context.getString(R.string.none)
}
// set the playback speed
val playbackSpeeds = context.resources.getStringArray(R.array.playbackSpeed)
val playbackSpeedValues =
context.resources.getStringArray(R.array.playbackSpeedValues)
val playbackSpeed = exoPlayer.playbackParameters.speed.toString()
currentPlaybackSpeed = playbackSpeeds[playbackSpeedValues.indexOf(playbackSpeed)]
currentPlaybackSpeed = "${exoPlayer.playbackParameters.speed}x"
// set the quality text
val isAdaptive = exoPlayer.videoFormat?.codecs != null
val quality = exoPlayer.videoSize.height

View File

@ -32,7 +32,7 @@ object PreferenceHelper {
}
fun putString(key: String?, value: String) {
editor.putString(key, value)
editor.putString(key, value).commit()
}
fun getString(key: String?, defValue: String?): String {

View File

@ -641,29 +641,6 @@
<item>.mkv</item>
</string-array>
<string-array name="playbackSpeed">
<item>0.25x</item>
<item>0.5x</item>
<item>0.75x</item>
<item>1x</item>
<item>1.25x</item>
<item>1.5x</item>
<item>2x</item>
<item>3x</item>
<item>4x</item>
</string-array>
<string-array name="playbackSpeedValues">
<item>0.25</item>
<item>0.5</item>
<item>0.75</item>
<item>1.0</item>
<item>1.25</item>
<item>1.5</item>
<item>2.0</item>
<item>3.0</item>
<item>4.0</item>
</string-array>
<string-array name="downloadLocation">
<item>@string/downloads_directory</item>
<item>@string/music_directory</item>

View File

@ -44,14 +44,11 @@
<PreferenceCategory app:title="@string/player">
<ListPreference
<com.github.libretube.extensions.SliderPreference
android:icon="@drawable/ic_speed"
app:defaultValue="1.0"
app:entries="@array/playbackSpeed"
app:entryValues="@array/playbackSpeedValues"
app:key="playback_speed"
app:title="@string/playback_speed"
app:useSimpleSummaryProvider="true" />
app:title="@string/playback_speed" />
<ListPreference
android:icon="@drawable/ic_skip"
@ -137,14 +134,11 @@
<PreferenceCategory app:title="@string/background_mode">
<ListPreference
<com.github.libretube.extensions.SliderPreference
android:icon="@drawable/ic_speed"
app:defaultValue="1.0"
app:entries="@array/playbackSpeed"
app:entryValues="@array/playbackSpeedValues"
app:key="background_playback_speed"
app:title="@string/playback_speed"
app:useSimpleSummaryProvider="true" />
app:title="@string/playback_speed" />
</PreferenceCategory>