From 3527dedc993afc0d88143c4fd03fbaf953a4e715 Mon Sep 17 00:00:00 2001 From: Ivan Iskandar <12537387+ivaniskandar@users.noreply.github.com> Date: Sun, 6 Jun 2021 21:30:26 +0700 Subject: [PATCH] Coil: Caching adjustments (#5311) * Coil: Enable disk cache for source items * MangaCoverFetcher: Let Coil's OkHttp client handle caching for non-library cover --- .../tachiyomi/data/coil/MangaCoverFetcher.kt | 39 ++++++++----------- .../browse/SourceComfortableGridHolder.kt | 2 - .../browse/source/browse/SourceGridHolder.kt | 2 - .../browse/source/browse/SourceListHolder.kt | 2 - .../globalsearch/GlobalSearchCardHolder.kt | 2 - 5 files changed, 17 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/coil/MangaCoverFetcher.kt b/app/src/main/java/eu/kanade/tachiyomi/data/coil/MangaCoverFetcher.kt index 6aae106cd8..5c5e09708c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/coil/MangaCoverFetcher.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/coil/MangaCoverFetcher.kt @@ -27,7 +27,6 @@ import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get import uy.kohesive.injekt.injectLazy import java.io.File -import java.util.Date /** * Coil component that fetches [Manga] cover while using the cached file in disk when available. @@ -62,14 +61,15 @@ class MangaCoverFetcher : Fetcher { } private suspend fun httpLoader(manga: Manga, options: Options): FetchResult { - val coverFile = coverCache.getCoverFile(manga) ?: error("No cover specified") + // Only cache separately if it's a library item + val coverCacheFile = if (manga.favorite) { + coverCache.getCoverFile(manga) ?: error("No cover specified") + } else { + null + } - // Use previously cached cover if exist - if (coverFile.exists() && options.diskCachePolicy.readEnabled) { - if (!manga.favorite) { - coverFile.setLastModified(Date().time) - } - return fileLoader(coverFile) + if (coverCacheFile?.exists() == true && options.diskCachePolicy.readEnabled) { + return fileLoader(coverCacheFile) } val (response, body) = awaitGetCall(manga, options) @@ -78,18 +78,16 @@ class MangaCoverFetcher : Fetcher { throw HttpException(response) } - // Write to disk for future use - if (options.diskCachePolicy.writeEnabled) { + if (coverCacheFile != null && options.diskCachePolicy.writeEnabled) { + @Suppress("BlockingMethodInNonBlockingContext") response.peekBody(Long.MAX_VALUE).source().use { input -> - val tmpFile = File(coverFile.absolutePath + "_tmp") - tmpFile.parentFile?.mkdirs() - tmpFile.sink().buffer().use { output -> + coverCacheFile.parentFile?.mkdirs() + if (coverCacheFile.exists()) { + coverCacheFile.delete() + } + coverCacheFile.sink().buffer().use { output -> output.writeAll(input) } - if (coverFile.exists()) { - coverFile.delete() - } - tmpFile.renameTo(coverFile) } } @@ -108,10 +106,6 @@ class MangaCoverFetcher : Fetcher { private fun getCall(manga: Manga, options: Options): Call { val source = sourceManager.get(manga.source) as? HttpSource - val client = source?.client ?: defaultClient - - val newClient = client.newBuilder().build() - val request = Request.Builder().url(manga.thumbnail_url!!).also { if (source != null) { it.headers(source.headers) @@ -135,7 +129,8 @@ class MangaCoverFetcher : Fetcher { } }.build() - return newClient.newCall(request) + val client = source?.client?.newBuilder()?.cache(defaultClient.cache)?.build() ?: defaultClient + return client.newCall(request) } private fun fileLoader(manga: Manga): FetchResult { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/SourceComfortableGridHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/SourceComfortableGridHolder.kt index 62787a684c..cd4f42f218 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/SourceComfortableGridHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/SourceComfortableGridHolder.kt @@ -3,7 +3,6 @@ package eu.kanade.tachiyomi.ui.browse.source.browse import android.view.View import coil.clear import coil.imageLoader -import coil.request.CachePolicy import coil.request.ImageRequest import coil.transition.CrossfadeTransition import eu.davidea.flexibleadapter.FlexibleAdapter @@ -53,7 +52,6 @@ class SourceComfortableGridHolder(private val view: View, private val adapter: F val request = ImageRequest.Builder(view.context) .data(manga) .setParameter(MangaCoverFetcher.USE_CUSTOM_COVER, false) - .diskCachePolicy(CachePolicy.DISABLED) .target(StateImageViewTarget(binding.thumbnail, binding.progress, crossfadeDuration)) .build() itemView.context.imageLoader.enqueue(request) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/SourceGridHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/SourceGridHolder.kt index 74ab0fd5e7..a238003b5f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/SourceGridHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/SourceGridHolder.kt @@ -3,7 +3,6 @@ package eu.kanade.tachiyomi.ui.browse.source.browse import android.view.View import coil.clear import coil.imageLoader -import coil.request.CachePolicy import coil.request.ImageRequest import coil.transition.CrossfadeTransition import eu.davidea.flexibleadapter.FlexibleAdapter @@ -53,7 +52,6 @@ open class SourceGridHolder(private val view: View, private val adapter: Flexibl val request = ImageRequest.Builder(view.context) .data(manga) .setParameter(MangaCoverFetcher.USE_CUSTOM_COVER, false) - .diskCachePolicy(CachePolicy.DISABLED) .target(StateImageViewTarget(binding.thumbnail, binding.progress, crossfadeDuration)) .build() itemView.context.imageLoader.enqueue(request) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/SourceListHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/SourceListHolder.kt index 5b98f5b481..a509c71e8e 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/SourceListHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/browse/SourceListHolder.kt @@ -3,7 +3,6 @@ package eu.kanade.tachiyomi.ui.browse.source.browse import android.view.View import coil.clear import coil.loadAny -import coil.request.CachePolicy import coil.transform.RoundedCornersTransformation import eu.davidea.flexibleadapter.FlexibleAdapter import eu.kanade.tachiyomi.R @@ -51,7 +50,6 @@ class SourceListHolder(private val view: View, adapter: FlexibleAdapter<*>) : binding.thumbnail.loadAny(manga) { setParameter(MangaCoverFetcher.USE_CUSTOM_COVER, false) transformations(RoundedCornersTransformation(radius)) - diskCachePolicy(CachePolicy.DISABLED) } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/globalsearch/GlobalSearchCardHolder.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/globalsearch/GlobalSearchCardHolder.kt index d7c83b0631..4c5c23d280 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/globalsearch/GlobalSearchCardHolder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/source/globalsearch/GlobalSearchCardHolder.kt @@ -3,7 +3,6 @@ package eu.kanade.tachiyomi.ui.browse.source.globalsearch import android.view.View import coil.clear import coil.imageLoader -import coil.request.CachePolicy import coil.request.ImageRequest import coil.transition.CrossfadeTransition import eu.davidea.viewholders.FlexibleViewHolder @@ -53,7 +52,6 @@ class GlobalSearchCardHolder(view: View, adapter: GlobalSearchCardAdapter) : val request = ImageRequest.Builder(itemView.context) .data(manga) .setParameter(MangaCoverFetcher.USE_CUSTOM_COVER, false) - .diskCachePolicy(CachePolicy.DISABLED) .target(StateImageViewTarget(binding.cover, binding.progress, crossfadeDuration)) .build() itemView.context.imageLoader.enqueue(request)