优化下载

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

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

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

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

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

Loading…
Cancel
Save