Przeglądaj źródła

Merge remote-tracking branch 'origin/master'

kunfei 4 lat temu
rodzic
commit
fa32c8a4cd

+ 0 - 15
app/src/main/java/io/legado/app/model/CacheBook.kt

@@ -16,11 +16,8 @@ import io.legado.app.service.CacheBookService
 import io.legado.app.utils.postEvent
 import io.legado.app.utils.postEvent
 
 
 import io.legado.app.utils.startService
 import io.legado.app.utils.startService
-import io.legado.app.utils.toastOnUi
 import kotlinx.coroutines.CoroutineScope
 import kotlinx.coroutines.CoroutineScope
 import kotlinx.coroutines.delay
 import kotlinx.coroutines.delay
-import kotlinx.coroutines.runBlocking
-import splitties.init.appCtx
 
 
 import java.util.concurrent.ConcurrentHashMap
 import java.util.concurrent.ConcurrentHashMap
 import kotlin.coroutines.CoroutineContext
 import kotlin.coroutines.CoroutineContext
@@ -177,18 +174,6 @@ object CacheBook {
                 }
                 }
             }
             }
             postEvent(EventBus.UP_DOWNLOAD, book.bookUrl)
             postEvent(EventBus.UP_DOWNLOAD, book.bookUrl)
-            val chapterCount = appDb.bookChapterDao.getChapterCount(book.bookUrl)
-            if (chapterCount == 0) {
-                runBlocking {
-                    WebBook.getChapterListAwait(bookSource, book)
-                        .onFailure {
-                            AppLog.put("缓存书籍没有目录且加载目录失败\n${it.localizedMessage}", it)
-                            appCtx.toastOnUi("缓存书籍没有目录且加载目录失败\n${it.localizedMessage}")
-                        }.getOrNull()
-                }?.let { toc ->
-                    appDb.bookChapterDao.insert(*toc.toTypedArray())
-                }
-            }
         }
         }
 
 
         @Synchronized
         @Synchronized

+ 21 - 1
app/src/main/java/io/legado/app/service/CacheBookService.kt

@@ -5,15 +5,20 @@ import androidx.core.app.NotificationCompat
 import io.legado.app.R
 import io.legado.app.R
 import io.legado.app.base.BaseService
 import io.legado.app.base.BaseService
 import io.legado.app.constant.AppConst
 import io.legado.app.constant.AppConst
+import io.legado.app.constant.AppLog
 import io.legado.app.constant.EventBus
 import io.legado.app.constant.EventBus
 import io.legado.app.constant.IntentAction
 import io.legado.app.constant.IntentAction
+import io.legado.app.data.appDb
 import io.legado.app.help.config.AppConfig
 import io.legado.app.help.config.AppConfig
 import io.legado.app.model.CacheBook
 import io.legado.app.model.CacheBook
+import io.legado.app.model.webBook.WebBook
 import io.legado.app.ui.book.cache.CacheActivity
 import io.legado.app.ui.book.cache.CacheActivity
 import io.legado.app.utils.activityPendingIntent
 import io.legado.app.utils.activityPendingIntent
 import io.legado.app.utils.postEvent
 import io.legado.app.utils.postEvent
 import io.legado.app.utils.servicePendingIntent
 import io.legado.app.utils.servicePendingIntent
+import io.legado.app.utils.toastOnUi
 import kotlinx.coroutines.*
 import kotlinx.coroutines.*
+import splitties.init.appCtx
 import java.util.concurrent.Executors
 import java.util.concurrent.Executors
 import kotlin.math.min
 import kotlin.math.min
 
 
@@ -84,7 +89,22 @@ class CacheBookService : BaseService() {
         bookUrl ?: return
         bookUrl ?: return
         execute {
         execute {
             val cacheBook = CacheBook.getOrCreate(bookUrl) ?: return@execute
             val cacheBook = CacheBook.getOrCreate(bookUrl) ?: return@execute
-            cacheBook.addDownload(start, end)
+            val chapterCount = appDb.bookChapterDao.getChapterCount(bookUrl)
+            if (chapterCount == 0) {
+                WebBook.getChapterListAwait(cacheBook.bookSource, cacheBook.book)
+                    .onFailure {
+                        AppLog.put("缓存书籍没有目录且加载目录失败\n${it.localizedMessage}", it)
+                        appCtx.toastOnUi("缓存书籍没有目录且加载目录失败\n${it.localizedMessage}")
+                    }.getOrNull()?.let { toc ->
+                        appDb.bookChapterDao.insert(*toc.toTypedArray())
+                    }
+            }
+            val end2 = if (end == 0) {
+                appDb.bookChapterDao.getChapterCount(bookUrl)
+            } else {
+                end
+            }
+            cacheBook.addDownload(start, end2)
             upNotification(CacheBook.downloadSummary)
             upNotification(CacheBook.downloadSummary)
             if (downloadJob == null) {
             if (downloadJob == null) {
                 download()
                 download()