Fix crash in history settings
This commit is contained in:
@@ -4,7 +4,7 @@ import android.content.Context
|
|||||||
import com.tomclaw.cache.DiskLruCache
|
import com.tomclaw.cache.DiskLruCache
|
||||||
import org.koitharu.kotatsu.utils.FileSizeUtils
|
import org.koitharu.kotatsu.utils.FileSizeUtils
|
||||||
import org.koitharu.kotatsu.utils.ext.longHashCode
|
import org.koitharu.kotatsu.utils.ext.longHashCode
|
||||||
import org.koitharu.kotatsu.utils.ext.sub
|
import org.koitharu.kotatsu.utils.ext.subdir
|
||||||
import org.koitharu.kotatsu.utils.ext.takeIfReadable
|
import org.koitharu.kotatsu.utils.ext.takeIfReadable
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.InputStream
|
import java.io.InputStream
|
||||||
@@ -13,8 +13,10 @@ import java.io.OutputStream
|
|||||||
class PagesCache(context: Context) {
|
class PagesCache(context: Context) {
|
||||||
|
|
||||||
private val cacheDir = context.externalCacheDir ?: context.cacheDir
|
private val cacheDir = context.externalCacheDir ?: context.cacheDir
|
||||||
private val lruCache =
|
private val lruCache = DiskLruCache.create(
|
||||||
DiskLruCache.create(cacheDir.sub(Cache.PAGES.dir), FileSizeUtils.mbToBytes(200))
|
cacheDir.subdir(Cache.PAGES.dir),
|
||||||
|
FileSizeUtils.mbToBytes(200)
|
||||||
|
)
|
||||||
|
|
||||||
operator fun get(url: String): File? {
|
operator fun get(url: String): File? {
|
||||||
return lruCache.get(url)?.takeIfReadable()
|
return lruCache.get(url)?.takeIfReadable()
|
||||||
@@ -22,7 +24,7 @@ class PagesCache(context: Context) {
|
|||||||
|
|
||||||
@Deprecated("Useless lambda")
|
@Deprecated("Useless lambda")
|
||||||
fun put(url: String, writer: (OutputStream) -> Unit): File {
|
fun put(url: String, writer: (OutputStream) -> Unit): File {
|
||||||
val file = cacheDir.sub(url.longHashCode().toString())
|
val file = File(cacheDir, url.longHashCode().toString())
|
||||||
file.outputStream().use(writer)
|
file.outputStream().use(writer)
|
||||||
val res = lruCache.put(url, file)
|
val res = lruCache.put(url, file)
|
||||||
file.delete()
|
file.delete()
|
||||||
@@ -30,7 +32,7 @@ class PagesCache(context: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun put(url: String, inputStream: InputStream): File {
|
fun put(url: String, inputStream: InputStream): File {
|
||||||
val file = cacheDir.sub(url.longHashCode().toString())
|
val file = File(cacheDir, url.longHashCode().toString())
|
||||||
file.outputStream().use { out ->
|
file.outputStream().use { out ->
|
||||||
inputStream.copyTo(out)
|
inputStream.copyTo(out)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -96,7 +96,7 @@ class MangaSearchRepository(
|
|||||||
MangaSuggestionsProvider.QUERY_URI,
|
MangaSuggestionsProvider.QUERY_URI,
|
||||||
SUGGESTION_PROJECTION,
|
SUGGESTION_PROJECTION,
|
||||||
null,
|
null,
|
||||||
null,
|
arrayOfNulls(1),
|
||||||
null
|
null
|
||||||
)?.use { cursor -> cursor.count } ?: 0
|
)?.use { cursor -> cursor.count } ?: 0
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,12 +23,12 @@ object CacheUtils {
|
|||||||
|
|
||||||
@WorkerThread
|
@WorkerThread
|
||||||
fun computeCacheSize(context: Context, name: String) = getCacheDirs(context)
|
fun computeCacheSize(context: Context, name: String) = getCacheDirs(context)
|
||||||
.map { it.sub(name) }
|
.map { File(it, name) }
|
||||||
.sumOf { x -> x.computeSize() }
|
.sumOf { x -> x.computeSize() }
|
||||||
|
|
||||||
@WorkerThread
|
@WorkerThread
|
||||||
fun clearCache(context: Context, name: String) = getCacheDirs(context)
|
fun clearCache(context: Context, name: String) = getCacheDirs(context)
|
||||||
.map { it.sub(name) }
|
.map { File(it, name) }
|
||||||
.forEach { it.deleteRecursively() }
|
.forEach { it.deleteRecursively() }
|
||||||
|
|
||||||
// FIXME need async implementation
|
// FIXME need async implementation
|
||||||
|
|||||||
@@ -13,8 +13,13 @@ import java.util.zip.ZipEntry
|
|||||||
import java.util.zip.ZipFile
|
import java.util.zip.ZipFile
|
||||||
|
|
||||||
@Suppress("NOTHING_TO_INLINE")
|
@Suppress("NOTHING_TO_INLINE")
|
||||||
|
@Deprecated("Useless", ReplaceWith("File(this, name)", "java.io.File"))
|
||||||
inline fun File.sub(name: String) = File(this, name)
|
inline fun File.sub(name: String) = File(this, name)
|
||||||
|
|
||||||
|
fun File.subdir(name: String) = File(this, name).also {
|
||||||
|
if (!it.exists()) it.mkdirs()
|
||||||
|
}
|
||||||
|
|
||||||
fun File.takeIfReadable() = takeIf { it.exists() && it.canRead() }
|
fun File.takeIfReadable() = takeIf { it.exists() && it.canRead() }
|
||||||
|
|
||||||
fun ZipFile.readText(entry: ZipEntry) = getInputStream(entry).bufferedReader().use {
|
fun ZipFile.readText(entry: ZipEntry) = getInputStream(entry).bufferedReader().use {
|
||||||
|
|||||||
Reference in New Issue
Block a user