Fix loading local manga in some corner cases

devel
Koitharu 7 months ago
parent 8995762935
commit c557a51c4d
Signed by: Koitharu
GPG Key ID: 676DEE768C17A9D7

@ -105,7 +105,14 @@ class PagesViewModel @Inject constructor(
chaptersLoader.peekChapter(it) != null chaptersLoader.peekChapter(it) != null
} ?: state.details.allChapters.firstOrNull()?.id ?: return } ?: state.details.allChapters.firstOrNull()?.id ?: return
if (!chaptersLoader.hasPages(initialChapterId)) { 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) updateList(state.readerState)
} }

@ -61,7 +61,9 @@ class LocalMangaParser(private val uri: Uri) {
val index = MangaIndex.read(fileSystem, rootPath / ENTRY_NAME_INDEX) val index = MangaIndex.read(fileSystem, rootPath / ENTRY_NAME_INDEX)
val mangaInfo = index?.getMangaInfo() val mangaInfo = index?.getMangaInfo()
if (mangaInfo != null) { 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( mangaInfo.copy(
source = LocalMangaSource, source = LocalMangaSource,
url = rootFile.toUri().toString(), url = rootFile.toUri().toString(),

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

Loading…
Cancel
Save