From e6f96f4dd22de5a036eda1fffcb062d276e38bbc Mon Sep 17 00:00:00 2001 From: gedoor Date: Sat, 31 Jul 2021 18:07:34 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/model/webBook/SearchBookModel.kt | 5 ++--- .../io/legado/app/service/CacheBookService.kt | 20 +++++++++---------- .../legado/app/service/CheckSourceService.kt | 9 ++++----- .../book/changecover/ChangeCoverViewModel.kt | 5 ++--- .../changesource/ChangeSourceViewModel.kt | 4 ++-- .../io/legado/app/ui/main/MainViewModel.kt | 6 +++--- 6 files changed, 23 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/io/legado/app/model/webBook/SearchBookModel.kt b/app/src/main/java/io/legado/app/model/webBook/SearchBookModel.kt index 3397e6198..c15134453 100644 --- a/app/src/main/java/io/legado/app/model/webBook/SearchBookModel.kt +++ b/app/src/main/java/io/legado/app/model/webBook/SearchBookModel.kt @@ -7,7 +7,6 @@ import io.legado.app.help.AppConfig import io.legado.app.help.coroutine.CompositeCoroutine import io.legado.app.utils.getPrefString import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.ExecutorCoroutineDispatcher import kotlinx.coroutines.asCoroutineDispatcher import splitties.init.appCtx @@ -74,12 +73,12 @@ class SearchBookModel(private val scope: CoroutineScope, private val callBack: C searchPage, context = searchPool!! ).timeout(30000L) - .onSuccess(IO) { + .onSuccess(searchPool) { if (searchId == mSearchId) { callBack.onSearchSuccess(it) } } - .onFinally { + .onFinally(searchPool) { synchronized(this) { if (searchIndex < bookSourceList.lastIndex) { search(searchId) diff --git a/app/src/main/java/io/legado/app/service/CacheBookService.kt b/app/src/main/java/io/legado/app/service/CacheBookService.kt index 4f95185f0..7e592f545 100644 --- a/app/src/main/java/io/legado/app/service/CacheBookService.kt +++ b/app/src/main/java/io/legado/app/service/CacheBookService.kt @@ -31,8 +31,8 @@ import kotlin.math.min class CacheBookService : BaseService() { private val threadCount = AppConfig.threadCount - private var searchPool = - Executors.newFixedThreadPool(min(threadCount,8)).asCoroutineDispatcher() + private var cachePool = + Executors.newFixedThreadPool(min(threadCount, 8)).asCoroutineDispatcher() private var tasks = CompositeCoroutine() private val handler = Handler(Looper.getMainLooper()) private var runnable: Runnable = Runnable { upDownload() } @@ -83,7 +83,7 @@ class CacheBookService : BaseService() { override fun onDestroy() { tasks.clear() - searchPool.close() + cachePool.close() handler.removeCallbacks(runnable) downloadMap.clear() finalMap.clear() @@ -134,7 +134,7 @@ class CacheBookService : BaseService() { return } downloadCount[bookUrl] = DownloadCount() - execute { + execute(context = cachePool) { appDb.bookChapterDao.getChapterList(bookUrl, start, end).let { if (it.isNotEmpty()) { val chapters = CopyOnWriteArraySet() @@ -159,7 +159,7 @@ class CacheBookService : BaseService() { private fun download() { downloadingCount += 1 - val task = Coroutine.async(this, context = searchPool) { + val task = Coroutine.async(this, context = cachePool) { if (!isActive) return@async val bookChapter: BookChapter? = synchronized(this@CacheBookService) { downloadMap.forEach { @@ -186,16 +186,16 @@ class CacheBookService : BaseService() { return@async } if (!BookHelp.hasImageContent(book, bookChapter)) { - webBook.getContent(this, book, bookChapter, context = searchPool) + webBook.getContent(this, book, bookChapter, context = cachePool) .timeout(60000L) - .onError { + .onError(cachePool) { synchronized(this) { downloadingList.remove(bookChapter.url) } notificationContent = "getContentError${it.localizedMessage}" upNotification() } - .onSuccess { + .onSuccess(cachePool) { synchronized(this@CacheBookService) { downloadCount[book.bookUrl]?.increaseSuccess() downloadCount[book.bookUrl]?.increaseFinished() @@ -218,7 +218,7 @@ class CacheBookService : BaseService() { downloadCount.remove(book.bookUrl) } } - }.onFinally { + }.onFinally(cachePool) { postDownloading(true) } } else { @@ -228,7 +228,7 @@ class CacheBookService : BaseService() { postDownloading(true) } } - }.onError { + }.onError(cachePool) { notificationContent = "ERROR:${it.localizedMessage}" CacheBook.addLog(notificationContent) upNotification() diff --git a/app/src/main/java/io/legado/app/service/CheckSourceService.kt b/app/src/main/java/io/legado/app/service/CheckSourceService.kt index e9a7ff6fe..0c19e7445 100644 --- a/app/src/main/java/io/legado/app/service/CheckSourceService.kt +++ b/app/src/main/java/io/legado/app/service/CheckSourceService.kt @@ -17,7 +17,6 @@ import io.legado.app.service.help.CheckSource import io.legado.app.ui.book.source.manage.BookSourceActivity import io.legado.app.utils.postEvent import io.legado.app.utils.toastOnUi -import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.asCoroutineDispatcher import java.util.concurrent.Executors import kotlin.math.min @@ -95,7 +94,7 @@ class CheckSourceService : BaseService() { synchronized(this) { processIndex++ } - execute { + execute(context = searchCoroutine) { if (index < allIds.size) { val sourceUrl = allIds[index] appDb.bookSourceDao.getBookSource(sourceUrl)?.let { source -> @@ -130,14 +129,14 @@ class CheckSourceService : BaseService() { throw Exception("正文内容为空") } }.timeout(180000L) - .onError(IO) { + .onError(searchCoroutine) { source.addGroup("失效") source.bookSourceComment = """ "error:${it.localizedMessage} ${source.bookSourceComment}" """.trimIndent() appDb.bookSourceDao.update(source) - }.onSuccess(IO) { + }.onSuccess(searchCoroutine) { source.removeGroup("失效") source.bookSourceComment = source.bookSourceComment ?.split("\n") @@ -145,7 +144,7 @@ class CheckSourceService : BaseService() { it.startsWith("error:") }?.joinToString("\n") appDb.bookSourceDao.update(source) - }.onFinally { + }.onFinally(searchCoroutine) { onNext(source.bookSourceUrl, source.bookSourceName) } } diff --git a/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverViewModel.kt b/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverViewModel.kt index 406ba4071..b73644267 100644 --- a/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverViewModel.kt @@ -14,7 +14,6 @@ import io.legado.app.data.entities.SearchBook import io.legado.app.help.AppConfig import io.legado.app.help.coroutine.CompositeCoroutine import io.legado.app.model.webBook.WebBook -import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExecutorCoroutineDispatcher import kotlinx.coroutines.asCoroutineDispatcher import java.util.concurrent.CopyOnWriteArraySet @@ -105,7 +104,7 @@ class ChangeCoverViewModel(application: Application) : BaseViewModel(application val task = WebBook(source) .searchBook(viewModelScope, name, context = searchPool!!) .timeout(60000L) - .onSuccess(Dispatchers.IO) { + .onSuccess(searchPool) { if (it.isNotEmpty()) { val searchBook = it[0] if (searchBook.name == name && searchBook.author == author @@ -119,7 +118,7 @@ class ChangeCoverViewModel(application: Application) : BaseViewModel(application } } } - .onFinally { + .onFinally(searchPool) { searchNext() } tasks.add(task) diff --git a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt index 74b9aa099..e7b73f7fc 100644 --- a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt @@ -133,7 +133,7 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio val task = webBook .searchBook(viewModelScope, name, context = searchPool!!) .timeout(60000L) - .onSuccess(IO) { + .onSuccess(searchPool) { it.forEach { searchBook -> if (searchBook.name == name) { if ((AppConfig.changeSourceCheckAuthor && searchBook.author.contains(author)) @@ -152,7 +152,7 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio } } } - .onFinally { + .onFinally(searchPool) { synchronized(this) { if (searchIndex < bookSourceList.lastIndex) { search() diff --git a/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt b/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt index 1a21440f3..b5f4c0eaa 100644 --- a/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt @@ -48,7 +48,7 @@ class MainViewModel(application: Application) : BaseViewModel(application) { } fun upToc(books: List) { - execute { + execute(context = upTocPool) { books.filter { it.origin != BookType.local && it.canUpdate }.forEach { @@ -85,9 +85,9 @@ class MainViewModel(application: Application) : BaseViewModel(application) { appDb.bookChapterDao.delByBook(book.bookUrl) appDb.bookChapterDao.insert(*toc.toTypedArray()) cacheBook(webBook, book) - }.onError { + }.onError(upTocPool) { it.printStackTrace() - }.onFinally { + }.onFinally(upTocPool) { synchronized(this) { bookMap.remove(bookEntry.key) updateList.remove(book.bookUrl)