Add song cache settings

This commit is contained in:
vfsfitvnm
2022-06-12 14:40:41 +02:00
parent 05afb475d4
commit 625fbb0247
5 changed files with 87 additions and 24 deletions

View File

@@ -1,5 +1,6 @@
package it.vfsfitvnm.vimusic.ui.screens.settings
import android.os.Bundle
import android.text.format.Formatter
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.*
@@ -16,15 +17,18 @@ import coil.Coil
import coil.annotation.ExperimentalCoilApi
import it.vfsfitvnm.route.RouteHandler
import it.vfsfitvnm.vimusic.R
import it.vfsfitvnm.vimusic.services.GetCacheSizeCommand
import it.vfsfitvnm.vimusic.ui.components.SeekBar
import it.vfsfitvnm.vimusic.ui.components.TopAppBar
import it.vfsfitvnm.vimusic.ui.screens.*
import it.vfsfitvnm.vimusic.ui.styling.LocalColorPalette
import it.vfsfitvnm.vimusic.ui.styling.LocalTypography
import it.vfsfitvnm.vimusic.utils.LocalPreferences
import it.vfsfitvnm.vimusic.utils.LocalYoutubePlayer
import it.vfsfitvnm.vimusic.utils.secondary
import it.vfsfitvnm.vimusic.utils.semiBold
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.guava.await
import kotlinx.coroutines.launch
@OptIn(ExperimentalCoilApi::class)
@@ -54,10 +58,9 @@ fun OtherScreen() {
val colorPalette = LocalColorPalette.current
val typography = LocalTypography.current
val preferences = LocalPreferences.current
val mediaController = LocalYoutubePlayer.current?.mediaController
var coilDiskCache by remember {
mutableStateOf(Coil.imageLoader(context).diskCache)
}
val coilDiskCache = Coil.imageLoader(context).diskCache
val coroutineScope = rememberCoroutineScope()
@@ -94,7 +97,6 @@ fun OtherScreen() {
)
}
coilDiskCache?.let { diskCache ->
var diskCacheSize by remember(diskCache) {
mutableStateOf(diskCache.size)
@@ -133,7 +135,6 @@ fun OtherScreen() {
},
onDrag = { delta ->
scrubbingDiskCacheMaxSize = scrubbingDiskCacheMaxSize?.plus(delta)?.coerceIn(250L * 1024 * 1024, 2048L * 1024 * 1024)
println("new = $scrubbingDiskCacheMaxSize")
},
onDragEnd = {
preferences.coilDiskCacheMaxSizeBytes = scrubbingDiskCacheMaxSize ?: preferences.coilDiskCacheMaxSizeBytes
@@ -164,6 +165,64 @@ fun OtherScreen() {
}
)
}
mediaController?.let { mediaController ->
val diskCacheSize by produceState(initialValue = 0L) {
value = mediaController.sendCustomCommand(GetCacheSizeCommand, Bundle.EMPTY).await().extras.getLong("cacheSize")
}
var scrubbingDiskCacheMaxSize by remember {
mutableStateOf<Long?>(null)
}
SettingsEntryGroupText(
title = "SONG CACHE",
)
Column(
modifier = Modifier
.padding(start = 24.dp)
.padding(horizontal = 32.dp, vertical = 16.dp)
.fillMaxWidth()
) {
BasicText(
text = "Max size",
style = typography.xs.semiBold,
)
BasicText(
text = Formatter.formatShortFileSize(context, scrubbingDiskCacheMaxSize ?: preferences.exoPlayerDiskCacheMaxSizeBytes),
style = typography.xs.semiBold.secondary
)
SeekBar(
value = (scrubbingDiskCacheMaxSize ?: preferences.exoPlayerDiskCacheMaxSizeBytes).coerceIn(250L * 1024 * 1024, 4096L * 1024 * 1024),
minimumValue = 250L * 1024 * 1024,
maximumValue = 4096L * 1024 * 1024,
onDragStart = {
scrubbingDiskCacheMaxSize = it
},
onDrag = { delta ->
scrubbingDiskCacheMaxSize = scrubbingDiskCacheMaxSize?.plus(delta)?.coerceIn(250L * 1024 * 1024, 4096L * 1024 * 1024)
},
onDragEnd = {
preferences.exoPlayerDiskCacheMaxSizeBytes = scrubbingDiskCacheMaxSize ?: preferences.exoPlayerDiskCacheMaxSizeBytes
scrubbingDiskCacheMaxSize = null
},
color = colorPalette.text,
backgroundColor = colorPalette.textDisabled,
shape = RoundedCornerShape(8.dp),
modifier = Modifier
.padding(top = 8.dp)
.fillMaxWidth()
)
}
DisabledSettingsEntry(
title = "Space used",
text = "${Formatter.formatShortFileSize(context, diskCacheSize)} (${diskCacheSize * 100 / preferences.exoPlayerDiskCacheMaxSizeBytes.coerceAtLeast(1)}%)",
)
}
}
}
}