From 839e24e5ced631665f4f11629962575e4ebd7821 Mon Sep 17 00:00:00 2001 From: twistios Date: Tue, 15 Oct 2024 00:24:27 +0200 Subject: [PATCH 1/4] improved coil image disk cache - added options for 8GB and custom size - applied new formatting for custom size (usage) --- .../fast4x/rimusic/enums/CoilDiskCacheSize.kt | 6 ++- .../ui/screens/settings/CacheSettings.kt | 2 + .../ui/screens/settings/DataSettings.kt | 53 ++++++++++++++++--- .../it/fast4x/rimusic/utils/Preferences.kt | 1 + 4 files changed, 54 insertions(+), 8 deletions(-) diff --git a/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/enums/CoilDiskCacheSize.kt b/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/enums/CoilDiskCacheSize.kt index 48c5fb1bb0..1df5184dbd 100644 --- a/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/enums/CoilDiskCacheSize.kt +++ b/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/enums/CoilDiskCacheSize.kt @@ -8,7 +8,9 @@ enum class CoilDiskCacheMaxSize { `512MB`, `1GB`, `2GB`, - `4GB`; + `4GB`, + `8GB`, + Custom; val bytes: Long get() = when (this) { @@ -20,5 +22,7 @@ enum class CoilDiskCacheMaxSize { `1GB` -> 1024 `2GB` -> 2048 `4GB` -> 4096 + `8GB` -> 8192 + Custom -> 1000000 } * 1000 * 1000L } diff --git a/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/screens/settings/CacheSettings.kt b/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/screens/settings/CacheSettings.kt index 880dde7238..1119346880 100644 --- a/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/screens/settings/CacheSettings.kt +++ b/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/screens/settings/CacheSettings.kt @@ -155,6 +155,8 @@ fun CacheSettings() { CoilDiskCacheMaxSize.`1GB`-> "1GB" CoilDiskCacheMaxSize.`2GB` -> "2GB" CoilDiskCacheMaxSize.`4GB` -> "4GB" + CoilDiskCacheMaxSize.`8GB` -> "8GB" + CoilDiskCacheMaxSize.Custom -> stringResource(R.string.custom) } } ) diff --git a/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/screens/settings/DataSettings.kt b/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/screens/settings/DataSettings.kt index 5306af9693..82105b2495 100644 --- a/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/screens/settings/DataSettings.kt +++ b/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/screens/settings/DataSettings.kt @@ -58,6 +58,7 @@ import it.fast4x.rimusic.ui.components.themed.SmartMessage import it.fast4x.rimusic.ui.styling.Dimensions import it.fast4x.rimusic.ui.styling.shimmer import it.fast4x.rimusic.utils.bold +import it.fast4x.rimusic.utils.coilCustomDiskCacheKey import it.fast4x.rimusic.utils.coilDiskCacheMaxSizeKey import it.fast4x.rimusic.utils.exoPlayerCacheLocationKey import it.fast4x.rimusic.utils.exoPlayerCustomCacheKey @@ -127,6 +128,11 @@ fun DataSettings() { exoPlayerCustomCacheKey,32 ) + var showCoilCustomDiskCacheDialog by remember { mutableStateOf(false) } + var coilCustomDiskCache by rememberPreference( + coilCustomDiskCacheKey,32 + ) + //val release = Build.VERSION.RELEASE; val sdkVersion = Build.VERSION.SDK_INT; //if (sdkVersion.toShort() < 29) exoPlayerAlternateCacheLocation="" @@ -354,12 +360,24 @@ fun DataSettings() { EnumValueSelectorSettingsEntry( title = stringResource(R.string.image_cache_max_size), - titleSecondary = "${ - Formatter.formatShortFileSize( - context, - diskCacheSize - ) - } ${stringResource(R.string.used)} (${diskCacheSize * 100 / coilDiskCacheMaxSize.bytes.coerceAtLeast(1)}%)", +// titleSecondary = "${ +// Formatter.formatShortFileSize( +// context, +// diskCacheSize +// ) +// } ${stringResource(R.string.used)} (${diskCacheSize * 100 / coilDiskCacheMaxSize.bytes.coerceAtLeast(1)}%)", + titleSecondary = when (coilDiskCacheMaxSize) { + CoilDiskCacheMaxSize.Custom -> buildString { + append(Formatter.formatShortFileSize(context, diskCacheSize)) + append("/${Formatter.formatShortFileSize(context, coilCustomDiskCache.toLong() * 1000 * 1000)}") + append(" ${stringResource(R.string.used)}") + } + else -> buildString { + append(Formatter.formatShortFileSize(context, diskCacheSize)) + append(" ${stringResource(R.string.used)}") + append(" (${diskCacheSize * 100 / coilDiskCacheMaxSize.bytes}%)") + } + }, trailingContent = { HeaderIconButton( icon = R.drawable.trash, @@ -369,9 +387,14 @@ fun DataSettings() { ) }, selectedValue = coilDiskCacheMaxSize, - onValueSelected = { coilDiskCacheMaxSize = it}, + onValueSelected = { + coilDiskCacheMaxSize = it + if (coilDiskCacheMaxSize == CoilDiskCacheMaxSize.Custom) + showCoilCustomDiskCacheDialog = true + }, valueText = { when (it) { + CoilDiskCacheMaxSize.Custom -> stringResource(R.string.custom) CoilDiskCacheMaxSize.`32MB` -> "32MB" CoilDiskCacheMaxSize.`64MB` -> "64MB" CoilDiskCacheMaxSize.`128MB` -> "128MB" @@ -380,10 +403,26 @@ fun DataSettings() { CoilDiskCacheMaxSize.`1GB`-> "1GB" CoilDiskCacheMaxSize.`2GB` -> "2GB" CoilDiskCacheMaxSize.`4GB` -> "4GB" + CoilDiskCacheMaxSize.`8GB` -> "8GB" } } ) + if (showCoilCustomDiskCacheDialog) + InputNumericDialog( + title = stringResource(R.string.set_custom_cache), + placeholder = stringResource(R.string.enter_value_in_mb), + value = coilCustomDiskCache.toString(), + valueMin = "32", + valueMax = "10000", + onDismiss = { showCoilCustomDiskCacheDialog = false }, + setValue = { + //Log.d("customCache", it) + coilCustomDiskCache = it.toInt() + showCoilCustomDiskCacheDialog = false + } + ) + /* EnumValueSelectorSettingsEntry( title = stringResource(R.string.image_cache_max_size), diff --git a/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/utils/Preferences.kt b/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/utils/Preferences.kt index 62a2811b20..07930f15c0 100644 --- a/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/utils/Preferences.kt +++ b/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/utils/Preferences.kt @@ -69,6 +69,7 @@ const val UiTypeKey = "UiType" const val disablePlayerHorizontalSwipeKey = "disablePlayerHorizontalSwipe" const val disableIconButtonOnTopKey = "disableIconButtonOnTop" const val exoPlayerCustomCacheKey = "exoPlayerCustomCache" +const val coilCustomDiskCacheKey = "exoPlayerCustomCache" const val disableScrollingTextKey = "disableScrollingText" const val audioQualityFormatKey = "audioQualityFormat" const val showLikeButtonBackgroundPlayerKey = "showLikeButtonBackgroundPlayer" From 5508319df2d41600fc4c60b7ae47d53a772362a3 Mon Sep 17 00:00:00 2001 From: twistios Date: Tue, 15 Oct 2024 00:27:10 +0200 Subject: [PATCH 2/4] improved ExoPlayer disk cache custom text - applied new formatting for custom size (usage) - removed "exoPlayerCustomCache" in when-case (not used and also can never be reached) --- .../rimusic/ui/screens/settings/DataSettings.kt | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/screens/settings/DataSettings.kt b/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/screens/settings/DataSettings.kt index 82105b2495..bf3e9dc820 100644 --- a/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/screens/settings/DataSettings.kt +++ b/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/screens/settings/DataSettings.kt @@ -547,13 +547,20 @@ fun DataSettings() { title = stringResource(R.string.song_cache_max_size), titleSecondary = when (exoPlayerDiskCacheMaxSize) { ExoPlayerDiskCacheMaxSize.Disabled -> "" - ExoPlayerDiskCacheMaxSize.Custom -> stringResource(R.string.custom_cache_size) +" "+exoPlayerCustomCache+"MB" + ExoPlayerDiskCacheMaxSize.Custom -> buildString { + append(Formatter.formatShortFileSize(context, diskCacheSize)) + append("/${Formatter.formatShortFileSize(context, + exoPlayerCustomCache.toLong() * 1000 * 1000 + )}") + append(" ${stringResource(R.string.used)}") + } + // stringResource(R.string.custom_cache_size) +" "+exoPlayerCustomCache+"MB" else -> buildString { append(Formatter.formatShortFileSize(context, diskCacheSize)) append(" ${stringResource(R.string.used)}") when (val size = exoPlayerDiskCacheMaxSize) { ExoPlayerDiskCacheMaxSize.Unlimited -> {} - ExoPlayerDiskCacheMaxSize.Custom -> { exoPlayerCustomCache } + ExoPlayerDiskCacheMaxSize.Custom -> {} // only needed because of UNLIMITED else -> append(" (${diskCacheSize * 100 / size.bytes}%)") } } From b7e73604e29bbada850e67e3cab82620e74cb523 Mon Sep 17 00:00:00 2001 From: twistios Date: Tue, 15 Oct 2024 12:48:41 +0200 Subject: [PATCH 3/4] now the custom size is actually applied instead of only displayed --- .../it/fast4x/rimusic/MainApplication.kt | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/MainApplication.kt b/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/MainApplication.kt index 73107e2850..23638babe8 100644 --- a/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/MainApplication.kt +++ b/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/MainApplication.kt @@ -8,6 +8,7 @@ import coil.request.CachePolicy import it.fast4x.rimusic.enums.CoilDiskCacheMaxSize import it.fast4x.rimusic.utils.CaptureCrash import it.fast4x.rimusic.utils.FileLoggingTree +import it.fast4x.rimusic.utils.coilCustomDiskCacheKey import it.fast4x.rimusic.utils.coilDiskCacheMaxSizeKey import it.fast4x.rimusic.utils.getEnum import it.fast4x.rimusic.utils.logDebugEnabledKey @@ -42,6 +43,7 @@ class MainApplication : Application(), ImageLoaderFactory { } override fun newImageLoader(): ImageLoader { + val coilCustomDiskCache = preferences.getInt(coilCustomDiskCacheKey, 128) * 1000 * 1000L return ImageLoader.Builder(this) .crossfade(true) .networkCachePolicy(CachePolicy.ENABLED) @@ -62,10 +64,18 @@ class MainApplication : Application(), ImageLoaderFactory { DiskCache.Builder() .directory(filesDir.resolve("coil")) .maxSizeBytes( - preferences.getEnum( - coilDiskCacheMaxSizeKey, - CoilDiskCacheMaxSize.`128MB` - ).bytes + when (val size = + preferences.getEnum( + coilDiskCacheMaxSizeKey, + CoilDiskCacheMaxSize.`128MB` + )) { + CoilDiskCacheMaxSize.Custom -> coilCustomDiskCache + else -> size.bytes + } +// preferences.getEnum( +// coilDiskCacheMaxSizeKey, +// CoilDiskCacheMaxSize.`128MB` +// ).bytes ) .build() ) From 7538de4fa01f6e4a75a77c65b72d67ee220f1594 Mon Sep 17 00:00:00 2001 From: twistios Date: Sat, 19 Oct 2024 00:44:04 +0200 Subject: [PATCH 4/4] removed commented sections (for cleanup) --- .../androidMain/kotlin/it/fast4x/rimusic/MainApplication.kt | 4 ---- .../it/fast4x/rimusic/ui/screens/settings/DataSettings.kt | 6 ------ 2 files changed, 10 deletions(-) diff --git a/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/MainApplication.kt b/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/MainApplication.kt index 23638babe8..4986807c50 100644 --- a/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/MainApplication.kt +++ b/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/MainApplication.kt @@ -72,10 +72,6 @@ class MainApplication : Application(), ImageLoaderFactory { CoilDiskCacheMaxSize.Custom -> coilCustomDiskCache else -> size.bytes } -// preferences.getEnum( -// coilDiskCacheMaxSizeKey, -// CoilDiskCacheMaxSize.`128MB` -// ).bytes ) .build() ) diff --git a/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/screens/settings/DataSettings.kt b/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/screens/settings/DataSettings.kt index bf3e9dc820..813f4aabc7 100644 --- a/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/screens/settings/DataSettings.kt +++ b/composeApp/src/androidMain/kotlin/it/fast4x/rimusic/ui/screens/settings/DataSettings.kt @@ -360,12 +360,6 @@ fun DataSettings() { EnumValueSelectorSettingsEntry( title = stringResource(R.string.image_cache_max_size), -// titleSecondary = "${ -// Formatter.formatShortFileSize( -// context, -// diskCacheSize -// ) -// } ${stringResource(R.string.used)} (${diskCacheSize * 100 / coilDiskCacheMaxSize.bytes.coerceAtLeast(1)}%)", titleSecondary = when (coilDiskCacheMaxSize) { CoilDiskCacheMaxSize.Custom -> buildString { append(Formatter.formatShortFileSize(context, diskCacheSize))