Fix preload in reader (#1517)
* Fix preload in reader * Reduce preload page count for ArchivePageLoader
This commit is contained in:
1 parent
b73704ede1
commit
03ab435401
4 files changed
+37
-18
No files matched your search
@@ -91,13 +91,17 @@ class ArchivePageLoader(
|
||||
|
||||
override fun onRequest(index: Int) {
|
||||
synchronized(mJobMap) {
|
||||
val current = mJobMap[index]
|
||||
if (current?.isActive != true) {
|
||||
mJobMap[index] = launch {
|
||||
mWorkerMutex.withLock(index) {
|
||||
mSemaphore.withPermit {
|
||||
doRealWork(index)
|
||||
}
|
||||
enqueue(index)
|
||||
}
|
||||
}
|
||||
|
||||
private fun enqueue(index: Int) {
|
||||
val current = mJobMap[index]
|
||||
if (current?.isActive != true) {
|
||||
mJobMap[index] = launch {
|
||||
mWorkerMutex.withLock(index) {
|
||||
mSemaphore.withPermit {
|
||||
doRealWork(index)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -161,7 +165,19 @@ class ArchivePageLoader(
|
||||
false
|
||||
}
|
||||
|
||||
override fun preloadPages(pages: List<Int>, pair: Pair<Int, Int>) {}
|
||||
override val preloadPageCount = 3
|
||||
|
||||
override fun preloadPages(pages: List<Int>, bounds: Pair<Int, Int>) {
|
||||
synchronized(mJobMap) {
|
||||
val (min, max) = bounds
|
||||
mJobMap.forEach { (i, job) ->
|
||||
if (i < min || i > max) {
|
||||
job.cancel()
|
||||
}
|
||||
}
|
||||
pages.forEach(::enqueue)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private const val DEBUG_TAG = "ArchivePageLoader"
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package com.hippo.ehviewer.gallery
|
||||
|
||||
import com.hippo.ehviewer.Settings
|
||||
import com.hippo.ehviewer.client.EhUtils
|
||||
import com.hippo.ehviewer.client.data.GalleryInfo
|
||||
import com.hippo.ehviewer.image.Image
|
||||
@@ -105,7 +106,9 @@ class EhPageLoader(private val mGalleryInfo: GalleryInfo, startPage: Int) :
|
||||
notifyPageFailed(index, error)
|
||||
}
|
||||
|
||||
override fun preloadPages(pages: List<Int>, pair: Pair<Int, Int>) {
|
||||
mSpiderQueen.preloadPages(pages, pair)
|
||||
override val preloadPageCount = Settings.preloadImage
|
||||
|
||||
override fun preloadPages(pages: List<Int>, bounds: Pair<Int, Int>) {
|
||||
mSpiderQueen.preloadPages(pages, bounds)
|
||||
}
|
||||
}
|
||||
@@ -45,13 +45,13 @@ fun PagerItem(
|
||||
contentScale: ContentScale,
|
||||
modifier: Modifier = Modifier,
|
||||
) {
|
||||
LaunchedEffect(Unit) {
|
||||
pageLoader.request(page.index)
|
||||
}
|
||||
val defaultError = stringResource(id = R.string.decode_image_error)
|
||||
val state by page.status.collectAsState()
|
||||
when (state) {
|
||||
Page.State.QUEUE, Page.State.LOAD_PAGE, Page.State.DOWNLOAD_IMAGE -> {
|
||||
LaunchedEffect(Unit) {
|
||||
pageLoader.request(page.index)
|
||||
}
|
||||
Box(
|
||||
modifier = Modifier.fillMaxWidth().aspectRatio(DEFAULT_ASPECT),
|
||||
contentAlignment = Alignment.Center,
|
||||
|
||||
@@ -33,7 +33,7 @@ abstract class PageLoader {
|
||||
(0 until size).map { ReaderPage(it) }
|
||||
}
|
||||
|
||||
private val preloads = com.hippo.ehviewer.Settings.preloadImage.coerceIn(0, 100)
|
||||
abstract val preloadPageCount: Int
|
||||
|
||||
@CallSuper
|
||||
open suspend fun awaitReady(): Boolean {
|
||||
@@ -66,11 +66,11 @@ abstract class PageLoader {
|
||||
onRequest(index)
|
||||
}
|
||||
|
||||
val pagesAbsent = ((index - 5).coerceAtLeast(0) until (preloads + index).coerceAtMost(size))
|
||||
.mapNotNullTo(mutableListOf()) { it.takeIf { it != index && cache[it] == null } }
|
||||
val preloadRange = (index - 3).coerceAtLeast(0) until (index + preloadPageCount).coerceAtMost(size)
|
||||
val pagesAbsent = preloadRange.mapNotNullTo(mutableListOf()) { it.takeIf { it != index && cache[it] == null } }
|
||||
// Load forward first, then load backward from the nearest index
|
||||
pagesAbsent.sortBy { (index - it).coerceAtLeast(0) }
|
||||
preloadPages(pagesAbsent, (index - 10).coerceAtLeast(0) to (preloads + index + 10).coerceAtMost(size))
|
||||
preloadPages(pagesAbsent, (preloadRange.first - 5).coerceAtLeast(0) to (preloadRange.last + 10).coerceAtMost(size))
|
||||
}
|
||||
|
||||
fun retryPage(index: Int, orgImg: Boolean = false) {
|
||||
@@ -78,7 +78,7 @@ abstract class PageLoader {
|
||||
onForceRequest(index, orgImg)
|
||||
}
|
||||
|
||||
protected abstract fun preloadPages(pages: List<Int>, pair: Pair<Int, Int>)
|
||||
protected abstract fun preloadPages(pages: List<Int>, bounds: Pair<Int, Int>)
|
||||
|
||||
protected abstract fun onRequest(index: Int)
|
||||
|
||||
|
||||
Reference in new issue
Block a user