优化
This commit is contained in:
@@ -183,23 +183,24 @@ object BookController {
|
|||||||
if (content != null) {
|
if (content != null) {
|
||||||
val contentProcessor = ContentProcessor.get(book.name, book.origin)
|
val contentProcessor = ContentProcessor.get(book.name, book.origin)
|
||||||
saveBookReadIndex(book, index)
|
saveBookReadIndex(book, index)
|
||||||
return returnData.setData(
|
content = runBlocking {
|
||||||
contentProcessor.getContent(book, chapter, content, includeTitle = false)
|
contentProcessor.getContent(book, chapter, content!!, includeTitle = false)
|
||||||
.joinToString("\n")
|
.joinToString("\n")
|
||||||
)
|
}
|
||||||
|
return returnData.setData(content)
|
||||||
}
|
}
|
||||||
val bookSource = appDb.bookSourceDao.getBookSource(book.origin)
|
val bookSource = appDb.bookSourceDao.getBookSource(book.origin)
|
||||||
?: return returnData.setErrorMsg("未找到书源")
|
?: return returnData.setErrorMsg("未找到书源")
|
||||||
try {
|
try {
|
||||||
content = runBlocking {
|
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)
|
||||||
|
contentProcessor.getContent(book, chapter, it, includeTitle = false)
|
||||||
|
.joinToString("\n")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
val contentProcessor = ContentProcessor.get(book.name, book.origin)
|
returnData.setData(content)
|
||||||
saveBookReadIndex(book, index)
|
|
||||||
returnData.setData(
|
|
||||||
contentProcessor.getContent(book, chapter, content, includeTitle = false)
|
|
||||||
.joinToString("\n")
|
|
||||||
)
|
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
returnData.setErrorMsg(e.msg)
|
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.AppConfig
|
||||||
import io.legado.app.help.config.ReadBookConfig
|
import io.legado.app.help.config.ReadBookConfig
|
||||||
import io.legado.app.utils.toastOnUi
|
import io.legado.app.utils.toastOnUi
|
||||||
|
import kotlinx.coroutines.withTimeout
|
||||||
import splitties.init.appCtx
|
import splitties.init.appCtx
|
||||||
import java.lang.ref.WeakReference
|
import java.lang.ref.WeakReference
|
||||||
import java.util.concurrent.CopyOnWriteArrayList
|
import java.util.concurrent.CopyOnWriteArrayList
|
||||||
@@ -67,7 +68,7 @@ class ContentProcessor private constructor(
|
|||||||
return contentReplaceRules
|
return contentReplaceRules
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getContent(
|
suspend fun getContent(
|
||||||
book: Book,
|
book: Book,
|
||||||
chapter: BookChapter,
|
chapter: BookChapter,
|
||||||
content: String,
|
content: String,
|
||||||
@@ -128,18 +129,20 @@ class ContentProcessor private constructor(
|
|||||||
return contents
|
return contents
|
||||||
}
|
}
|
||||||
|
|
||||||
fun replaceContent(content: String): String {
|
suspend fun replaceContent(content: String): String {
|
||||||
var mContent = content
|
var mContent = content
|
||||||
getContentReplaceRules().forEach { item ->
|
getContentReplaceRules().forEach { item ->
|
||||||
if (item.pattern.isNotEmpty()) {
|
if (item.pattern.isNotEmpty()) {
|
||||||
try {
|
kotlin.runCatching {
|
||||||
mContent = if (item.isRegex) {
|
withTimeout(1000) {
|
||||||
mContent.replace(item.pattern.toRegex(), item.replacement)
|
mContent = if (item.isRegex) {
|
||||||
} else {
|
mContent.replace(item.pattern.toRegex(), item.replacement)
|
||||||
mContent.replace(item.pattern, item.replacement)
|
} else {
|
||||||
|
mContent.replace(item.pattern, item.replacement)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
}.onFailure {
|
||||||
AppLog.put("${item.name}替换出错\n${e.localizedMessage}")
|
AppLog.put("${item.name}替换出错\n${it.localizedMessage}")
|
||||||
appCtx.toastOnUi("${item.name}替换出错")
|
appCtx.toastOnUi("${item.name}替换出错")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -137,7 +137,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getAllContents(
|
private suspend fun getAllContents(
|
||||||
scope: CoroutineScope,
|
scope: CoroutineScope,
|
||||||
book: Book,
|
book: Book,
|
||||||
append: (text: String, srcList: ArrayList<Triple<String, Int, String>>?) -> Unit
|
append: (text: String, srcList: ArrayList<Triple<String, Int, String>>?) -> Unit
|
||||||
@@ -214,7 +214,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Suppress("BlockingMethodInNonBlockingContext")
|
@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"
|
val filename = "${getExportFileName(book)}.epub"
|
||||||
DocumentUtils.delete(doc, filename)
|
DocumentUtils.delete(doc, filename)
|
||||||
val epubBook = EpubBook()
|
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 filename = "${getExportFileName(book)}.epub"
|
||||||
val epubBook = EpubBook()
|
val epubBook = EpubBook()
|
||||||
epubBook.version = "2.0"
|
epubBook.version = "2.0"
|
||||||
@@ -252,6 +252,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
|
|||||||
val bookFile = FileUtils.createFileWithReplace(bookPath)
|
val bookFile = FileUtils.createFileWithReplace(bookPath)
|
||||||
//设置正文
|
//设置正文
|
||||||
setEpubContent(scope, contentModel, book, epubBook)
|
setEpubContent(scope, contentModel, book, epubBook)
|
||||||
|
@Suppress("BlockingMethodInNonBlockingContext")
|
||||||
EpubWriter().write(epubBook, FileOutputStream(bookFile))
|
EpubWriter().write(epubBook, FileOutputStream(bookFile))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -404,7 +405,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setEpubContent(
|
private suspend fun setEpubContent(
|
||||||
scope: CoroutineScope,
|
scope: CoroutineScope,
|
||||||
contentModel: String,
|
contentModel: String,
|
||||||
book: Book,
|
book: Book,
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ class SearchContentViewModel(application: Application) : BaseViewModel(applicati
|
|||||||
return searchResultsWithinChapter
|
return searchResultsWithinChapter
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun searchPosition(pattern: String): List<Int> {
|
private suspend fun searchPosition(pattern: String): List<Int> {
|
||||||
val position: MutableList<Int> = mutableListOf()
|
val position: MutableList<Int> = mutableListOf()
|
||||||
var index = mContent.indexOf(pattern)
|
var index = mContent.indexOf(pattern)
|
||||||
if (index >= 0) {
|
if (index >= 0) {
|
||||||
|
|||||||
Reference in New Issue
Block a user