pull/1771/head
kunfei 3 years ago
parent bbb6548b5d
commit 708a071e2e
  1. 17
      app/src/main/java/io/legado/app/api/controller/BookController.kt
  2. 13
      app/src/main/java/io/legado/app/help/ContentProcessor.kt
  3. 9
      app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt
  4. 2
      app/src/main/java/io/legado/app/ui/book/searchContent/SearchContentViewModel.kt

@ -183,23 +183,24 @@ object BookController {
if (content != null) {
val contentProcessor = ContentProcessor.get(book.name, book.origin)
saveBookReadIndex(book, index)
return returnData.setData(
contentProcessor.getContent(book, chapter, content, includeTitle = false)
content = runBlocking {
contentProcessor.getContent(book, chapter, content!!, includeTitle = false)
.joinToString("\n")
)
}
return returnData.setData(content)
}
val bookSource = appDb.bookSourceDao.getBookSource(book.origin)
?: return returnData.setErrorMsg("未找到书源")
try {
content = runBlocking {
WebBook.getContentAwait(this, bookSource, book, chapter)
}
WebBook.getContentAwait(this, bookSource, book, chapter).let {
val contentProcessor = ContentProcessor.get(book.name, book.origin)
saveBookReadIndex(book, index)
returnData.setData(
contentProcessor.getContent(book, chapter, content, includeTitle = false)
contentProcessor.getContent(book, chapter, it, includeTitle = false)
.joinToString("\n")
)
}
}
returnData.setData(content)
} catch (e: Exception) {
returnData.setErrorMsg(e.msg)
}

@ -9,6 +9,7 @@ import io.legado.app.data.entities.ReplaceRule
import io.legado.app.help.config.AppConfig
import io.legado.app.help.config.ReadBookConfig
import io.legado.app.utils.toastOnUi
import kotlinx.coroutines.withTimeout
import splitties.init.appCtx
import java.lang.ref.WeakReference
import java.util.concurrent.CopyOnWriteArrayList
@ -67,7 +68,7 @@ class ContentProcessor private constructor(
return contentReplaceRules
}
fun getContent(
suspend fun getContent(
book: Book,
chapter: BookChapter,
content: String,
@ -128,18 +129,20 @@ class ContentProcessor private constructor(
return contents
}
fun replaceContent(content: String): String {
suspend fun replaceContent(content: String): String {
var mContent = content
getContentReplaceRules().forEach { item ->
if (item.pattern.isNotEmpty()) {
try {
kotlin.runCatching {
withTimeout(1000) {
mContent = if (item.isRegex) {
mContent.replace(item.pattern.toRegex(), item.replacement)
} else {
mContent.replace(item.pattern, item.replacement)
}
} catch (e: Exception) {
AppLog.put("${item.name}替换出错\n${e.localizedMessage}")
}
}.onFailure {
AppLog.put("${item.name}替换出错\n${it.localizedMessage}")
appCtx.toastOnUi("${item.name}替换出错")
}
}

@ -137,7 +137,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
}
}
private fun getAllContents(
private suspend fun getAllContents(
scope: CoroutineScope,
book: Book,
append: (text: String, srcList: ArrayList<Triple<String, Int, String>>?) -> Unit
@ -214,7 +214,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
}
@Suppress("BlockingMethodInNonBlockingContext")
private fun exportEpub(scope: CoroutineScope, doc: DocumentFile, book: Book) {
private suspend fun exportEpub(scope: CoroutineScope, doc: DocumentFile, book: Book) {
val filename = "${getExportFileName(book)}.epub"
DocumentUtils.delete(doc, filename)
val epubBook = EpubBook()
@ -237,7 +237,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
}
private fun exportEpub(scope: CoroutineScope, file: File, book: Book) {
private suspend fun exportEpub(scope: CoroutineScope, file: File, book: Book) {
val filename = "${getExportFileName(book)}.epub"
val epubBook = EpubBook()
epubBook.version = "2.0"
@ -252,6 +252,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
val bookFile = FileUtils.createFileWithReplace(bookPath)
//设置正文
setEpubContent(scope, contentModel, book, epubBook)
@Suppress("BlockingMethodInNonBlockingContext")
EpubWriter().write(epubBook, FileOutputStream(bookFile))
}
@ -404,7 +405,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
})
}
private fun setEpubContent(
private suspend fun setEpubContent(
scope: CoroutineScope,
contentModel: String,
book: Book,

@ -76,7 +76,7 @@ class SearchContentViewModel(application: Application) : BaseViewModel(applicati
return searchResultsWithinChapter
}
private fun searchPosition(pattern: String): List<Int> {
private suspend fun searchPosition(pattern: String): List<Int> {
val position: MutableList<Int> = mutableListOf()
var index = mContent.indexOf(pattern)
if (index >= 0) {

Loading…
Cancel
Save