Skip to content
This repository has been archived by the owner on Aug 7, 2024. It is now read-only.

Commit

Permalink
feat: option to change music cache size (#27)
Browse files Browse the repository at this point in the history
* feat: option to change music cache size
  • Loading branch information
SuhasDissa authored Oct 11, 2023
1 parent 6b48d83 commit f601aa5
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import androidx.media3.datasource.DefaultDataSource
import androidx.media3.datasource.DefaultHttpDataSource
import androidx.media3.datasource.ResolvingDataSource
import androidx.media3.datasource.cache.CacheDataSource
import androidx.media3.datasource.cache.LeastRecentlyUsedCacheEvictor
import androidx.media3.datasource.cache.NoOpCacheEvictor
import androidx.media3.datasource.cache.SimpleCache
import androidx.media3.exoplayer.ExoPlayer
Expand All @@ -35,6 +36,7 @@ import androidx.media3.session.MediaSession
import androidx.media3.session.MediaSessionService
import app.suhasdissa.mellowmusic.MellowMusicApplication
import app.suhasdissa.mellowmusic.utils.DynamicDataSource
import app.suhasdissa.mellowmusic.utils.Pref
import app.suhasdissa.mellowmusic.utils.mediaIdList
import coil.ImageLoader
import coil.request.ErrorResult
Expand All @@ -60,7 +62,9 @@ class PlayerService : MediaSessionService(), MediaSession.Callback, Player.Liste
@androidx.annotation.OptIn(androidx.media3.common.util.UnstableApi::class)
override fun onCreate() {
super.onCreate()
val cacheEvictor = NoOpCacheEvictor()
val maxMBytes = Pref.sharedPreferences.getInt(Pref.exoCacheKey, 0)
val cacheEvictor =
if (maxMBytes > 0) LeastRecentlyUsedCacheEvictor(maxMBytes * 1024 * 1024L) else NoOpCacheEvictor()
val directory = cacheDir.resolve("exoplayer").also { directory ->
directory.mkdir()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package app.suhasdissa.mellowmusic.ui.components

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.FilterChip
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.setValue
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import app.suhasdissa.mellowmusic.R
import app.suhasdissa.mellowmusic.utils.Pref
import app.suhasdissa.mellowmusic.utils.formatMB
import app.suhasdissa.mellowmusic.utils.rememberPreference

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun CacheSizeDialog(onDismissRequest: () -> Unit) {
val cacheSizes = listOf(0, 512, 1024, 1024 * 2, 1024 * 4, 1024 * 6)
var prefSize by rememberPreference(key = Pref.exoCacheKey, defaultValue = 0)
AlertDialog(
onDismissRequest,
title = { Text(stringResource(R.string.change_music_cache_size)) },
confirmButton = {
Button(onClick = {
onDismissRequest.invoke()
}) {
Text(text = stringResource(R.string.ok))
}
},
text = {
LazyVerticalGrid(
columns = GridCells.Fixed(3),
verticalArrangement = Arrangement.spacedBy(8.dp),
horizontalArrangement = Arrangement.spacedBy(8.dp)
) {
items(items = cacheSizes) {
FilterChip(
selected = prefSize == it,
onClick = { prefSize = it },
label = {
Text(
if (it == 0) {
stringResource(
R.string.unlimited
)
} else {
formatMB(it)
}
)
}
)
}
}
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.SettingsBackupRestore
import androidx.compose.material.icons.filled.Storage
import androidx.compose.material.icons.filled.Web
import androidx.compose.material.icons.outlined.Info
import androidx.compose.material3.*
Expand All @@ -25,6 +26,7 @@ import app.suhasdissa.mellowmusic.Destination
import app.suhasdissa.mellowmusic.R
import app.suhasdissa.mellowmusic.backend.models.Login
import app.suhasdissa.mellowmusic.backend.viewmodel.AuthViewModel
import app.suhasdissa.mellowmusic.ui.components.CacheSizeDialog
import app.suhasdissa.mellowmusic.ui.components.SettingItem

@OptIn(ExperimentalMaterial3Api::class)
Expand All @@ -36,6 +38,7 @@ fun SettingsScreen(
var showLoginDialog by remember { mutableStateOf(false) }
val authViewModel: AuthViewModel = viewModel(factory = AuthViewModel.Factory)
val topBarBehavior = TopAppBarDefaults.exitUntilCollapsedScrollBehavior()
var showImageCacheDialog by remember { mutableStateOf(false) }

Scaffold(modifier = modifier.fillMaxSize(), topBar = {
LargeTopAppBar(
Expand Down Expand Up @@ -66,6 +69,16 @@ fun SettingsScreen(
icon = Icons.Default.Web
)
}
item {
SettingItem(
title = stringResource(R.string.music_cache_limit),
description = stringResource(R.string.change_music_cache_size),
onClick = {
showImageCacheDialog = true
},
icon = Icons.Default.Storage
)
}
/*
item {
SettingItem(
Expand Down Expand Up @@ -115,4 +128,9 @@ fun SettingsScreen(
}
)
}
if (showImageCacheDialog) {
CacheSizeDialog {
showImageCacheDialog = false
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package app.suhasdissa.mellowmusic.utils

import kotlin.math.ln
import kotlin.math.pow

fun formatMB(mb: Int): String {
val index = (ln(mb.toDouble()) / ln(1024.0)).toInt()
return "${mb.div(1024f.pow(index)).toInt()} ${arrayOf("M", "G", "T")[index]}B"
}
1 change: 1 addition & 0 deletions app/src/main/java/app/suhasdissa/mellowmusic/utils/Pref.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import kotlinx.serialization.json.Json
object Pref {
private const val pipedInstanceKey = "SelectedPipedInstanceKey"
const val authTokenKey = "AuthTokenKey"
const val exoCacheKey = "ExoCacheKey"

lateinit var sharedPreferences: SharedPreferences

Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,8 @@
<string name="local_music">Local Music</string>
<string name="albums">Albums</string>
<string name="artists">Artists</string>
<string name="ok">OK</string>
<string name="change_music_cache_size">Change music cache size</string>
<string name="music_cache_limit">Music cache limit</string>
<string name="unlimited">Unlimited</string>
</resources>

0 comments on commit f601aa5

Please sign in to comment.