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.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)

@ -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<BookChapter>()
@ -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()

@ -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)
}
}

@ -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)

@ -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()

@ -48,7 +48,7 @@ class MainViewModel(application: Application) : BaseViewModel(application) {
}
fun upToc(books: List<Book>) {
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)

Loading…
Cancel
Save