Fix loading local manga in some corner cases

This commit is contained in:
Koitharu
2025-10-11 14:28:42 +03:00
parent 8995762935
commit c557a51c4d
3 changed files with 20 additions and 7 deletions

View File

@@ -105,7 +105,14 @@ class PagesViewModel @Inject constructor(
chaptersLoader.peekChapter(it) != null
} ?: state.details.allChapters.firstOrNull()?.id ?: return
if (!chaptersLoader.hasPages(initialChapterId)) {
chaptersLoader.loadSingleChapter(initialChapterId)
var hasPages = chaptersLoader.loadSingleChapter(initialChapterId)
while (!hasPages) {
if (chaptersLoader.loadPrevNextChapter(state.details, initialChapterId, isNext = true)) {
hasPages = chaptersLoader.snapshot().isNotEmpty()
} else {
break
}
}
}
updateList(state.readerState)
}

View File

@@ -61,7 +61,9 @@ class LocalMangaParser(private val uri: Uri) {
val index = MangaIndex.read(fileSystem, rootPath / ENTRY_NAME_INDEX)
val mangaInfo = index?.getMangaInfo()
if (mangaInfo != null) {
val coverEntry: Path? = index.getCoverEntry()?.let { rootPath / it }
val coverEntry: Path? = index.getCoverEntry()?.let { rootPath / it }?.takeIf {
fileSystem.exists(it)
}
mangaInfo.copy(
source = LocalMangaSource,
url = rootFile.toUri().toString(),

View File

@@ -1,6 +1,7 @@
package org.koitharu.kotatsu.reader.domain
import android.util.LongSparseArray
import androidx.annotation.CheckResult
import dagger.hilt.android.scopes.ViewModelScoped
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
@@ -32,12 +33,12 @@ class ChaptersLoader @Inject constructor(
}
}
suspend fun loadPrevNextChapter(manga: MangaDetails, currentId: Long, isNext: Boolean) {
suspend fun loadPrevNextChapter(manga: MangaDetails, currentId: Long, isNext: Boolean): Boolean {
val chapters = manga.allChapters
val predicate: (MangaChapter) -> Boolean = { it.id == currentId }
val index = if (isNext) chapters.indexOfFirst(predicate) else chapters.indexOfLast(predicate)
if (index == -1) return
val newChapter = chapters.getOrNull(if (isNext) index + 1 else index - 1) ?: return
if (index == -1) return false
val newChapter = chapters.getOrNull(if (isNext) index + 1 else index - 1) ?: return false
val newPages = loadChapter(newChapter.id)
mutex.withLock {
if (chapterPages.chaptersSize > 1) {
@@ -56,13 +57,16 @@ class ChaptersLoader @Inject constructor(
chapterPages.addFirst(newChapter.id, newPages)
}
}
return true
}
suspend fun loadSingleChapter(chapterId: Long) {
@CheckResult
suspend fun loadSingleChapter(chapterId: Long): Boolean {
val pages = loadChapter(chapterId)
mutex.withLock {
return mutex.withLock {
chapterPages.clear()
chapterPages.addLast(chapterId, pages)
pages.isNotEmpty()
}
}