Changes in cover cache. Store covers in external cache dir

This commit is contained in:
len
2016-04-11 18:49:45 +02:00
parent b84635ffec
commit b8d1a88623
11 changed files with 115 additions and 124 deletions

View File

@@ -1,14 +1,11 @@
package eu.kanade.tachiyomi.data.cache
import android.content.Context
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.model.GlideUrl
import com.bumptech.glide.load.model.LazyHeaders
import com.bumptech.glide.request.animation.GlideAnimation
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.signature.StringSignature
import eu.kanade.tachiyomi.util.DiskUtils
import java.io.File
import java.io.IOException
@@ -28,17 +25,15 @@ class CoverCache(private val context: Context) {
/**
* Cache directory used for cache management.
*/
private val CACHE_DIRNAME = "cover_disk_cache"
private val cacheDir: File = File(context.cacheDir, CACHE_DIRNAME)
private val cacheDir: File = File(context.externalCacheDir, "cover_disk_cache")
/**
* Download the cover with Glide and save the file.
* @param thumbnailUrl url of thumbnail.
* @param headers headers included in Glide request.
* @param imageView imageView where picture should be displayed.
* @param onReady function to call when the image is ready
*/
@JvmOverloads
fun save(thumbnailUrl: String?, headers: LazyHeaders, imageView: ImageView? = null) {
fun save(thumbnailUrl: String?, headers: LazyHeaders, onReady: ((File) -> Unit)? = null) {
// Check if url is empty.
if (thumbnailUrl.isNullOrEmpty())
return
@@ -51,12 +46,9 @@ class CoverCache(private val context: Context) {
override fun onResourceReady(resource: File, anim: GlideAnimation<in File>) {
try {
// Copy the cover from Glide's cache to local cache.
copyToLocalCache(thumbnailUrl!!, resource)
copyToCache(thumbnailUrl!!, resource)
// Check if imageView isn't null and show picture in imageView.
if (imageView != null) {
loadFromCache(imageView, resource)
}
onReady?.invoke(resource)
} catch (e: IOException) {
// Do nothing.
}
@@ -64,6 +56,35 @@ class CoverCache(private val context: Context) {
})
}
/**
* Save or load the image from cache
* @param thumbnailUrl the thumbnail url.
* @param headers headers included in Glide request.
* @param onReady function to call when the image is ready
*/
fun saveOrLoadFromCache(thumbnailUrl: String?, headers: LazyHeaders, onReady: ((File) -> Unit)?) {
// Check if url is empty.
if (thumbnailUrl.isNullOrEmpty())
return
// If file exist load it otherwise save it.
val localCover = getCoverFromCache(thumbnailUrl!!)
if (localCover.exists()) {
onReady?.invoke(localCover)
} else {
save(thumbnailUrl, headers, onReady)
}
}
/**
* Returns the cover from cache.
* @param thumbnailUrl the thumbnail url.
* @return cover image.
*/
private fun getCoverFromCache(thumbnailUrl: String): File {
return File(cacheDir, DiskUtils.hashKeyForDisk(thumbnailUrl))
}
/**
* Copy the given file to this cache.
* @param thumbnailUrl url of thumbnail.
@@ -71,7 +92,7 @@ class CoverCache(private val context: Context) {
* @throws IOException if there's any error.
*/
@Throws(IOException::class)
fun copyToLocalCache(thumbnailUrl: String, sourceFile: File) {
fun copyToCache(thumbnailUrl: String, sourceFile: File) {
// Get destination file.
val destFile = getCoverFromCache(thumbnailUrl)
@@ -85,28 +106,19 @@ class CoverCache(private val context: Context) {
* @throws IOException if there's any error.
*/
@Throws(IOException::class)
fun copyToLocalCache(thumbnailUrl: String, inputStream: InputStream) {
fun copyToCache(thumbnailUrl: String, inputStream: InputStream) {
// Get destination file.
val destFile = getCoverFromCache(thumbnailUrl)
destFile.outputStream().use { inputStream.copyTo(it) }
}
/**
* Returns the cover from cache.
* @param thumbnailUrl the thumbnail url.
* @return cover image.
*/
private fun getCoverFromCache(thumbnailUrl: String): File {
return File(cacheDir, DiskUtils.hashKeyForDisk(thumbnailUrl))
}
/**
* Delete the cover file from the cache.
* @param thumbnailUrl the thumbnail url.
* @return status of deletion.
*/
fun deleteCoverFromCache(thumbnailUrl: String?): Boolean {
fun deleteFromCache(thumbnailUrl: String?): Boolean {
// Check if url is empty.
if (thumbnailUrl.isNullOrEmpty())
return false
@@ -116,56 +128,4 @@ class CoverCache(private val context: Context) {
return file.exists() && file.delete()
}
/**
* Save or load the image from cache
* @param imageView imageView where picture should be displayed.
* @param thumbnailUrl the thumbnail url.
* @param headers headers included in Glide request.
*/
fun saveOrLoadFromCache(imageView: ImageView, thumbnailUrl: String, headers: LazyHeaders) {
// If file exist load it otherwise save it.
val localCover = getCoverFromCache(thumbnailUrl)
if (localCover.exists()) {
loadFromCache(imageView, localCover)
} else {
save(thumbnailUrl, headers, imageView)
}
}
/**
* Helper method to load the cover from the cache directory into the specified image view.
* Glide stores the resized image in its cache to improve performance.
* @param imageView imageView where picture should be displayed.
* @param file file to load. Must exist!.
*/
private fun loadFromCache(imageView: ImageView, file: File) {
Glide.with(context)
.load(file)
.diskCacheStrategy(DiskCacheStrategy.RESULT)
.centerCrop()
.signature(StringSignature(file.lastModified().toString()))
.into(imageView)
}
/**
* Helper method to load the cover from network into the specified image view.
* The source image is stored in Glide's cache so that it can be easily copied to this cache
* if the manga is added to the library.
* @param imageView imageView where picture should be displayed.
* @param thumbnailUrl url of thumbnail.
* @param headers headers included in Glide request.
*/
fun loadFromNetwork(imageView: ImageView, thumbnailUrl: String?, headers: LazyHeaders) {
// Check if url is empty.
if (thumbnailUrl.isNullOrEmpty())
return
val url = GlideUrl(thumbnailUrl, headers)
Glide.with(context)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.centerCrop()
.into(imageView)
}
}