This commit is contained in:
Tristan Corbellari 2024-01-13 19:02:30 +00:00 committed by GitHub
commit b90989fcec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 40 additions and 6 deletions

View file

@ -51,6 +51,7 @@ sealed class Preference {
val value: Int,
val min: Int = 0,
val max: Int,
val steps: Int? = null,
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,19 @@ 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.InfoPreference(stringResource(MR.strings.chapter_cache_size_info)),
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
@ -28,6 +31,7 @@ import java.io.IOException
class ChapterCache(
private val context: Context,
private val json: Json,
var libraryPreferences: LibraryPreferences,
) {
/** Cache class used for cache management. */
@ -35,7 +39,7 @@ class ChapterCache(
File(context.cacheDir, "chapter_disk_cache"),
PARAMETER_APP_VERSION,
PARAMETER_VALUE_COUNT,
PARAMETER_CACHE_SIZE,
libraryPreferences.chapterCacheSize().get().toLong() * 1024 * 1024,
)
/**
@ -160,7 +164,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 +213,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

@ -114,7 +114,7 @@ class AppModule(val app: Application) : InjektModule {
addSingletonFactory { UniFileTempFileManager(app) }
addSingletonFactory { ChapterCache(app, get()) }
addSingletonFactory { ChapterCache(app, get(), get()) }
addSingletonFactory { CoverCache(app) }
addSingletonFactory { NetworkHelper(app, get()) }

View file

@ -170,6 +170,8 @@ class LibraryPreferences(
)
}
fun chapterCacheSize() = preferenceStore.getInt("chapter_cache_size", 100)
fun autoClearChapterCache() = preferenceStore.getBoolean("auto_clear_chapter_cache", false)
// endregion
@ -204,5 +206,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

@ -534,6 +534,9 @@
<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="chapter_cache_size_info">Changes will take effect upon app restart or clearing chapter cache.</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? = null,
) {
val haptic = LocalHapticFeedback.current
@ -193,7 +194,7 @@ fun SliderItem(
},
modifier = Modifier.weight(1.5f),
valueRange = min.toFloat()..max.toFloat(),
steps = max - min,
steps = steps ?: (max - min),
)
}
}