Skip to content

Commit

Permalink
Add Cache Size setting
Browse files Browse the repository at this point in the history
  • Loading branch information
brahmkshatriya committed Aug 31, 2024
1 parent 9469ab6 commit d6ce232
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 3 deletions.
6 changes: 4 additions & 2 deletions app/src/main/java/dev/brahmkshatriya/echo/di/AppModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")

Expand Down
Original file line number Diff line number Diff line change
@@ -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<Slider>(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"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
24 changes: 24 additions & 0 deletions app/src/main/res/layout/preference_slider.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingVertical="12dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingVertical="8dp">

<include
android:id="@android:id/icon_frame"
layout="@layout/preference_common" />
</LinearLayout>

<com.google.android.material.slider.Slider
android:id="@+id/preferences_slider"
android:layout_width="match_parent"
android:value="0.4"
android:layout_marginHorizontal="8dp"
android:layout_height="wrap_content" />
</LinearLayout>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@
<string name="video_quality">Video Quality</string>
<string name="video_quality_summary">Quality of the video, Video can also be disabled from here</string>
<string name="video_quality_none">None</string>
<string name="cache_size">Cache Size</string>
<string name="cache_size_summary">Size in MB to cache audio and video</string>
<string-array name="stream_qualities">
<item>Highest</item>
<item>Medium</item>
Expand Down

0 comments on commit d6ce232

Please sign in to comment.