Kaynağa Gözat

Coil: Caching adjustments (#5311)

* Coil: Enable disk cache for source items

* MangaCoverFetcher: Let Coil's OkHttp client handle caching for non-library cover
Ivan Iskandar 3 yıl önce
ebeveyn
işleme
3527dedc99

+ 17 - 22
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<Manga> {
     }
 
     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<Manga> {
             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 ->
-                    output.writeAll(input)
+                coverCacheFile.parentFile?.mkdirs()
+                if (coverCacheFile.exists()) {
+                    coverCacheFile.delete()
                 }
-                if (coverFile.exists()) {
-                    coverFile.delete()
+                coverCacheFile.sink().buffer().use { output ->
+                    output.writeAll(input)
                 }
-                tmpFile.renameTo(coverFile)
             }
         }
 
@@ -108,10 +106,6 @@ class MangaCoverFetcher : Fetcher<Manga> {
 
     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<Manga> {
             }
         }.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 {

+ 0 - 2
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)

+ 0 - 2
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)

+ 0 - 2
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)
             }
         }
     }

+ 0 - 2
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)