Added ability for user to change chapter cache size

This commit is contained in:
tristancorbellari 2024-01-07 23:55:52 +02:00
parent bce6af62fc
commit 661ad61bae
7 changed files with 38 additions and 5 deletions

View file

@ -51,6 +51,7 @@ sealed class Preference {
val value: Int,
val min: Int = 0,
val max: Int,
val steps: Int = 0,
override val title: String = "",
override val subtitle: String? = null,
override val icon: ImageVector? = null,

View file

@ -83,6 +83,7 @@ internal fun PreferenceItem(
label = item.title,
min = item.min,
max = item.max,
steps = item.steps,
value = item.value,
valueText = item.subtitle.takeUnless { it.isNullOrEmpty() } ?: item.value.toString(),
onChange = {

View file

@ -252,9 +252,12 @@ object SettingsDataScreen : SearchableSettings {
val scope = rememberCoroutineScope()
val libraryPreferences = remember { Injekt.get<LibraryPreferences>() }
val chapterCacheSizePref = libraryPreferences.chapterCacheSize()
val chapterCache = remember { Injekt.get<ChapterCache>() }
var cacheReadableSizeSema by remember { mutableIntStateOf(0) }
val cacheReadableSize = remember(cacheReadableSizeSema) { chapterCache.readableSize }
val chapterCacheSize by chapterCacheSizePref.collectAsState()
return Preference.PreferenceGroup(
title = stringResource(MR.strings.pref_storage_usage),
@ -289,6 +292,18 @@ object SettingsDataScreen : SearchableSettings {
}
},
),
Preference.PreferenceItem.SliderPreference(
value = chapterCacheSize,
title = stringResource(MR.strings.pref_chapter_cache_size),
subtitle = stringResource(MR.strings.chapter_cache_size, chapterCacheSize),
min = LibraryPreferences.CHAPTER_CACHE_SIZE_MIN,
max = LibraryPreferences.CHAPTER_CACHE_SIZE_MAX,
steps = 7,
onValueChanged = {
chapterCacheSizePref.set(it)
true
},
),
Preference.PreferenceItem.SwitchPreference(
pref = libraryPreferences.autoClearChapterCache(),
title = stringResource(MR.strings.pref_auto_clear_chapter_cache),

View file

@ -14,6 +14,9 @@ import okio.buffer
import okio.sink
import tachiyomi.core.util.system.logcat
import tachiyomi.domain.chapter.model.Chapter
import tachiyomi.domain.library.service.LibraryPreferences
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import java.io.File
import java.io.IOException
@ -30,12 +33,14 @@ class ChapterCache(
private val json: Json,
) {
val libraryPreferences = Injekt.get<LibraryPreferences>()
/** Cache class used for cache management. */
private val diskCache = DiskLruCache.open(
File(context.cacheDir, "chapter_disk_cache"),
PARAMETER_APP_VERSION,
PARAMETER_VALUE_COUNT,
PARAMETER_CACHE_SIZE,
libraryPreferences.chapterCacheSize().get().toLong() * 1024 * 1024,
)
/**
@ -160,7 +165,13 @@ class ChapterCache(
}
}
/**
* Clear all files from cache.
*
* @return number of files deleted.
*/
fun clear(): Int {
diskCache.maxSize = libraryPreferences.chapterCacheSize().get().toLong() * 1024 * 1024
var deletedFiles = 0
cacheDir.listFiles()?.forEach {
if (removeFileFromCache(it.name)) {
@ -203,6 +214,3 @@ private const val PARAMETER_APP_VERSION = 1
/** The number of values per cache entry. Must be positive. */
private const val PARAMETER_VALUE_COUNT = 1
/** The maximum number of bytes this cache should use to store. */
private const val PARAMETER_CACHE_SIZE = 100L * 1024 * 1024

View file

@ -190,6 +190,8 @@ class LibraryPreferences(
)
}
fun chapterCacheSize() = preferenceStore.getInt("chapter_cache_size", 100)
fun autoClearChapterCache() = preferenceStore.getBoolean("auto_clear_chapter_cache", false)
// endregion
@ -224,5 +226,8 @@ class LibraryPreferences(
const val MANGA_HAS_UNREAD = "manga_fully_read"
const val MANGA_NON_READ = "manga_started"
const val MANGA_OUTSIDE_RELEASE_PERIOD = "manga_outside_release_period"
const val CHAPTER_CACHE_SIZE_MIN = 100
const val CHAPTER_CACHE_SIZE_MAX = 500
}
}

View file

@ -539,6 +539,8 @@
<string name="used_cache">Used: %1$s</string>
<string name="cache_deleted">Cache cleared, %1$d files deleted</string>
<string name="cache_delete_error">Error occurred while clearing</string>
<string name="pref_chapter_cache_size">Chapter cache size</string>
<string name="chapter_cache_size">%1$s MB</string>
<string name="pref_auto_clear_chapter_cache">Clear chapter cache on app launch</string>
<!-- Sync section -->

View file

@ -161,6 +161,7 @@ fun SliderItem(
onChange: (Int) -> Unit,
max: Int,
min: Int = 0,
steps: Int = 0,
) {
val haptic = LocalHapticFeedback.current
@ -193,7 +194,7 @@ fun SliderItem(
},
modifier = Modifier.weight(1.5f),
valueRange = min.toFloat()..max.toFloat(),
steps = max - min,
steps = if (steps > 0) steps else max - min,
)
}
}