优化下载

pull/1298/head
gedoor 3 years ago
parent bf0e4c6ef1
commit 350f402b3a
  1. 17
      app/src/main/java/io/legado/app/model/CacheBook.kt
  2. 2
      app/src/main/java/io/legado/app/model/ReadBook.kt
  3. 8
      app/src/main/java/io/legado/app/service/CacheBookService.kt
  4. 4
      app/src/main/java/io/legado/app/ui/book/cache/CacheAdapter.kt
  5. 2
      app/src/main/java/io/legado/app/ui/main/MainViewModel.kt

@ -20,7 +20,7 @@ import java.util.*
import java.util.concurrent.ConcurrentHashMap
import kotlin.coroutines.CoroutineContext
class CacheBook(val bookSource: BookSource, val book: Book) {
class CacheBook(var bookSource: BookSource, var book: Book) {
companion object {
@ -31,22 +31,28 @@ class CacheBook(val bookSource: BookSource, val book: Book) {
private val logTimeFormat = SimpleDateFormat("[mm:ss.SSS]", Locale.getDefault())
@Synchronized
fun get(bookUrl: String): CacheBook? {
fun getOrCreate(bookUrl: String): CacheBook? {
val book = appDb.bookDao.getBook(bookUrl) ?: return null
val bookSource = appDb.bookSourceDao.getBookSource(book.origin) ?: return null
var cacheBook = cacheBookMap[bookUrl]
if (cacheBook != null) {
//存在时更新,书源可能会变化,必须更新
cacheBook.bookSource = bookSource
cacheBook.book = book
return cacheBook
}
val book = appDb.bookDao.getBook(bookUrl) ?: return null
val bookSource = appDb.bookSourceDao.getBookSource(book.origin) ?: return null
cacheBook = CacheBook(bookSource, book)
cacheBookMap[bookUrl] = cacheBook
return cacheBook
}
@Synchronized
fun get(bookSource: BookSource, book: Book): CacheBook {
fun getOrCreate(bookSource: BookSource, book: Book): CacheBook {
var cacheBook = cacheBookMap[book.bookUrl]
if (cacheBook != null) {
//存在时更新,书源可能会变化,必须更新
cacheBook.bookSource = bookSource
cacheBook.book = book
return cacheBook
}
cacheBook = CacheBook(bookSource, book)
@ -191,6 +197,7 @@ class CacheBook(val bookSource: BookSource, val book: Book) {
}.onFinally {
if (waitDownloadSet.isEmpty() && onDownloadSet.isEmpty()) {
postEvent(EventBus.UP_DOWNLOAD, "")
cacheBookMap.remove(book.bookUrl)
}
}
return true

@ -296,7 +296,7 @@ object ReadBook : CoroutineScope by MainScope() {
val book = book
val bookSource = bookSource
if (book != null && bookSource != null) {
CacheBook.get(bookSource, book).download(scope, chapter)
CacheBook.getOrCreate(bookSource, book).download(scope, chapter)
} else if (book != null) {
contentLoadFinish(
book, chapter, "没有书源", resetPageOffset = resetPageOffset

@ -73,12 +73,14 @@ class CacheBookService : BaseService() {
private fun addDownloadData(bookUrl: String?, start: Int, end: Int) {
bookUrl ?: return
val cacheBook = CacheBook.get(bookUrl) ?: return
execute {
val cacheBook = CacheBook.getOrCreate(bookUrl) ?: return@execute
cacheBook.addDownload(start, end)
if (downloadJob == null) {
download()
}
}
}
private fun removeDownload(bookUrl: String?) {
CacheBook.cacheBookMap.remove(bookUrl)
@ -90,6 +92,10 @@ class CacheBookService : BaseService() {
private fun download() {
downloadJob = launch(cachePool) {
while (isActive) {
if (CacheBook.cacheBookMap.isEmpty()) {
CacheBook.stop(this@CacheBookService)
return@launch
}
CacheBook.cacheBookMap.forEach {
while (CacheBook.onDownloadCount > threadCount) {
delay(100)

@ -50,7 +50,7 @@ class CacheAdapter(context: Context, private val callBack: CallBack) :
binding.run {
ivDownload.setOnClickListener {
getItem(holder.layoutPosition)?.let { book ->
CacheBook.get(book.bookUrl)?.let {
CacheBook.cacheBookMap[book.bookUrl]?.let {
if (it.isRun()) {
CacheBook.remove(context, book.bookUrl)
} else {
@ -68,7 +68,7 @@ class CacheAdapter(context: Context, private val callBack: CallBack) :
}
private fun upDownloadIv(iv: ImageView, book: Book) {
CacheBook.get(book.bookUrl)?.let {
CacheBook.cacheBookMap[book.bookUrl]?.let {
if (it.isRun()) {
iv.setImageResource(R.drawable.ic_stop_black_24dp)
} else {

@ -118,7 +118,7 @@ class MainViewModel(application: Application) : BaseViewModel(application) {
if (!BookHelp.hasContent(book, chapter)) {
var addToCache = false
while (!addToCache) {
val cacheBook = CacheBook.get(bookSource, book)
val cacheBook = CacheBook.getOrCreate(bookSource, book)
if (CacheBook.onDownloadCount < 10) {
cacheBook.download(this, chapter)
addToCache = true

Loading…
Cancel
Save