From ae01341bfa3375222ff2e8f0d84fbb5ec1b1c88c Mon Sep 17 00:00:00 2001 From: Bnyro Date: Tue, 17 Sep 2024 17:23:28 +0200 Subject: [PATCH] feat: allow user to set custom preloading amount --- .../com/github/libretube/helpers/PlayerHelper.kt | 3 ++- .../libretube/ui/base/BasePreferenceFragment.kt | 13 +++++++++++++ .../ui/preferences/EditNumberPreference.kt | 12 ++++++++++++ app/src/main/res/values/array.xml | 15 --------------- app/src/main/res/xml/player_settings.xml | 4 +--- 5 files changed, 28 insertions(+), 19 deletions(-) create mode 100644 app/src/main/java/com/github/libretube/ui/preferences/EditNumberPreference.kt diff --git a/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt b/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt index 22ff62a93f..e27ef87b12 100644 --- a/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/PlayerHelper.kt @@ -53,6 +53,7 @@ import kotlinx.coroutines.runBlocking import java.util.Locale import java.util.concurrent.Executors import kotlin.math.absoluteValue +import kotlin.math.max import kotlin.math.roundToInt object PlayerHelper { @@ -543,7 +544,7 @@ object PlayerHelper { .setBackBuffer(1000 * 60 * 3, true) .setBufferDurationsMs( MINIMUM_BUFFER_DURATION, - bufferingGoal, + max(bufferingGoal, MINIMUM_BUFFER_DURATION), DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_MS, DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_AFTER_REBUFFER_MS ) diff --git a/app/src/main/java/com/github/libretube/ui/base/BasePreferenceFragment.kt b/app/src/main/java/com/github/libretube/ui/base/BasePreferenceFragment.kt index 0a579e0dd5..c6663453d0 100644 --- a/app/src/main/java/com/github/libretube/ui/base/BasePreferenceFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/base/BasePreferenceFragment.kt @@ -1,5 +1,7 @@ package com.github.libretube.ui.base +import android.text.InputType +import android.widget.Toast import androidx.preference.EditTextPreference import androidx.preference.ListPreference import androidx.preference.MultiSelectListPreference @@ -8,6 +10,7 @@ import androidx.preference.PreferenceFragmentCompat import com.github.libretube.R import com.github.libretube.databinding.DialogTextPreferenceBinding import com.github.libretube.ui.activities.SettingsActivity +import com.github.libretube.ui.preferences.EditNumberPreference import com.google.android.material.dialog.MaterialAlertDialogBuilder /** @@ -76,11 +79,21 @@ abstract class BasePreferenceFragment : PreferenceFragmentCompat() { is EditTextPreference -> { val binding = DialogTextPreferenceBinding.inflate(layoutInflater) binding.input.setText(preference.text) + + if (preference is EditNumberPreference) { + binding.input.inputType = InputType.TYPE_NUMBER_FLAG_SIGNED + } + MaterialAlertDialogBuilder(requireContext()) .setTitle(preference.title) .setView(binding.root) .setPositiveButton(android.R.string.ok) { _, _ -> val newValue = binding.input.text.toString() + if (preference is EditNumberPreference && newValue.any { !it.isDigit() }) { + Toast.makeText(context, R.string.invalid_input, Toast.LENGTH_LONG).show() + return@setPositiveButton + } + if (preference.callChangeListener(newValue)) { preference.text = newValue } diff --git a/app/src/main/java/com/github/libretube/ui/preferences/EditNumberPreference.kt b/app/src/main/java/com/github/libretube/ui/preferences/EditNumberPreference.kt new file mode 100644 index 0000000000..b0cf3d9a0a --- /dev/null +++ b/app/src/main/java/com/github/libretube/ui/preferences/EditNumberPreference.kt @@ -0,0 +1,12 @@ +package com.github.libretube.ui.preferences + +import android.content.Context +import android.util.AttributeSet +import androidx.preference.EditTextPreference +import com.github.libretube.ui.base.BasePreferenceFragment + +/** + * [EditTextPreference] that only allows numeric input. + * The actual functionality is done in [BasePreferenceFragment]. + */ +class EditNumberPreference(context: Context, attributeSet: AttributeSet?): EditTextPreference(context, attributeSet) \ No newline at end of file diff --git a/app/src/main/res/values/array.xml b/app/src/main/res/values/array.xml index 686e0782cc..2a2e7d51ee 100644 --- a/app/src/main/res/values/array.xml +++ b/app/src/main/res/values/array.xml @@ -200,21 +200,6 @@ 6 - - 50s - 100s - 200s - 300s - 450s - - - 50 - 100 - 200 - 300 - 450 - - WEBM MPEG_4 diff --git a/app/src/main/res/xml/player_settings.xml b/app/src/main/res/xml/player_settings.xml index 1702218753..1ef1eae187 100644 --- a/app/src/main/res/xml/player_settings.xml +++ b/app/src/main/res/xml/player_settings.xml @@ -203,11 +203,9 @@ -