pull/1336/head
gedoor 3 years ago
parent 486333dcbd
commit 1b37baa805
  1. 1
      app/src/main/java/io/legado/app/service/CacheBookService.kt
  2. 47
      app/src/main/java/io/legado/app/ui/main/MainViewModel.kt

@ -99,6 +99,7 @@ class CacheBookService : BaseService() {
} }
private fun download() { private fun download() {
downloadJob?.cancel()
downloadJob = launch(cachePool) { downloadJob = launch(cachePool) {
while (isActive) { while (isActive) {
if (!CacheBook.isRun) { if (!CacheBook.isRun) {

@ -9,12 +9,13 @@ import io.legado.app.constant.BookType
import io.legado.app.constant.EventBus import io.legado.app.constant.EventBus
import io.legado.app.data.appDb import io.legado.app.data.appDb
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookSource
import io.legado.app.help.AppConfig import io.legado.app.help.AppConfig
import io.legado.app.help.BookHelp
import io.legado.app.help.DefaultData import io.legado.app.help.DefaultData
import io.legado.app.help.LocalConfig import io.legado.app.help.LocalConfig
import io.legado.app.model.CacheBook import io.legado.app.model.CacheBook
import io.legado.app.model.webBook.WebBook import io.legado.app.model.webBook.WebBook
import io.legado.app.service.CacheBookService
import io.legado.app.utils.postEvent import io.legado.app.utils.postEvent
import io.legado.app.utils.printOnDebug import io.legado.app.utils.printOnDebug
import kotlinx.coroutines.* import kotlinx.coroutines.*
@ -29,6 +30,7 @@ class MainViewModel(application: Application) : BaseViewModel(application) {
private val waitUpTocBooks = arrayListOf<String>() private val waitUpTocBooks = arrayListOf<String>()
private val onUpTocBooks = CopyOnWriteArraySet<String>() private val onUpTocBooks = CopyOnWriteArraySet<String>()
private var upTocJob: Job? = null private var upTocJob: Job? = null
private var cacheBookJob: Job? = null
override fun onCleared() { override fun onCleared() {
super.onCleared() super.onCleared()
@ -121,7 +123,7 @@ class MainViewModel(application: Application) : BaseViewModel(application) {
appDb.bookDao.update(book) appDb.bookDao.update(book)
appDb.bookChapterDao.delByBook(book.bookUrl) appDb.bookChapterDao.delByBook(book.bookUrl)
appDb.bookChapterDao.insert(*toc.toTypedArray()) appDb.bookChapterDao.insert(*toc.toTypedArray())
cacheBook(book) addDownload(source, book)
}.onError(upTocPool) { }.onError(upTocPool) {
AppLog.addLog("${book.name} 更新目录失败\n${it.localizedMessage}", it) AppLog.addLog("${book.name} 更新目录失败\n${it.localizedMessage}", it)
it.printOnDebug() it.printOnDebug()
@ -145,23 +147,44 @@ class MainViewModel(application: Application) : BaseViewModel(application) {
postEvent(EventBus.UP_BOOKSHELF, bookUrl) postEvent(EventBus.UP_BOOKSHELF, bookUrl)
} }
/** @Synchronized
* 缓存书籍 private fun addDownload(source: BookSource, book: Book) {
*/
private fun cacheBook(book: Book) {
val endIndex = min( val endIndex = min(
book.totalChapterNum - 1, book.totalChapterNum - 1,
book.durChapterIndex.plus(AppConfig.preDownloadNum) book.durChapterIndex.plus(AppConfig.preDownloadNum)
) )
for (i in book.durChapterIndex..endIndex) { val cacheBook = CacheBook.getOrCreate(source, book)
appDb.bookChapterDao.getChapter(book.bookUrl, i)?.let { chapter -> cacheBook.addDownload(book.durChapterIndex, endIndex)
if (!BookHelp.hasContent(book, chapter)) { if (cacheBookJob == null && !CacheBookService.isRun) {
CacheBook.start(context, book.bookUrl, i, endIndex) cacheBook()
return }
}
/**
* 缓存书籍
*/
private fun cacheBook() {
cacheBookJob?.cancel()
cacheBookJob = viewModelScope.launch(upTocPool) {
while (isActive) {
if (CacheBookService.isRun) {
cacheBookJob?.cancel()
cacheBookJob = null
return@launch
}
if (!CacheBook.isRun) {
cacheBookJob?.cancel()
cacheBookJob = null
return@launch
}
CacheBook.cacheBookMap.forEach {
while (CacheBook.onDownloadCount > threadCount) {
delay(100)
}
it.value.download(this, upTocPool)
} }
} }
} }
} }
fun postLoad() { fun postLoad() {

Loading…
Cancel
Save