Horis 1 год назад
Родитель
Сommit
896980e5b2

+ 27 - 91
app/src/main/java/io/legado/app/model/ReadManga.kt

@@ -61,7 +61,6 @@ object ReadManga : CoroutineScope by MainScope() {
     var preDownloadTask: Job? = null
     var preDownloadTask: Job? = null
     val downloadedChapters = hashSetOf<Int>()
     val downloadedChapters = hashSetOf<Int>()
     val downloadFailChapters = hashMapOf<Int, Int>()
     val downloadFailChapters = hashMapOf<Int, Int>()
-    private val downloadLoadingChapters = arrayListOf<Int>()
     val downloadScope = CoroutineScope(SupervisorJob() + IO)
     val downloadScope = CoroutineScope(SupervisorJob() + IO)
     var rateLimiter = ConcurrentRateLimiter(null)
     var rateLimiter = ConcurrentRateLimiter(null)
 
 
@@ -153,21 +152,6 @@ object ReadManga : CoroutineScope by MainScope() {
         }
         }
     }
     }
 
 
-    private fun addDownloadLoading(index: Int): Boolean {
-        synchronized(this) {
-            if (downloadLoadingChapters.contains(index)) return false
-            downloadLoadingChapters.add(index)
-            return true
-        }
-    }
-
-
-    fun removeDownloadLoading(index: Int) {
-        synchronized(this) {
-            downloadLoadingChapters.remove(index)
-        }
-    }
-
     fun removeLoading(index: Int) {
     fun removeLoading(index: Int) {
         synchronized(this) {
         synchronized(this) {
             loadingChapters.remove(index)
             loadingChapters.remove(index)
@@ -180,22 +164,28 @@ object ReadManga : CoroutineScope by MainScope() {
         }
         }
     }
     }
 
 
-
     /**
     /**
      * 获取正文
      * 获取正文
      */
      */
-    private suspend fun getContent(
+    private fun download(
         scope: CoroutineScope,
         scope: CoroutineScope,
         chapter: BookChapter,
         chapter: BookChapter,
     ) {
     ) {
         val book = book ?: return removeLoading(chapter.index)
         val book = book ?: return removeLoading(chapter.index)
         val bookSource = bookSource
         val bookSource = bookSource
         if (bookSource != null) {
         if (bookSource != null) {
-            getContent(bookSource, scope, chapter, book)
+            downloadNetworkContent(bookSource, scope, chapter, book, success = {
+                downloadedChapters.add(chapter.index)
+                downloadFailChapters.remove(chapter.index)
+                contentLoadFinish(chapter, it, book)
+            }, error = {
+                downloadFailChapters[chapter.index] =
+                    (downloadFailChapters[chapter.index] ?: 0) + 1
+                contentLoadFinish(chapter, null, book)
+            })
         }
         }
     }
     }
 
 
-
     fun loadContent() {
     fun loadContent() {
         loadContent(durChapterIndex)
         loadContent(durChapterIndex)
     }
     }
@@ -210,7 +200,9 @@ object ReadManga : CoroutineScope by MainScope() {
             Coroutine.async {
             Coroutine.async {
                 val book = book!!
                 val book = book!!
                 appDb.bookChapterDao.getChapter(book.bookUrl, index)?.let { chapter ->
                 appDb.bookChapterDao.getChapter(book.bookUrl, index)?.let { chapter ->
-                    getContent(
+                    BookHelp.getContent(book, chapter)?.let {
+                        contentLoadFinish(chapter, it, book)
+                    } ?: download(
                         downloadScope,
                         downloadScope,
                         chapter,
                         chapter,
                     )
                     )
@@ -220,7 +212,6 @@ object ReadManga : CoroutineScope by MainScope() {
                 AppLog.put("加载正文出错\n${it.localizedMessage}")
                 AppLog.put("加载正文出错\n${it.localizedMessage}")
             }
             }
         }
         }
-
     }
     }
 
 
 
 
@@ -253,7 +244,6 @@ object ReadManga : CoroutineScope by MainScope() {
         preDownloadTask = null
         preDownloadTask = null
         downloadedChapters.clear()
         downloadedChapters.clear()
         downloadFailChapters.clear()
         downloadFailChapters.clear()
-        downloadLoadingChapters.clear()
         downloadScope.coroutineContext.cancelChildren()
         downloadScope.coroutineContext.cancelChildren()
         coroutineContext.cancelChildren()
         coroutineContext.cancelChildren()
     }
     }
@@ -263,13 +253,18 @@ object ReadManga : CoroutineScope by MainScope() {
      */
      */
     suspend fun contentLoadFinish(
     suspend fun contentLoadFinish(
         chapter: BookChapter,
         chapter: BookChapter,
-        content: String,
+        content: String?,
         book: Book,
         book: Book,
     ) {
     ) {
         chapterTitle = chapter.title
         chapterTitle = chapter.title
+        removeLoading(chapter.index)
         if (chapter.index !in durChapterIndex - 1..durChapterIndex + 1) {
         if (chapter.index !in durChapterIndex - 1..durChapterIndex + 1) {
             return
             return
         }
         }
+        if (content == null) {
+            mCallback?.loadFail("加载内容失败")
+            return
+        }
         if (content.isNotEmpty()) {
         if (content.isNotEmpty()) {
             val list = flow {
             val list = flow {
                 val matcher = AppPattern.imgPattern.matcher(content)
                 val matcher = AppPattern.imgPattern.matcher(content)
@@ -311,6 +306,7 @@ object ReadManga : CoroutineScope by MainScope() {
             )
             )
             mCallback?.loadContentFinish(contentList)
             mCallback?.loadContentFinish(contentList)
         }
         }
+        mCallback?.loadComplete()
     }
     }
 
 
     /**
     /**
@@ -376,38 +372,6 @@ object ReadManga : CoroutineScope by MainScope() {
         }
         }
     }
     }
 
 
-    private suspend fun getContent(
-        bookSource: BookSource,
-        scope: CoroutineScope,
-        chapter: BookChapter,
-        book: Book,
-    ) {
-        if (BookHelp.hasContent(book, chapter)) {
-            BookHelp.getContent(book, chapter)?.apply {
-                contentLoadFinish(chapter, this, book)
-                mCallback?.loadComplete()
-                if (durChapterIndex >= chapterSize.minus(1)) {
-                    gameOver = true
-                    mCallback?.noData()
-                }
-            } ?: downloadNetworkContent(bookSource, scope, chapter, book, success = {
-                contentLoadFinish(chapter, it, book)
-                mCallback?.loadComplete()
-            }, error = {
-                removeLoading(chapter.index)
-                mCallback?.loadFail("加载内容失败")
-            })
-        } else {
-            downloadNetworkContent(bookSource, scope, chapter, book, success = {
-                contentLoadFinish(chapter, it, book)
-                mCallback?.loadComplete()
-            }, error = {
-                removeLoading(chapter.index)
-                mCallback?.loadFail("加载内容失败")
-            })
-        }
-    }
-
     private fun downloadNetworkContent(
     private fun downloadNetworkContent(
         bookSource: BookSource,
         bookSource: BookSource,
         scope: CoroutineScope,
         scope: CoroutineScope,
@@ -468,30 +432,17 @@ object ReadManga : CoroutineScope by MainScope() {
             return
             return
         }
         }
         val book = book ?: return
         val book = book ?: return
-        if (addDownloadLoading(index)) {
+        if (addLoading(index)) {
             try {
             try {
                 appDb.bookChapterDao.getChapter(book.bookUrl, index)?.let { chapter ->
                 appDb.bookChapterDao.getChapter(book.bookUrl, index)?.let { chapter ->
                     if (BookHelp.hasContent(book, chapter)) {
                     if (BookHelp.hasContent(book, chapter)) {
-                        removeDownloadLoading(chapter.index)
+                        removeLoading(chapter.index)
                         downloadedChapters.add(chapter.index)
                         downloadedChapters.add(chapter.index)
                     } else {
                     } else {
                         delay(1000)
                         delay(1000)
-                        downloadNetworkContent(
-                            bookSource!!,
-                            downloadScope,
-                            chapter,
-                            book,
-                            success = {
-                                downloadedChapters.add(chapter.index)
-                                downloadFailChapters.remove(chapter.index)
-                            },
-                            error = {
-                                downloadFailChapters[chapter.index] =
-                                    (downloadFailChapters[chapter.index] ?: 0) + 1
-                                removeDownloadLoading(chapter.index)
-                            })
+                        download(downloadScope, chapter)
                     }
                     }
-                } ?: removeDownloadLoading(index)
+                } ?: removeLoading(index)
             } catch (_: Exception) {
             } catch (_: Exception) {
                 removeLoading(index)
                 removeLoading(index)
             }
             }
@@ -575,26 +526,11 @@ object ReadManga : CoroutineScope by MainScope() {
             chapterChanged = true
             chapterChanged = true
             if (progress.durChapterIndex == durChapterIndex) {
             if (progress.durChapterIndex == durChapterIndex) {
                 durChapterPos = progress.durChapterPos
                 durChapterPos = progress.durChapterPos
-                mCallback?.adjustmentProgress()
+                mCallback?.adjustProgress()
             } else {
             } else {
                 durChapterIndex = progress.durChapterIndex
                 durChapterIndex = progress.durChapterIndex
                 durChapterPos = progress.durChapterPos
                 durChapterPos = progress.durChapterPos
-                if (addLoading(durChapterIndex)) {
-                    loadContent(durChapterIndex)
-                } else {
-                    Coroutine.async {
-                        val book = book!!
-                        appDb.bookChapterDao.getChapter(book.bookUrl, durChapterIndex)
-                            ?.let { chapter ->
-                                getContent(
-                                    downloadScope,
-                                    chapter,
-                                )
-                            }
-                    }.onError {
-                        AppLog.put("加载正文出错\n${it.localizedMessage}")
-                    }
-                }
+                loadContent()
             }
             }
             saveRead()
             saveRead()
         }
         }
@@ -605,7 +541,7 @@ object ReadManga : CoroutineScope by MainScope() {
         fun loadComplete()
         fun loadComplete()
         fun loadFail(msg: String)
         fun loadFail(msg: String)
         fun noData()
         fun noData()
-        fun adjustmentProgress()
+        fun adjustProgress()
         fun sureNewProgress(progress: BookProgress)
         fun sureNewProgress(progress: BookProgress)
     }
     }
 }
 }

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

@@ -390,7 +390,7 @@ class ReadMangaActivity : VMBaseActivity<ActivityMangaBinding, MangaViewModel>()
         }
         }
     }
     }
 
 
-    override fun adjustmentProgress() {
+    override fun adjustProgress() {
         if (ReadManga.chapterChanged) {
         if (ReadManga.chapterChanged) {
             binding.mRecyclerManga.scrollToPosition(ReadManga.durChapterPos)
             binding.mRecyclerManga.scrollToPosition(ReadManga.durChapterPos)
             binding.flLoading.isGone = true
             binding.flLoading.isGone = true