diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/cache/ChapterCache.kt b/app/src/main/java/eu/kanade/tachiyomi/data/cache/ChapterCache.kt index 6f96d61cba..db9a3d1eea 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/cache/ChapterCache.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/cache/ChapterCache.kt @@ -2,6 +2,7 @@ package eu.kanade.tachiyomi.data.cache import android.content.Context import android.text.format.Formatter +import co.touchlab.kermit.Logger import com.jakewharton.disklrucache.DiskLruCache import eu.kanade.tachiyomi.data.database.models.Chapter import eu.kanade.tachiyomi.data.preference.PreferencesHelper @@ -106,19 +107,20 @@ class ChapterCache(private val context: Context) { * @param file name of file "md5.0". * @return status of deletion for the file. */ - fun removeFileFromCache(file: String): Boolean { + private fun removeFileFromCache(file: String): Boolean { // Make sure we don't delete the journal file (keeps track of cache). if (file == "journal" || file.startsWith("journal.")) { return false } - try { + return try { // Remove the extension from the file to get the key of the cache val key = file.substringBeforeLast(".") // Remove file from cache. - return diskCache.remove(key) + diskCache.remove(key) } catch (e: Exception) { - return false + Logger.w(e) { "Failed to remove file from cache" } + false } } @@ -166,6 +168,7 @@ class ChapterCache(private val context: Context) { editor.commit() editor.abortUnlessCommitted() } catch (e: Exception) { + Logger.w(e) { "Failed to put page list to cache" } // Ignore. } finally { editor?.abortUnlessCommitted() diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/HttpPageLoader.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/HttpPageLoader.kt index 4af8537c43..130a7fee57 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/HttpPageLoader.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/loader/HttpPageLoader.kt @@ -19,8 +19,8 @@ import kotlinx.coroutines.runInterruptible import kotlinx.coroutines.suspendCancellableCoroutine import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get -import java.util.concurrent.PriorityBlockingQueue -import java.util.concurrent.atomic.AtomicInteger +import java.util.concurrent.* +import java.util.concurrent.atomic.* import kotlin.math.min /** @@ -65,8 +65,7 @@ class HttpPageLoader( queue.clear() // Cache current page list progress for online chapters to allow a faster reopen - val pages = chapter.pages - if (pages != null) { + chapter.pages?.let { pages -> launchIO { try { // Convert to pages without reader information @@ -103,32 +102,30 @@ class HttpPageLoader( /** * Loads a page through the queue. Handles re-enqueueing pages if they were evicted from the cache. */ - override suspend fun loadPage(page: ReaderPage) { - withIOContext { - val imageUrl = page.imageUrl + override suspend fun loadPage(page: ReaderPage) = withIOContext { + val imageUrl = page.imageUrl - // Check if the image has been deleted - if (page.status == Page.State.READY && imageUrl != null && !chapterCache.isImageInCache(imageUrl)) { - page.status = Page.State.QUEUE - } + // Check if the image has been deleted + if (page.status == Page.State.READY && imageUrl != null && !chapterCache.isImageInCache(imageUrl)) { + page.status = Page.State.QUEUE + } - // Automatically retry failed pages when subscribed to this page - if (page.status == Page.State.ERROR) { - page.status = Page.State.QUEUE - } + // Automatically retry failed pages when subscribed to this page + if (page.status == Page.State.ERROR) { + page.status = Page.State.QUEUE + } - val queuedPages = mutableListOf() - if (page.status == Page.State.QUEUE) { - queuedPages += PriorityPage(page, 1).also { queue.offer(it) } - } - queuedPages += preloadNextPages(page, preloadSize) + val queuedPages = mutableListOf() + if (page.status == Page.State.QUEUE) { + queuedPages += PriorityPage(page, 1).also { queue.offer(it) } + } + queuedPages += preloadNextPages(page, preloadSize) - suspendCancellableCoroutine { continuation -> - continuation.invokeOnCancellation { - queuedPages.forEach { - if (it.page.status == Page.State.QUEUE) { - queue.remove(it) - } + suspendCancellableCoroutine { continuation -> + continuation.invokeOnCancellation { + queuedPages.forEach { + if (it.page.status == Page.State.QUEUE) { + queue.remove(it) } } }