优化预下载,防止同时下载太多卡顿

pull/416/head
gedoor 4 years ago
parent 9d9fbb83d5
commit 91a1608298
  1. 11
      app/src/main/java/io/legado/app/service/help/CacheBook.kt
  2. 14
      app/src/main/java/io/legado/app/ui/main/MainViewModel.kt

@ -54,6 +54,14 @@ object CacheBook {
} }
} }
fun downloadCount(): Int {
var count = 0
downloadMap.forEach {
count += it.value.size
}
return count
}
fun download( fun download(
webBook: WebBook, webBook: WebBook,
book: Book, book: Book,
@ -98,6 +106,9 @@ object CacheBook {
} }
}.onFinally { }.onFinally {
downloadMap[book.bookUrl]?.remove(chapter.index) downloadMap[book.bookUrl]?.remove(chapter.index)
if (downloadMap[book.bookUrl].isNullOrEmpty()) {
downloadMap.remove(book.bookUrl)
}
ReadBook.removeLoading(chapter.index) ReadBook.removeLoading(chapter.index)
} }
} }

@ -8,6 +8,7 @@ import io.legado.app.constant.EventBus
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.data.entities.RssSource import io.legado.app.data.entities.RssSource
import io.legado.app.help.AppConfig import io.legado.app.help.AppConfig
import io.legado.app.help.BookHelp
import io.legado.app.help.DefaultValueHelp import io.legado.app.help.DefaultValueHelp
import io.legado.app.help.http.HttpHelper import io.legado.app.help.http.HttpHelper
import io.legado.app.help.storage.Restore import io.legado.app.help.storage.Restore
@ -19,6 +20,7 @@ import io.legado.app.utils.fromJsonObject
import io.legado.app.utils.postEvent import io.legado.app.utils.postEvent
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.asCoroutineDispatcher import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.delay
import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.CopyOnWriteArraySet import java.util.concurrent.CopyOnWriteArraySet
import java.util.concurrent.Executors import java.util.concurrent.Executors
@ -109,11 +111,23 @@ class MainViewModel(application: Application) : BaseViewModel(application) {
} }
private fun cacheBook(webBook: WebBook, book: Book) { private fun cacheBook(webBook: WebBook, book: Book) {
execute {
if (book.totalChapterNum > book.durChapterIndex) { if (book.totalChapterNum > book.durChapterIndex) {
val downloadToIndex = min(book.totalChapterNum, book.durChapterIndex.plus(10)) val downloadToIndex = min(book.totalChapterNum, book.durChapterIndex.plus(10))
for (i in book.durChapterIndex until downloadToIndex) { for (i in book.durChapterIndex until downloadToIndex) {
App.db.bookChapterDao().getChapter(book.bookUrl, i)?.let { chapter -> App.db.bookChapterDao().getChapter(book.bookUrl, i)?.let { chapter ->
if (!BookHelp.hasContent(book, chapter)) {
var addToCache = false
while (!addToCache) {
if (CacheBook.downloadCount() < 5) {
CacheBook.download(webBook, book, chapter) CacheBook.download(webBook, book, chapter)
addToCache = true
} else {
delay(1000)
}
}
}
}
} }
} }
} }

Loading…
Cancel
Save