Add option for runtime caching

This commit is contained in:
Bnyro 2022-11-17 15:59:12 +01:00
parent c3c24a73b3
commit ef48cc6bb1
3 changed files with 19 additions and 8 deletions

View File

@ -9,6 +9,7 @@ import android.widget.ImageView
import coil.ImageLoader
import coil.disk.DiskCache
import coil.load
import coil.request.CachePolicy
import coil.request.ImageRequest
import com.github.libretube.api.CronetHelper
import com.github.libretube.constants.PreferenceKeys
@ -25,17 +26,24 @@ object ImageHelper {
fun initializeImageLoader(context: Context) {
val maxImageCacheSize = PreferenceHelper.getString(
PreferenceKeys.MAX_IMAGE_CACHE,
"128"
).toInt()
val diskCache = DiskCache.Builder()
.directory(context.filesDir.resolve("coil"))
.maxSizeBytes(maxImageCacheSize * 1024 * 1024L)
.build()
""
)
imageLoader = ImageLoader.Builder(context)
.callFactory(CronetHelper.callFactory)
.diskCache(diskCache)
.apply {
when (maxImageCacheSize) {
"" -> {
memoryCachePolicy(CachePolicy.DISABLED)
}
else -> diskCache(
DiskCache.Builder()
.directory(context.filesDir.resolve("coil"))
.maxSizeBytes(maxImageCacheSize.toInt() * 1024 * 1024L)
.build()
)
}
}
.build()
}

View File

@ -399,6 +399,7 @@
<item>128MB</item>
<item>256MB</item>
<item>512MB</item>
<item>@string/limit_to_runtime</item>
</string-array>
<string-array name="cacheSizeValues">
@ -408,6 +409,7 @@
<item>128</item>
<item>256</item>
<item>512</item>
<item />
</string-array>
<string-array name="loadingInterval">

View File

@ -376,6 +376,7 @@
<string name="hls_instead_of_dash">Use HLS</string>
<string name="hls_instead_of_dash_summary">Use HLS instead of DASH (will be slower, not recommended)</string>
<string name="auto_quality">Auto</string>
<string name="limit_to_runtime">Limit to runtime</string>
<!-- Notification channel strings -->
<string name="download_channel_name">Download Service</string>