优化
This commit is contained in:
@@ -202,25 +202,25 @@ object CacheBook {
|
|||||||
* 从待下载列表内取第一条下载
|
* 从待下载列表内取第一条下载
|
||||||
*/
|
*/
|
||||||
@Synchronized
|
@Synchronized
|
||||||
fun download(scope: CoroutineScope, context: CoroutineContext): Boolean {
|
fun download(scope: CoroutineScope, context: CoroutineContext) {
|
||||||
val chapterIndex = waitDownloadSet.firstOrNull()
|
val chapterIndex = waitDownloadSet.firstOrNull()
|
||||||
if (chapterIndex == null) {
|
if (chapterIndex == null) {
|
||||||
if (onDownloadSet.isEmpty()) {
|
if (onDownloadSet.isEmpty()) {
|
||||||
cacheBookMap.remove(book.bookUrl)
|
cacheBookMap.remove(book.bookUrl)
|
||||||
}
|
}
|
||||||
return false
|
return
|
||||||
}
|
}
|
||||||
if (onDownloadSet.contains(chapterIndex)) {
|
if (onDownloadSet.contains(chapterIndex)) {
|
||||||
waitDownloadSet.remove(chapterIndex)
|
waitDownloadSet.remove(chapterIndex)
|
||||||
return download(scope, context)
|
return
|
||||||
}
|
}
|
||||||
val chapter = appDb.bookChapterDao.getChapter(book.bookUrl, chapterIndex) ?: let {
|
val chapter = appDb.bookChapterDao.getChapter(book.bookUrl, chapterIndex) ?: let {
|
||||||
waitDownloadSet.remove(chapterIndex)
|
waitDownloadSet.remove(chapterIndex)
|
||||||
return download(scope, context)
|
return
|
||||||
}
|
}
|
||||||
if (BookHelp.hasContent(book, chapter)) {
|
if (BookHelp.hasContent(book, chapter)) {
|
||||||
waitDownloadSet.remove(chapterIndex)
|
waitDownloadSet.remove(chapterIndex)
|
||||||
return download(scope, context)
|
return
|
||||||
}
|
}
|
||||||
waitDownloadSet.remove(chapterIndex)
|
waitDownloadSet.remove(chapterIndex)
|
||||||
onDownloadSet.add(chapterIndex)
|
onDownloadSet.add(chapterIndex)
|
||||||
@@ -243,7 +243,6 @@ object CacheBook {
|
|||||||
}.onFinally {
|
}.onFinally {
|
||||||
onFinally()
|
onFinally()
|
||||||
}
|
}
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Synchronized
|
@Synchronized
|
||||||
|
|||||||
@@ -107,10 +107,14 @@ class CacheBookService : BaseService() {
|
|||||||
return@launch
|
return@launch
|
||||||
}
|
}
|
||||||
CacheBook.cacheBookMap.forEach {
|
CacheBook.cacheBookMap.forEach {
|
||||||
while (CacheBook.onDownloadCount > threadCount) {
|
val cacheBookModel = it.value
|
||||||
|
while (cacheBookModel.waitCount > 0) {
|
||||||
|
if (CacheBook.onDownloadCount < threadCount) {
|
||||||
|
cacheBookModel.download(this, cachePool)
|
||||||
|
} else {
|
||||||
delay(100)
|
delay(100)
|
||||||
}
|
}
|
||||||
it.value.download(this, cachePool)
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -195,10 +195,14 @@ class MainViewModel(application: Application) : BaseViewModel(application) {
|
|||||||
return@launch
|
return@launch
|
||||||
}
|
}
|
||||||
CacheBook.cacheBookMap.forEach {
|
CacheBook.cacheBookMap.forEach {
|
||||||
while (CacheBook.onDownloadCount > threadCount) {
|
val cacheBookModel = it.value
|
||||||
|
while (cacheBookModel.waitCount > 0) {
|
||||||
|
if (CacheBook.onDownloadCount < threadCount) {
|
||||||
|
cacheBookModel.download(this, upTocPool)
|
||||||
|
} else {
|
||||||
delay(100)
|
delay(100)
|
||||||
}
|
}
|
||||||
it.value.download(this, upTocPool)
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user