Allow to share images when reading online. Move chapter cache to external cache dir. Dependency updates.

This commit is contained in:
len
2016-11-29 21:37:35 +01:00
parent 86b8712dd1
commit d30c019b89
12 changed files with 51 additions and 71 deletions

View File

@@ -2,8 +2,8 @@ package eu.kanade.tachiyomi.data.cache
import android.content.Context
import android.text.format.Formatter
import com.github.salomonbrys.kotson.fromJson
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.jakewharton.disklrucache.DiskLruCache
import eu.kanade.tachiyomi.data.source.model.Page
import eu.kanade.tachiyomi.util.DiskUtil
@@ -11,9 +11,9 @@ import eu.kanade.tachiyomi.util.saveTo
import okhttp3.Response
import okio.Okio
import rx.Observable
import uy.kohesive.injekt.injectLazy
import java.io.File
import java.io.IOException
import java.lang.reflect.Type
/**
* Class used to create chapter cache
@@ -26,15 +26,6 @@ import java.lang.reflect.Type
*/
class ChapterCache(private val context: Context) {
/** Google Json class used for parsing JSON files. */
private val gson: Gson = Gson()
/** Cache class used for cache management. */
private val diskCache: DiskLruCache
/** Page list collection used for deserializing from JSON. */
private val pageListCollection: Type = object : TypeToken<List<Page>>() {}.type
companion object {
/** Name of cache directory. */
const val PARAMETER_CACHE_DIRECTORY = "chapter_disk_cache"
@@ -49,38 +40,37 @@ class ChapterCache(private val context: Context) {
const val PARAMETER_CACHE_SIZE = 75L * 1024 * 1024
}
init {
// Open cache in default cache directory.
diskCache = DiskLruCache.open(
File(context.cacheDir, PARAMETER_CACHE_DIRECTORY),
PARAMETER_APP_VERSION,
PARAMETER_VALUE_COUNT,
PARAMETER_CACHE_SIZE)
}
/** Google Json class used for parsing JSON files. */
private val gson: Gson by injectLazy()
/** Cache class used for cache management. */
private val diskCache = DiskLruCache.open(
File(context.externalCacheDir, PARAMETER_CACHE_DIRECTORY),
PARAMETER_APP_VERSION,
PARAMETER_VALUE_COUNT,
PARAMETER_CACHE_SIZE)
/**
* Returns directory of cache.
* @return directory of cache.
*/
val cacheDir: File
get() = diskCache.directory
/**
* Returns real size of directory.
* @return real size of directory.
*/
private val realSize: Long
get() = DiskUtil.getDirectorySize(cacheDir)
/**
* Returns real size of directory in human readable format.
* @return real size of directory.
*/
val readableSize: String
get() = Formatter.formatFileSize(context, realSize)
/**
* Remove file from cache.
*
* @param file name of file "md5.0".
* @return status of deletion for the file.
*/
@@ -101,6 +91,7 @@ class ChapterCache(private val context: Context) {
/**
* Get page list from cache.
*
* @param chapterUrl the url of the chapter.
* @return an observable of the list of pages.
*/
@@ -111,13 +102,14 @@ class ChapterCache(private val context: Context) {
// Convert JSON string to list of objects. Throws an exception if snapshot is null
diskCache.get(key).use {
gson.fromJson(it.getString(0), pageListCollection)
gson.fromJson<List<Page>>(it.getString(0))
}
}
}
/**
* Add page list to disk cache.
*
* @param chapterUrl the url of the chapter.
* @param pages list of pages.
*/
@@ -151,7 +143,8 @@ class ChapterCache(private val context: Context) {
}
/**
* Check if image is in cache.
* Returns true if image is in cache.
*
* @param imageUrl url of image.
* @return true if in cache otherwise false.
*/
@@ -164,22 +157,20 @@ class ChapterCache(private val context: Context) {
}
/**
* Get image path from url.
* Get image file from url.
*
* @param imageUrl url of image.
* @return path of image.
*/
fun getImagePath(imageUrl: String): File? {
try {
// Get file from md5 key.
val imageName = DiskUtil.hashKeyForDisk(imageUrl) + ".0"
return File(diskCache.directory, imageName)
} catch (e: IOException) {
return null
}
fun getImageFile(imageUrl: String): File {
// Get file from md5 key.
val imageName = DiskUtil.hashKeyForDisk(imageUrl) + ".0"
return File(diskCache.directory, imageName)
}
/**
* Add image to cache.
*
* @param imageUrl url of image.
* @param response http response from page.
* @throws IOException image error.

View File

@@ -41,8 +41,8 @@ class DownloadProvider(private val context: Context) {
*/
internal fun getMangaDir(source: Source, manga: Manga): UniFile {
return downloadsDir
.subFile(getSourceDirName(source))!!
.subFile(getMangaDirName(manga))!!
.createDirectory(getSourceDirName(source))
.createDirectory(getMangaDirName(manga))
}
/**

View File

@@ -247,7 +247,7 @@ class Downloader(private val context: Context, private val provider: DownloadPro
private fun downloadChapter(download: Download): Observable<Download> {
val chapterDirname = provider.getChapterDirName(download.chapter)
val mangaDir = provider.getMangaDir(download.source, download.manga)
val tmpDir = mangaDir.subFile("${chapterDirname}_tmp")!!
val tmpDir = mangaDir.createDirectory("${chapterDirname}_tmp")
val pageListObservable = if (download.pages == null) {
// Pull page list from network and add them to download object
@@ -262,8 +262,6 @@ class Downloader(private val context: Context, private val provider: DownloadPro
return pageListObservable
.doOnNext { pages ->
tmpDir.ensureDir()
// Delete all temporary (unfinished) files
tmpDir.listFiles()
?.filter { it.name!!.endsWith(".tmp") }

View File

@@ -417,7 +417,7 @@ abstract class OnlineSource() : Source {
}
}
.doOnNext {
page.uri = Uri.fromFile(chapterCache.getImagePath(imageUrl))
page.uri = Uri.fromFile(chapterCache.getImageFile(imageUrl))
page.status = Page.READY
}
.doOnError { page.status = Page.ERROR }