优化 #5570
This commit is contained in:
@@ -21,6 +21,7 @@ object EventBus {
|
|||||||
const val NOTIFY_MAIN = "notifyMain"
|
const val NOTIFY_MAIN = "notifyMain"
|
||||||
const val WEB_SERVICE = "webService"
|
const val WEB_SERVICE = "webService"
|
||||||
const val UP_DOWNLOAD = "upDownload"
|
const val UP_DOWNLOAD = "upDownload"
|
||||||
|
const val UP_DOWNLOAD_STATE = "upDownloadState"
|
||||||
const val SAVE_CONTENT = "saveContent"
|
const val SAVE_CONTENT = "saveContent"
|
||||||
const val CHECK_SOURCE = "checkSource"
|
const val CHECK_SOURCE = "checkSource"
|
||||||
const val CHECK_SOURCE_DONE = "checkSourceDone"
|
const val CHECK_SOURCE_DONE = "checkSourceDone"
|
||||||
|
|||||||
@@ -30,6 +30,8 @@ import kotlinx.coroutines.flow.MutableStateFlow
|
|||||||
import kotlinx.coroutines.flow.collect
|
import kotlinx.coroutines.flow.collect
|
||||||
import kotlinx.coroutines.flow.first
|
import kotlinx.coroutines.flow.first
|
||||||
import kotlinx.coroutines.flow.flow
|
import kotlinx.coroutines.flow.flow
|
||||||
|
import kotlinx.coroutines.flow.onCompletion
|
||||||
|
import kotlinx.coroutines.flow.onStart
|
||||||
import kotlinx.coroutines.isActive
|
import kotlinx.coroutines.isActive
|
||||||
import kotlinx.coroutines.sync.Mutex
|
import kotlinx.coroutines.sync.Mutex
|
||||||
import kotlinx.coroutines.sync.Semaphore
|
import kotlinx.coroutines.sync.Semaphore
|
||||||
@@ -140,10 +142,14 @@ object CacheBook {
|
|||||||
delay(1000)
|
delay(1000)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}.onStart {
|
||||||
|
postEvent(EventBus.UP_DOWNLOAD_STATE, "")
|
||||||
}.onEachParallel(AppConfig.threadCount) {
|
}.onEachParallel(AppConfig.threadCount) {
|
||||||
coroutineScope {
|
coroutineScope {
|
||||||
it.download(this, context)
|
it.download(this, context)
|
||||||
}
|
}
|
||||||
|
}.onCompletion {
|
||||||
|
postEvent(EventBus.UP_DOWNLOAD_STATE, "")
|
||||||
}.collect()
|
}.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -298,7 +304,6 @@ 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 (!isLoading && onDownloadSet.isEmpty()) {
|
if (!isLoading && onDownloadSet.isEmpty()) {
|
||||||
@@ -374,7 +379,6 @@ object CacheBook {
|
|||||||
}
|
}
|
||||||
|
|
||||||
suspend fun downloadAwait(chapter: BookChapter): String {
|
suspend fun downloadAwait(chapter: BookChapter): String {
|
||||||
postEvent(EventBus.UP_DOWNLOAD, book.bookUrl)
|
|
||||||
synchronized(this) {
|
synchronized(this) {
|
||||||
onDownloadSet.add(chapter.index)
|
onDownloadSet.add(chapter.index)
|
||||||
waitDownloadSet.remove(chapter.index)
|
waitDownloadSet.remove(chapter.index)
|
||||||
@@ -408,7 +412,6 @@ 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(
|
WebBook.getContent(
|
||||||
|
|||||||
@@ -300,6 +300,9 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
|
|||||||
notifyItemChanged(it)
|
notifyItemChanged(it)
|
||||||
}
|
}
|
||||||
observeEvent<String>(EventBus.UP_DOWNLOAD) {
|
observeEvent<String>(EventBus.UP_DOWNLOAD) {
|
||||||
|
notifyItemChanged(it)
|
||||||
|
}
|
||||||
|
observeEvent<String>(EventBus.UP_DOWNLOAD_STATE) {
|
||||||
if (!CacheBook.isRun) {
|
if (!CacheBook.isRun) {
|
||||||
menu?.findItem(R.id.menu_download)?.let { item ->
|
menu?.findItem(R.id.menu_download)?.let { item ->
|
||||||
item.setIconCompat(R.drawable.ic_play_24dp)
|
item.setIconCompat(R.drawable.ic_play_24dp)
|
||||||
@@ -313,7 +316,6 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
|
|||||||
}
|
}
|
||||||
menu?.applyTint(this)
|
menu?.applyTint(this)
|
||||||
}
|
}
|
||||||
notifyItemChanged(it)
|
|
||||||
}
|
}
|
||||||
observeEvent<Pair<Book, BookChapter>>(EventBus.SAVE_CONTENT) { (book, chapter) ->
|
observeEvent<Pair<Book, BookChapter>>(EventBus.SAVE_CONTENT) { (book, chapter) ->
|
||||||
viewModel.cacheChapters[book.bookUrl]?.add(chapter.url)
|
viewModel.cacheChapters[book.bookUrl]?.add(chapter.url)
|
||||||
|
|||||||
Reference in New Issue
Block a user