refactor: Some code refactoring

This commit is contained in:
Ahmad Ansori Palembani 2024-06-12 08:53:23 +07:00
parent 3b2fa5d542
commit c23588881f
Signed by: null2264
GPG key ID: BA64F8B60AF3EFB6
2 changed files with 30 additions and 30 deletions

View file

@ -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()

View file

@ -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<PriorityPage>()
if (page.status == Page.State.QUEUE) {
queuedPages += PriorityPage(page, 1).also { queue.offer(it) }
}
queuedPages += preloadNextPages(page, preloadSize)
val queuedPages = mutableListOf<PriorityPage>()
if (page.status == Page.State.QUEUE) {
queuedPages += PriorityPage(page, 1).also { queue.offer(it) }
}
queuedPages += preloadNextPages(page, preloadSize)
suspendCancellableCoroutine<Nothing> { continuation ->
continuation.invokeOnCancellation {
queuedPages.forEach {
if (it.page.status == Page.State.QUEUE) {
queue.remove(it)
}
suspendCancellableCoroutine<Nothing> { continuation ->
continuation.invokeOnCancellation {
queuedPages.forEach {
if (it.page.status == Page.State.QUEUE) {
queue.remove(it)
}
}
}