Coil: Caching adjustments (#5311)

* Coil: Enable disk cache for source items

* MangaCoverFetcher: Let Coil's OkHttp client handle caching for non-library cover
This commit is contained in:
Ivan Iskandar 2021-06-06 21:30:26 +07:00 committed by GitHub
parent de50f53be4
commit 3527dedc99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 17 additions and 30 deletions

View File

@ -27,7 +27,6 @@ import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get import uy.kohesive.injekt.api.get
import uy.kohesive.injekt.injectLazy import uy.kohesive.injekt.injectLazy
import java.io.File import java.io.File
import java.util.Date
/** /**
* Coil component that fetches [Manga] cover while using the cached file in disk when available. * Coil component that fetches [Manga] cover while using the cached file in disk when available.
@ -62,14 +61,15 @@ class MangaCoverFetcher : Fetcher<Manga> {
} }
private suspend fun httpLoader(manga: Manga, options: Options): FetchResult { 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 (coverCacheFile?.exists() == true && options.diskCachePolicy.readEnabled) {
if (coverFile.exists() && options.diskCachePolicy.readEnabled) { return fileLoader(coverCacheFile)
if (!manga.favorite) {
coverFile.setLastModified(Date().time)
}
return fileLoader(coverFile)
} }
val (response, body) = awaitGetCall(manga, options) val (response, body) = awaitGetCall(manga, options)
@ -78,18 +78,16 @@ class MangaCoverFetcher : Fetcher<Manga> {
throw HttpException(response) throw HttpException(response)
} }
// Write to disk for future use if (coverCacheFile != null && options.diskCachePolicy.writeEnabled) {
if (options.diskCachePolicy.writeEnabled) { @Suppress("BlockingMethodInNonBlockingContext")
response.peekBody(Long.MAX_VALUE).source().use { input -> response.peekBody(Long.MAX_VALUE).source().use { input ->
val tmpFile = File(coverFile.absolutePath + "_tmp") coverCacheFile.parentFile?.mkdirs()
tmpFile.parentFile?.mkdirs() if (coverCacheFile.exists()) {
tmpFile.sink().buffer().use { output -> coverCacheFile.delete()
}
coverCacheFile.sink().buffer().use { output ->
output.writeAll(input) output.writeAll(input)
} }
if (coverFile.exists()) {
coverFile.delete()
}
tmpFile.renameTo(coverFile)
} }
} }
@ -108,10 +106,6 @@ class MangaCoverFetcher : Fetcher<Manga> {
private fun getCall(manga: Manga, options: Options): Call { private fun getCall(manga: Manga, options: Options): Call {
val source = sourceManager.get(manga.source) as? HttpSource 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 { val request = Request.Builder().url(manga.thumbnail_url!!).also {
if (source != null) { if (source != null) {
it.headers(source.headers) it.headers(source.headers)
@ -135,7 +129,8 @@ class MangaCoverFetcher : Fetcher<Manga> {
} }
}.build() }.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 { private fun fileLoader(manga: Manga): FetchResult {

View File

@ -3,7 +3,6 @@ package eu.kanade.tachiyomi.ui.browse.source.browse
import android.view.View import android.view.View
import coil.clear import coil.clear
import coil.imageLoader import coil.imageLoader
import coil.request.CachePolicy
import coil.request.ImageRequest import coil.request.ImageRequest
import coil.transition.CrossfadeTransition import coil.transition.CrossfadeTransition
import eu.davidea.flexibleadapter.FlexibleAdapter 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) val request = ImageRequest.Builder(view.context)
.data(manga) .data(manga)
.setParameter(MangaCoverFetcher.USE_CUSTOM_COVER, false) .setParameter(MangaCoverFetcher.USE_CUSTOM_COVER, false)
.diskCachePolicy(CachePolicy.DISABLED)
.target(StateImageViewTarget(binding.thumbnail, binding.progress, crossfadeDuration)) .target(StateImageViewTarget(binding.thumbnail, binding.progress, crossfadeDuration))
.build() .build()
itemView.context.imageLoader.enqueue(request) itemView.context.imageLoader.enqueue(request)

View File

@ -3,7 +3,6 @@ package eu.kanade.tachiyomi.ui.browse.source.browse
import android.view.View import android.view.View
import coil.clear import coil.clear
import coil.imageLoader import coil.imageLoader
import coil.request.CachePolicy
import coil.request.ImageRequest import coil.request.ImageRequest
import coil.transition.CrossfadeTransition import coil.transition.CrossfadeTransition
import eu.davidea.flexibleadapter.FlexibleAdapter 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) val request = ImageRequest.Builder(view.context)
.data(manga) .data(manga)
.setParameter(MangaCoverFetcher.USE_CUSTOM_COVER, false) .setParameter(MangaCoverFetcher.USE_CUSTOM_COVER, false)
.diskCachePolicy(CachePolicy.DISABLED)
.target(StateImageViewTarget(binding.thumbnail, binding.progress, crossfadeDuration)) .target(StateImageViewTarget(binding.thumbnail, binding.progress, crossfadeDuration))
.build() .build()
itemView.context.imageLoader.enqueue(request) itemView.context.imageLoader.enqueue(request)

View File

@ -3,7 +3,6 @@ package eu.kanade.tachiyomi.ui.browse.source.browse
import android.view.View import android.view.View
import coil.clear import coil.clear
import coil.loadAny import coil.loadAny
import coil.request.CachePolicy
import coil.transform.RoundedCornersTransformation import coil.transform.RoundedCornersTransformation
import eu.davidea.flexibleadapter.FlexibleAdapter import eu.davidea.flexibleadapter.FlexibleAdapter
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
@ -51,7 +50,6 @@ class SourceListHolder(private val view: View, adapter: FlexibleAdapter<*>) :
binding.thumbnail.loadAny(manga) { binding.thumbnail.loadAny(manga) {
setParameter(MangaCoverFetcher.USE_CUSTOM_COVER, false) setParameter(MangaCoverFetcher.USE_CUSTOM_COVER, false)
transformations(RoundedCornersTransformation(radius)) transformations(RoundedCornersTransformation(radius))
diskCachePolicy(CachePolicy.DISABLED)
} }
} }
} }

View File

@ -3,7 +3,6 @@ package eu.kanade.tachiyomi.ui.browse.source.globalsearch
import android.view.View import android.view.View
import coil.clear import coil.clear
import coil.imageLoader import coil.imageLoader
import coil.request.CachePolicy
import coil.request.ImageRequest import coil.request.ImageRequest
import coil.transition.CrossfadeTransition import coil.transition.CrossfadeTransition
import eu.davidea.viewholders.FlexibleViewHolder import eu.davidea.viewholders.FlexibleViewHolder
@ -53,7 +52,6 @@ class GlobalSearchCardHolder(view: View, adapter: GlobalSearchCardAdapter) :
val request = ImageRequest.Builder(itemView.context) val request = ImageRequest.Builder(itemView.context)
.data(manga) .data(manga)
.setParameter(MangaCoverFetcher.USE_CUSTOM_COVER, false) .setParameter(MangaCoverFetcher.USE_CUSTOM_COVER, false)
.diskCachePolicy(CachePolicy.DISABLED)
.target(StateImageViewTarget(binding.cover, binding.progress, crossfadeDuration)) .target(StateImageViewTarget(binding.cover, binding.progress, crossfadeDuration))
.build() .build()
itemView.context.imageLoader.enqueue(request) itemView.context.imageLoader.enqueue(request)