pull/1198/head
gedoor 3 years ago
parent aaaef4b3c0
commit e6f96f4dd2
  1. 5
      app/src/main/java/io/legado/app/model/webBook/SearchBookModel.kt
  2. 20
      app/src/main/java/io/legado/app/service/CacheBookService.kt
  3. 9
      app/src/main/java/io/legado/app/service/CheckSourceService.kt
  4. 5
      app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverViewModel.kt
  5. 4
      app/src/main/java/io/legado/app/ui/book/changesource/ChangeSourceViewModel.kt
  6. 6
      app/src/main/java/io/legado/app/ui/main/MainViewModel.kt

@ -7,7 +7,6 @@ import io.legado.app.help.AppConfig
import io.legado.app.help.coroutine.CompositeCoroutine import io.legado.app.help.coroutine.CompositeCoroutine
import io.legado.app.utils.getPrefString import io.legado.app.utils.getPrefString
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.ExecutorCoroutineDispatcher import kotlinx.coroutines.ExecutorCoroutineDispatcher
import kotlinx.coroutines.asCoroutineDispatcher import kotlinx.coroutines.asCoroutineDispatcher
import splitties.init.appCtx import splitties.init.appCtx
@ -74,12 +73,12 @@ class SearchBookModel(private val scope: CoroutineScope, private val callBack: C
searchPage, searchPage,
context = searchPool!! context = searchPool!!
).timeout(30000L) ).timeout(30000L)
.onSuccess(IO) { .onSuccess(searchPool) {
if (searchId == mSearchId) { if (searchId == mSearchId) {
callBack.onSearchSuccess(it) callBack.onSearchSuccess(it)
} }
} }
.onFinally { .onFinally(searchPool) {
synchronized(this) { synchronized(this) {
if (searchIndex < bookSourceList.lastIndex) { if (searchIndex < bookSourceList.lastIndex) {
search(searchId) search(searchId)

@ -31,8 +31,8 @@ import kotlin.math.min
class CacheBookService : BaseService() { class CacheBookService : BaseService() {
private val threadCount = AppConfig.threadCount private val threadCount = AppConfig.threadCount
private var searchPool = private var cachePool =
Executors.newFixedThreadPool(min(threadCount,8)).asCoroutineDispatcher() Executors.newFixedThreadPool(min(threadCount, 8)).asCoroutineDispatcher()
private var tasks = CompositeCoroutine() private var tasks = CompositeCoroutine()
private val handler = Handler(Looper.getMainLooper()) private val handler = Handler(Looper.getMainLooper())
private var runnable: Runnable = Runnable { upDownload() } private var runnable: Runnable = Runnable { upDownload() }
@ -83,7 +83,7 @@ class CacheBookService : BaseService() {
override fun onDestroy() { override fun onDestroy() {
tasks.clear() tasks.clear()
searchPool.close() cachePool.close()
handler.removeCallbacks(runnable) handler.removeCallbacks(runnable)
downloadMap.clear() downloadMap.clear()
finalMap.clear() finalMap.clear()
@ -134,7 +134,7 @@ class CacheBookService : BaseService() {
return return
} }
downloadCount[bookUrl] = DownloadCount() downloadCount[bookUrl] = DownloadCount()
execute { execute(context = cachePool) {
appDb.bookChapterDao.getChapterList(bookUrl, start, end).let { appDb.bookChapterDao.getChapterList(bookUrl, start, end).let {
if (it.isNotEmpty()) { if (it.isNotEmpty()) {
val chapters = CopyOnWriteArraySet<BookChapter>() val chapters = CopyOnWriteArraySet<BookChapter>()
@ -159,7 +159,7 @@ class CacheBookService : BaseService() {
private fun download() { private fun download() {
downloadingCount += 1 downloadingCount += 1
val task = Coroutine.async(this, context = searchPool) { val task = Coroutine.async(this, context = cachePool) {
if (!isActive) return@async if (!isActive) return@async
val bookChapter: BookChapter? = synchronized(this@CacheBookService) { val bookChapter: BookChapter? = synchronized(this@CacheBookService) {
downloadMap.forEach { downloadMap.forEach {
@ -186,16 +186,16 @@ class CacheBookService : BaseService() {
return@async return@async
} }
if (!BookHelp.hasImageContent(book, bookChapter)) { if (!BookHelp.hasImageContent(book, bookChapter)) {
webBook.getContent(this, book, bookChapter, context = searchPool) webBook.getContent(this, book, bookChapter, context = cachePool)
.timeout(60000L) .timeout(60000L)
.onError { .onError(cachePool) {
synchronized(this) { synchronized(this) {
downloadingList.remove(bookChapter.url) downloadingList.remove(bookChapter.url)
} }
notificationContent = "getContentError${it.localizedMessage}" notificationContent = "getContentError${it.localizedMessage}"
upNotification() upNotification()
} }
.onSuccess { .onSuccess(cachePool) {
synchronized(this@CacheBookService) { synchronized(this@CacheBookService) {
downloadCount[book.bookUrl]?.increaseSuccess() downloadCount[book.bookUrl]?.increaseSuccess()
downloadCount[book.bookUrl]?.increaseFinished() downloadCount[book.bookUrl]?.increaseFinished()
@ -218,7 +218,7 @@ class CacheBookService : BaseService() {
downloadCount.remove(book.bookUrl) downloadCount.remove(book.bookUrl)
} }
} }
}.onFinally { }.onFinally(cachePool) {
postDownloading(true) postDownloading(true)
} }
} else { } else {
@ -228,7 +228,7 @@ class CacheBookService : BaseService() {
postDownloading(true) postDownloading(true)
} }
} }
}.onError { }.onError(cachePool) {
notificationContent = "ERROR:${it.localizedMessage}" notificationContent = "ERROR:${it.localizedMessage}"
CacheBook.addLog(notificationContent) CacheBook.addLog(notificationContent)
upNotification() upNotification()

@ -17,7 +17,6 @@ import io.legado.app.service.help.CheckSource
import io.legado.app.ui.book.source.manage.BookSourceActivity import io.legado.app.ui.book.source.manage.BookSourceActivity
import io.legado.app.utils.postEvent import io.legado.app.utils.postEvent
import io.legado.app.utils.toastOnUi import io.legado.app.utils.toastOnUi
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.asCoroutineDispatcher import kotlinx.coroutines.asCoroutineDispatcher
import java.util.concurrent.Executors import java.util.concurrent.Executors
import kotlin.math.min import kotlin.math.min
@ -95,7 +94,7 @@ class CheckSourceService : BaseService() {
synchronized(this) { synchronized(this) {
processIndex++ processIndex++
} }
execute { execute(context = searchCoroutine) {
if (index < allIds.size) { if (index < allIds.size) {
val sourceUrl = allIds[index] val sourceUrl = allIds[index]
appDb.bookSourceDao.getBookSource(sourceUrl)?.let { source -> appDb.bookSourceDao.getBookSource(sourceUrl)?.let { source ->
@ -130,14 +129,14 @@ class CheckSourceService : BaseService() {
throw Exception("正文内容为空") throw Exception("正文内容为空")
} }
}.timeout(180000L) }.timeout(180000L)
.onError(IO) { .onError(searchCoroutine) {
source.addGroup("失效") source.addGroup("失效")
source.bookSourceComment = """ source.bookSourceComment = """
"error:${it.localizedMessage} "error:${it.localizedMessage}
${source.bookSourceComment}" ${source.bookSourceComment}"
""".trimIndent() """.trimIndent()
appDb.bookSourceDao.update(source) appDb.bookSourceDao.update(source)
}.onSuccess(IO) { }.onSuccess(searchCoroutine) {
source.removeGroup("失效") source.removeGroup("失效")
source.bookSourceComment = source.bookSourceComment source.bookSourceComment = source.bookSourceComment
?.split("\n") ?.split("\n")
@ -145,7 +144,7 @@ class CheckSourceService : BaseService() {
it.startsWith("error:") it.startsWith("error:")
}?.joinToString("\n") }?.joinToString("\n")
appDb.bookSourceDao.update(source) appDb.bookSourceDao.update(source)
}.onFinally { }.onFinally(searchCoroutine) {
onNext(source.bookSourceUrl, source.bookSourceName) onNext(source.bookSourceUrl, source.bookSourceName)
} }
} }

@ -14,7 +14,6 @@ import io.legado.app.data.entities.SearchBook
import io.legado.app.help.AppConfig import io.legado.app.help.AppConfig
import io.legado.app.help.coroutine.CompositeCoroutine import io.legado.app.help.coroutine.CompositeCoroutine
import io.legado.app.model.webBook.WebBook import io.legado.app.model.webBook.WebBook
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExecutorCoroutineDispatcher import kotlinx.coroutines.ExecutorCoroutineDispatcher
import kotlinx.coroutines.asCoroutineDispatcher import kotlinx.coroutines.asCoroutineDispatcher
import java.util.concurrent.CopyOnWriteArraySet import java.util.concurrent.CopyOnWriteArraySet
@ -105,7 +104,7 @@ class ChangeCoverViewModel(application: Application) : BaseViewModel(application
val task = WebBook(source) val task = WebBook(source)
.searchBook(viewModelScope, name, context = searchPool!!) .searchBook(viewModelScope, name, context = searchPool!!)
.timeout(60000L) .timeout(60000L)
.onSuccess(Dispatchers.IO) { .onSuccess(searchPool) {
if (it.isNotEmpty()) { if (it.isNotEmpty()) {
val searchBook = it[0] val searchBook = it[0]
if (searchBook.name == name && searchBook.author == author if (searchBook.name == name && searchBook.author == author
@ -119,7 +118,7 @@ class ChangeCoverViewModel(application: Application) : BaseViewModel(application
} }
} }
} }
.onFinally { .onFinally(searchPool) {
searchNext() searchNext()
} }
tasks.add(task) tasks.add(task)

@ -133,7 +133,7 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio
val task = webBook val task = webBook
.searchBook(viewModelScope, name, context = searchPool!!) .searchBook(viewModelScope, name, context = searchPool!!)
.timeout(60000L) .timeout(60000L)
.onSuccess(IO) { .onSuccess(searchPool) {
it.forEach { searchBook -> it.forEach { searchBook ->
if (searchBook.name == name) { if (searchBook.name == name) {
if ((AppConfig.changeSourceCheckAuthor && searchBook.author.contains(author)) if ((AppConfig.changeSourceCheckAuthor && searchBook.author.contains(author))
@ -152,7 +152,7 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio
} }
} }
} }
.onFinally { .onFinally(searchPool) {
synchronized(this) { synchronized(this) {
if (searchIndex < bookSourceList.lastIndex) { if (searchIndex < bookSourceList.lastIndex) {
search() search()

@ -48,7 +48,7 @@ class MainViewModel(application: Application) : BaseViewModel(application) {
} }
fun upToc(books: List<Book>) { fun upToc(books: List<Book>) {
execute { execute(context = upTocPool) {
books.filter { books.filter {
it.origin != BookType.local && it.canUpdate it.origin != BookType.local && it.canUpdate
}.forEach { }.forEach {
@ -85,9 +85,9 @@ class MainViewModel(application: Application) : BaseViewModel(application) {
appDb.bookChapterDao.delByBook(book.bookUrl) appDb.bookChapterDao.delByBook(book.bookUrl)
appDb.bookChapterDao.insert(*toc.toTypedArray()) appDb.bookChapterDao.insert(*toc.toTypedArray())
cacheBook(webBook, book) cacheBook(webBook, book)
}.onError { }.onError(upTocPool) {
it.printStackTrace() it.printStackTrace()
}.onFinally { }.onFinally(upTocPool) {
synchronized(this) { synchronized(this) {
bookMap.remove(bookEntry.key) bookMap.remove(bookEntry.key)
updateList.remove(book.bookUrl) updateList.remove(book.bookUrl)

Loading…
Cancel
Save