Просмотр исходного кода

fix: prevent manga chapter navigation rollback

HapeLee 3 месяцев назад
Родитель
Сommit
71072e8dae

+ 2 - 1
app/src/main/java/io/legado/app/data/repository/MangaReaderSessionRepository.kt

@@ -362,9 +362,10 @@ class MangaReaderSessionRepository(
         val currentBook = ReadManga.book ?: return@withContext
         database.bookDao.getBook(currentBook.bookUrl)?.let(ReadManga::upData)
         if (index !in 0 until ReadManga.simulatedChapterSize) return@withContext
-        ReadManga.showLoading()
         ReadManga.durChapterIndex = index
         ReadManga.durChapterPos = pageIndex.coerceAtLeast(0)
+        ReadManga.clearMangaChapter()
+        ReadManga.showLoading()
         ReadManga.loadContent()
         ReadManga.saveRead(pageChanged = true)
     }

+ 3 - 0
app/src/main/java/io/legado/app/ui/book/manga/MangaReaderInteraction.kt

@@ -27,3 +27,6 @@ internal fun mangaPageStepTarget(
     val target = (currentIndex + direction).coerceIn(0, itemCount - 1)
     return target.takeIf { it != currentIndex }
 }
+
+internal fun shouldExposeMangaPages(currentChapterFinished: Boolean): Boolean =
+    currentChapterFinished

+ 21 - 1
app/src/main/java/io/legado/app/ui/book/manga/MangaReaderViewModel.kt

@@ -305,6 +305,17 @@ class MangaReaderViewModel(
         refreshContentJob?.cancel()
         refreshContentJob = viewModelScope.launch {
             val content = session.content
+            if (!shouldExposeMangaPages(content.currentFinished)) {
+                _uiState.update {
+                    it.copy(
+                        isLoading = true,
+                        pages = persistentListOf(),
+                        currentItemIndex = 0,
+                        scrollRequest = null,
+                    )
+                }
+                return@launch
+            }
             val items = content.items.mapIndexedNotNull { index, item ->
                 when (item) {
                     is MangaPage -> MangaReaderItemUi.Page(
@@ -361,7 +372,15 @@ class MangaReaderViewModel(
     }
 
     fun showLoading() {
-        _uiState.update { it.copy(isLoading = true, errorMessage = null) }
+        _uiState.update {
+            it.copy(
+                isLoading = true,
+                errorMessage = null,
+                pages = persistentListOf(),
+                currentItemIndex = 0,
+                scrollRequest = null,
+            )
+        }
     }
 
     fun showError(message: String) {
@@ -659,6 +678,7 @@ class MangaReaderViewModel(
 
     private fun updateVisibleItem(itemIndex: Int) {
         val state = _uiState.value
+        if (state.isLoading) return
         val item = state.pages.getOrNull(itemIndex) as? MangaReaderItemUi.Page ?: return
         sessionRepository.setVisiblePage(item.chapterIndex, item.pageIndex)
         _uiState.update {

+ 8 - 0
app/src/test/java/io/legado/app/ui/book/manga/MangaReaderInteractionTest.kt

@@ -2,6 +2,8 @@ package io.legado.app.ui.book.manga
 
 import org.junit.Assert.assertEquals
 import org.junit.Assert.assertNull
+import org.junit.Assert.assertFalse
+import org.junit.Assert.assertTrue
 import org.junit.Test
 
 class MangaReaderInteractionTest {
@@ -53,4 +55,10 @@ class MangaReaderInteractionTest {
         assertNull(mangaPageStepTarget(currentIndex = 7, itemCount = 8, direction = 1))
         assertNull(mangaPageStepTarget(currentIndex = 0, itemCount = 0, direction = 1))
     }
+
+    @Test
+    fun `adjacent chapter callbacks stay hidden until target chapter finishes`() {
+        assertFalse(shouldExposeMangaPages(currentChapterFinished = false))
+        assertTrue(shouldExposeMangaPages(currentChapterFinished = true))
+    }
 }