优化
This commit is contained in:
@@ -143,6 +143,10 @@ object CacheBook {
|
|||||||
val successCount get() = successDownloadSet.size
|
val successCount get() = successDownloadSet.size
|
||||||
val errorCount get() = errorDownloadMap.size
|
val errorCount get() = errorDownloadMap.size
|
||||||
|
|
||||||
|
init {
|
||||||
|
postEvent(EventBus.UP_DOWNLOAD, book.bookUrl)
|
||||||
|
}
|
||||||
|
|
||||||
@Synchronized
|
@Synchronized
|
||||||
fun isRun(): Boolean {
|
fun isRun(): Boolean {
|
||||||
return waitDownloadSet.size > 0 || onDownloadSet.size > 0
|
return waitDownloadSet.size > 0 || onDownloadSet.size > 0
|
||||||
@@ -214,9 +218,9 @@ object CacheBook {
|
|||||||
@Synchronized
|
@Synchronized
|
||||||
private fun onFinally() {
|
private fun onFinally() {
|
||||||
if (waitDownloadSet.isEmpty() && onDownloadSet.isEmpty()) {
|
if (waitDownloadSet.isEmpty() && onDownloadSet.isEmpty()) {
|
||||||
postEvent(EventBus.UP_DOWNLOAD, "")
|
|
||||||
cacheBookMap.remove(book.bookUrl)
|
cacheBookMap.remove(book.bookUrl)
|
||||||
}
|
}
|
||||||
|
postEvent(EventBus.UP_DOWNLOAD, book.bookUrl)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -224,6 +228,7 @@ object CacheBook {
|
|||||||
*/
|
*/
|
||||||
@Synchronized
|
@Synchronized
|
||||||
fun download(scope: CoroutineScope, context: CoroutineContext) {
|
fun download(scope: CoroutineScope, context: CoroutineContext) {
|
||||||
|
postEvent(EventBus.UP_DOWNLOAD, book.bookUrl)
|
||||||
val chapterIndex = waitDownloadSet.firstOrNull()
|
val chapterIndex = waitDownloadSet.firstOrNull()
|
||||||
if (chapterIndex == null) {
|
if (chapterIndex == null) {
|
||||||
if (onDownloadSet.isEmpty()) {
|
if (onDownloadSet.isEmpty()) {
|
||||||
@@ -276,6 +281,7 @@ object CacheBook {
|
|||||||
if (onDownloadSet.contains(chapter.index)) {
|
if (onDownloadSet.contains(chapter.index)) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
postEvent(EventBus.UP_DOWNLOAD, book.bookUrl)
|
||||||
onDownloadSet.add(chapter.index)
|
onDownloadSet.add(chapter.index)
|
||||||
waitDownloadSet.remove(chapter.index)
|
waitDownloadSet.remove(chapter.index)
|
||||||
WebBook.getContent(scope, bookSource, book, chapter)
|
WebBook.getContent(scope, bookSource, book, chapter)
|
||||||
@@ -288,9 +294,7 @@ object CacheBook {
|
|||||||
}.onCancel {
|
}.onCancel {
|
||||||
onCancel(chapter.index)
|
onCancel(chapter.index)
|
||||||
}.onFinally {
|
}.onFinally {
|
||||||
if (waitDownloadSet.isEmpty() && onDownloadSet.isEmpty()) {
|
postEvent(EventBus.UP_DOWNLOAD, book.bookUrl)
|
||||||
postEvent(EventBus.UP_DOWNLOAD, "")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -232,12 +232,15 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
|
|||||||
}
|
}
|
||||||
menu?.applyTint(this)
|
menu?.applyTint(this)
|
||||||
}
|
}
|
||||||
adapter.notifyItemRangeChanged(0, adapter.itemCount, true)
|
adapter.getItems().forEachIndexed { index, book ->
|
||||||
|
if (book.bookUrl == it) {
|
||||||
|
adapter.notifyItemChanged(index, true)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
observeEvent<BookChapter>(EventBus.SAVE_CONTENT) {
|
observeEvent<BookChapter>(EventBus.SAVE_CONTENT) {
|
||||||
adapter.cacheChapters[it.bookUrl]?.add(it.url)
|
adapter.cacheChapters[it.bookUrl]?.add(it.url)
|
||||||
}
|
}
|
||||||
postEventDelay(EventBus.UP_DOWNLOAD, "", 100)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun export(position: Int) {
|
override fun export(position: Int) {
|
||||||
|
|||||||
@@ -186,12 +186,6 @@ class MainViewModel(application: Application) : BaseViewModel(application) {
|
|||||||
private fun cacheBook() {
|
private fun cacheBook() {
|
||||||
cacheBookJob?.cancel()
|
cacheBookJob?.cancel()
|
||||||
cacheBookJob = viewModelScope.launch(upTocPool) {
|
cacheBookJob = viewModelScope.launch(upTocPool) {
|
||||||
launch {
|
|
||||||
while (isActive) {
|
|
||||||
postEvent(EventBus.UP_DOWNLOAD, "")
|
|
||||||
delay(1000)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
while (isActive) {
|
while (isActive) {
|
||||||
if (CacheBookService.isRun || !CacheBook.isRun) {
|
if (CacheBookService.isRun || !CacheBook.isRun) {
|
||||||
cacheBookJob?.cancel()
|
cacheBookJob?.cancel()
|
||||||
|
|||||||
Reference in New Issue
Block a user