Reader cache + retry improvements

This commit is contained in:
NerdNumber9 2018-07-17 22:29:49 -04:00
parent 1fd105ac56
commit 2fd4204db8
6 changed files with 103 additions and 23 deletions

View File

@ -6,12 +6,16 @@ import com.github.salomonbrys.kotson.fromJson
import com.google.gson.Gson
import com.jakewharton.disklrucache.DiskLruCache
import eu.kanade.tachiyomi.data.database.models.Chapter
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.preference.getOrDefault
import eu.kanade.tachiyomi.source.model.Page
import eu.kanade.tachiyomi.util.DiskUtil
import eu.kanade.tachiyomi.util.saveTo
import okhttp3.Response
import okio.Okio
import rx.Observable
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import uy.kohesive.injekt.injectLazy
import java.io.File
import java.io.IOException
@ -26,7 +30,6 @@ import java.io.IOException
* @constructor creates an instance of the chapter cache.
*/
class ChapterCache(private val context: Context) {
companion object {
/** Name of cache directory. */
const val PARAMETER_CACHE_DIRECTORY = "chapter_disk_cache"
@ -36,19 +39,27 @@ class ChapterCache(private val context: Context) {
/** The number of values per cache entry. Must be positive. */
const val PARAMETER_VALUE_COUNT = 1
/** The maximum number of bytes this cache should use to store. */
const val PARAMETER_CACHE_SIZE = 75L * 1024 * 1024
}
/** Google Json class used for parsing JSON files. */
private val gson: Gson by injectLazy()
// --> EH
private val prefs: PreferencesHelper by injectLazy()
// <-- EH
/** Cache class used for cache management. */
private val diskCache = DiskLruCache.open(File(context.cacheDir, PARAMETER_CACHE_DIRECTORY),
PARAMETER_APP_VERSION,
PARAMETER_VALUE_COUNT,
PARAMETER_CACHE_SIZE)
// --> EH
private var diskCache = setupDiskCache(prefs.eh_cacheSize().getOrDefault().toLong())
init {
prefs.eh_cacheSize().asObservable().subscribe {
// Save old cache for destruction later
val oldCache = diskCache
diskCache = setupDiskCache(it.toLong())
oldCache.close()
}
}
// <-- EH
/**
* Returns directory of cache.
@ -68,6 +79,16 @@ class ChapterCache(private val context: Context) {
val readableSize: String
get() = Formatter.formatFileSize(context, realSize)
// --> EH
// Cache size is in MB
private fun setupDiskCache(cacheSize: Long): DiskLruCache {
return DiskLruCache.open(File(context.cacheDir, PARAMETER_CACHE_DIRECTORY),
PARAMETER_APP_VERSION,
PARAMETER_VALUE_COUNT,
cacheSize * 1024 * 1024)
}
// <-- EH
/**
* Remove file from cache.
*

View File

@ -174,4 +174,6 @@ object PreferenceKeys {
const val eh_readerInstantRetry = "eh_reader_instant_retry"
const val eh_utilAutoscrollInterval = "eh_util_autoscroll_interval"
const val eh_cacheSize = "eh_cache_size"
}

View File

@ -245,4 +245,6 @@ class PreferencesHelper(val context: Context) {
fun eh_readerInstantRetry() = rxPrefs.getBoolean(Keys.eh_readerInstantRetry, true)
fun eh_utilAutoscrollInterval() = rxPrefs.getFloat(Keys.eh_utilAutoscrollInterval, 3f)
fun eh_cacheSize() = rxPrefs.getString(Keys.eh_utilAutoscrollInterval, "75")
}

View File

@ -14,6 +14,7 @@ import android.view.animation.Animation
import android.view.animation.AnimationUtils
import android.widget.SeekBar
import com.afollestad.materialdialogs.MaterialDialog
import com.hippo.unifile.UniFile
import com.jakewharton.rxbinding.view.clicks
import com.jakewharton.rxbinding.widget.checkedChanges
import com.jakewharton.rxbinding.widget.textChanges
@ -206,23 +207,35 @@ class ReaderActivity : BaseRxActivity<ReaderPresenter>() {
subscriptions += eh_retry_all.clicks().subscribe {
var retried = 0
viewer?.pages?.forEachIndexed { index, page ->
if(page.status == Page.ERROR)
page.status = Page.QUEUE
else
return@forEachIndexed
viewer?.chapters
?.flatMap { it.pages ?: emptyList() }
?.forEachIndexed { index, page ->
var shouldQueuePage = false
if(page.status == Page.ERROR) {
shouldQueuePage = true
} else if (page.uri == null) {
shouldQueuePage = true
} else if (!UniFile.fromUri(this, page.uri).exists()) {
shouldQueuePage = true
}
//If we are using EHentai/ExHentai, get a new image URL
if(presenter.source is EHentai)
page.imageUrl = null
if(shouldQueuePage) {
page.status = Page.QUEUE
} else {
return@forEachIndexed
}
if(viewer?.currentPage == index)
presenter.loader.loadPriorizedPage(page)
else
presenter.loader.loadPage(page)
//If we are using EHentai/ExHentai, get a new image URL
if(presenter.source is EHentai)
page.imageUrl = null
retried++
}
if(viewer?.currentPage == index)
presenter.loader.loadPriorizedPage(page)
else
presenter.loader.loadPage(page)
retried++
}
toast("Retrying $retried failed pages...")
}

View File

@ -34,7 +34,7 @@ abstract class BaseReader : Fragment() {
/**
* List of chapters added in the reader.
*/
private val chapters = ArrayList<ReaderChapter>()
val chapters = ArrayList<ReaderChapter>()
/**
* List of pages added in the reader. It can contain pages from more than one chapter.

View File

@ -99,6 +99,48 @@ class SettingsReaderController : SettingsController() {
summary = "Normally, pressing the retry button on a failed download will wait until the downloader has finished downloading the last page before beginning to re-download the failed page. Enabling this will force the downloader to begin re-downloading the failed page as soon as you press the retry button."
defaultValue = true
}
listPreference {
key = Keys.eh_cacheSize
title = "Reader cache size"
entryValues = arrayOf(
"50",
"75",
"100",
"150",
"250",
"500",
"750",
"1000",
"1500",
"2000",
"2500",
"3000",
"3500",
"4000",
"4500",
"5000"
)
entries = arrayOf(
"50 MB",
"75 MB",
"100 MB",
"150 MB",
"250 MB",
"500 MB",
"750 MB",
"1 GB",
"1.5 GB",
"2 GB",
"2.5 GB",
"3 GB",
"3.5 GB",
"4 GB",
"4.5 GB",
"5 GB"
)
defaultValue = "75"
summary = "The amount of images to save on device while reading. Higher values will result in a smoother reading experience, at the cost of higher disk space usage"
}
preferenceCategory {
titleRes = R.string.pager_viewer