diff --git a/app/src/main/java/dev/brahmkshatriya/echo/di/AppModule.kt b/app/src/main/java/dev/brahmkshatriya/echo/di/AppModule.kt index 5f7e018b..a1ba91eb 100644 --- a/app/src/main/java/dev/brahmkshatriya/echo/di/AppModule.kt +++ b/app/src/main/java/dev/brahmkshatriya/echo/di/AppModule.kt @@ -18,6 +18,7 @@ import dev.brahmkshatriya.echo.db.models.UserEntity import dev.brahmkshatriya.echo.playback.Current import dev.brahmkshatriya.echo.playback.FFTAudioProcessor import dev.brahmkshatriya.echo.playback.Radio +import dev.brahmkshatriya.echo.ui.settings.AudioFragment.AudioPreference.Companion.CACHE_SIZE import dev.brahmkshatriya.echo.viewmodels.SnackBar import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableStateFlow @@ -55,11 +56,12 @@ class AppModule { @Provides @Singleton @UnstableApi - fun provideCache(application: Application): SimpleCache { + fun provideCache(application: Application, settings: SharedPreferences): SimpleCache { val databaseProvider = StandaloneDatabaseProvider(application) + val cacheSize = settings.getInt(CACHE_SIZE, 200) return SimpleCache( File(application.cacheDir, "exoplayer"), - LeastRecentlyUsedCacheEvictor(100 * 1024 * 1024L), + LeastRecentlyUsedCacheEvictor(cacheSize * 1024 * 1024L), databaseProvider ) } diff --git a/app/src/main/java/dev/brahmkshatriya/echo/ui/settings/AudioFragment.kt b/app/src/main/java/dev/brahmkshatriya/echo/ui/settings/AudioFragment.kt index 9269ca03..80e6eaf9 100644 --- a/app/src/main/java/dev/brahmkshatriya/echo/ui/settings/AudioFragment.kt +++ b/app/src/main/java/dev/brahmkshatriya/echo/ui/settings/AudioFragment.kt @@ -13,6 +13,7 @@ import androidx.preference.SwitchPreferenceCompat import dev.brahmkshatriya.echo.R import dev.brahmkshatriya.echo.common.models.Streamable import dev.brahmkshatriya.echo.utils.prefs.MaterialListPreference +import dev.brahmkshatriya.echo.utils.prefs.MaterialSliderPreference class AudioFragment : BaseSettingsFragment() { override val title get() = getString(R.string.audio) @@ -106,6 +107,15 @@ class AudioFragment : BaseSettingsFragment() { addPreference(this) } + MaterialSliderPreference(context, 100, 1000, 100).apply { + key = CACHE_SIZE + title = getString(R.string.cache_size) + summary = getString(R.string.cache_size_summary) + isIconSpaceReserved = false + setDefaultValue(200) + addPreference(this) + } + Preference(context).apply { key = EQUALIZER title = getString(R.string.equalizer) @@ -139,6 +149,7 @@ class AudioFragment : BaseSettingsFragment() { const val AUDIO_STREAM_QUALITY = "stream_quality" const val VIDEO_STREAM_QUALITY = "video_stream_quality" + const val CACHE_SIZE = "cache_size" val streamQualities = arrayOf("highest", "medium", "lowest") val videoQualities = arrayOf("highest", "medium", "lowest", "none") diff --git a/app/src/main/java/dev/brahmkshatriya/echo/utils/prefs/MaterialSliderPreference.kt b/app/src/main/java/dev/brahmkshatriya/echo/utils/prefs/MaterialSliderPreference.kt new file mode 100644 index 00000000..d44b1663 --- /dev/null +++ b/app/src/main/java/dev/brahmkshatriya/echo/utils/prefs/MaterialSliderPreference.kt @@ -0,0 +1,48 @@ +package dev.brahmkshatriya.echo.utils.prefs + +import android.content.Context +import androidx.preference.Preference +import androidx.preference.PreferenceViewHolder +import com.google.android.material.slider.Slider +import dev.brahmkshatriya.echo.R + +class MaterialSliderPreference( + context: Context, + private val from: Int, + private val to: Int, + private val steps: Int? = null +) : + Preference(context) { + init { + layoutResource = R.layout.preference_slider + } + + private var customSummary: CharSequence? = null + private var defaultValue: Int? = null + override fun onSetInitialValue(defaultValue: Any?) { + customSummary = summary + this.defaultValue = defaultValue as? Int + updateSummary() + } + + override fun onBindViewHolder(holder: PreferenceViewHolder) { + super.onBindViewHolder(holder) + val slider = holder.itemView.findViewById(R.id.preferences_slider) + slider.valueFrom = from.toFloat() + slider.valueTo = to.toFloat() + steps?.let { slider.stepSize = it.toFloat() } + slider.value = getPersistedInt(defaultValue ?: from).toFloat() + + slider.addOnChangeListener { _, value, _ -> + persistInt(value.toInt()) + updateSummary() + } + } + + private fun updateSummary() { + val value = context.getString(R.string.value) + val entry = getPersistedInt(defaultValue ?: 0) + val sum = customSummary?.let { "\n\n$it" } ?: "" + summary = "$value : $entry$sum" + } +} \ No newline at end of file diff --git a/app/src/main/java/dev/brahmkshatriya/echo/utils/prefs/MaterialTextInputPreference.kt b/app/src/main/java/dev/brahmkshatriya/echo/utils/prefs/MaterialTextInputPreference.kt index 7fe9c4ff..72ef5708 100644 --- a/app/src/main/java/dev/brahmkshatriya/echo/utils/prefs/MaterialTextInputPreference.kt +++ b/app/src/main/java/dev/brahmkshatriya/echo/utils/prefs/MaterialTextInputPreference.kt @@ -32,7 +32,7 @@ class MaterialTextInputPreference(context: Context) : EditTextPreference(context editText?.hint = summary dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener { - val newText = editText?.text.toString() + val newText = editText?.text?.toString() if (callChangeListener(newText)) { text = newText dialog.dismiss() diff --git a/app/src/main/res/layout/preference_slider.xml b/app/src/main/res/layout/preference_slider.xml new file mode 100644 index 00000000..47c1cf77 --- /dev/null +++ b/app/src/main/res/layout/preference_slider.xml @@ -0,0 +1,24 @@ + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e888d038..f00f0bba 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -197,6 +197,8 @@ Video Quality Quality of the video, Video can also be disabled from here None + Cache Size + Size in MB to cache audio and video Highest Medium