diff --git a/app/src/main/java/io/legado/app/data/dao/ReplaceRuleDao.kt b/app/src/main/java/io/legado/app/data/dao/ReplaceRuleDao.kt index f65588378..b291ac968 100644 --- a/app/src/main/java/io/legado/app/data/dao/ReplaceRuleDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/ReplaceRuleDao.kt @@ -30,9 +30,18 @@ interface ReplaceRuleDao { @Query("SELECT * FROM replace_rules WHERE id in (:ids)") fun findByIds(vararg ids: Int): List - @Query("SELECT * FROM replace_rules WHERE isEnabled = 1 AND scope LIKE '%' || :scope || '%'") + @Query( + """SELECT * FROM replace_rules WHERE isEnabled = 1 + AND (scope LIKE '%' || :scope || '%' or scope = null or scope = '')""" + ) fun findEnabledByScope(scope: String): List + @Query( + """SELECT * FROM replace_rules WHERE isEnabled = 1 + AND (scope LIKE '%' || :name || '%' or scope LIKE '%' || :origin || '%' or scope = null or scope = '')""" + ) + fun findEnabledByScope(name: String, origin: String): List + @get:Query("SELECT COUNT(*) - SUM(isEnabled) FROM replace_rules") val summary: Int diff --git a/app/src/main/java/io/legado/app/data/dao/SearchBookDao.kt b/app/src/main/java/io/legado/app/data/dao/SearchBookDao.kt index 5d928c42a..a8b61a068 100644 --- a/app/src/main/java/io/legado/app/data/dao/SearchBookDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/SearchBookDao.kt @@ -12,13 +12,11 @@ import io.legado.app.data.entities.SearchShow interface SearchBookDao { @Query( - """ - SELECT name, author, min(time) time, max(kind) kind, max(coverUrl) coverUrl,max(intro) intro, max(wordCount) wordCount, - max(latestChapterTitle) latestChapterTitle, count(origin) originCount - FROM searchBooks where time >= :time - group by name, author - order by case when name = :key then 1 when author = :key then 2 when name like '%'+:key+'%' then 3 when author like '%'+:key+'%' then 4 else 5 end, time - """ + """SELECT name, author, min(time) time, max(kind) kind, max(coverUrl) coverUrl,max(intro) intro, max(wordCount) wordCount, + max(latestChapterTitle) latestChapterTitle, count(origin) originCount + FROM searchBooks where time >= :time + group by name, author + order by case when name = :key then 1 when author = :key then 2 when name like '%'||:key||'%' then 3 when author like '%'||:key||'%' then 4 else 5 end, time""" ) fun observeShow(key: String, time: Long): DataSource.Factory diff --git a/app/src/main/java/io/legado/app/data/entities/ReplaceRule.kt b/app/src/main/java/io/legado/app/data/entities/ReplaceRule.kt index e43699c36..65b070d57 100644 --- a/app/src/main/java/io/legado/app/data/entities/ReplaceRule.kt +++ b/app/src/main/java/io/legado/app/data/entities/ReplaceRule.kt @@ -23,6 +23,4 @@ data class ReplaceRule( var isRegex: Boolean = true, @ColumnInfo(name = "sortOrder") var order: Int = 0 -) : Parcelable - - +) : Parcelable \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/BookHelp.kt b/app/src/main/java/io/legado/app/help/BookHelp.kt index 83d8de8e5..9fc9206d1 100644 --- a/app/src/main/java/io/legado/app/help/BookHelp.kt +++ b/app/src/main/java/io/legado/app/help/BookHelp.kt @@ -3,6 +3,8 @@ package io.legado.app.help import io.legado.app.App import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter +import io.legado.app.data.entities.ReplaceRule +import io.legado.app.utils.getPrefInt import io.legado.app.utils.getPrefString import java.io.BufferedWriter import java.io.File @@ -11,10 +13,12 @@ import java.io.IOException object BookHelp { - private var downloadPath = App.INSTANCE.getPrefString("downloadPath") ?: App.INSTANCE.getExternalFilesDir(null) + private var downloadPath = + App.INSTANCE.getPrefString("downloadPath") ?: App.INSTANCE.getExternalFilesDir(null) fun upDownloadPath() { - downloadPath = App.INSTANCE.getPrefString("downloadPath") ?: App.INSTANCE.getExternalFilesDir(null) + downloadPath = + App.INSTANCE.getPrefString("downloadPath") ?: App.INSTANCE.getExternalFilesDir(null) } fun saveContent(book: Book, bookChapter: BookChapter, content: String) { @@ -69,7 +73,8 @@ object BookHelp { private fun getChapterPath(book: Book, bookChapter: BookChapter): String { val bookFolder = formatFolderName(book.name + book.bookUrl) - val chapterFile = String.format("%05d-%s", bookChapter.index, formatFolderName(bookChapter.title)) + val chapterFile = + String.format("%05d-%s", bookChapter.index, formatFolderName(bookChapter.title)) return "$downloadPath${File.separator}book_cache${File.separator}$bookFolder${File.separator}$chapterFile.nb" } @@ -88,4 +93,35 @@ object BookHelp { fun getDurChapterIndexByChapterName() { } + + var bookName: String? = null + var bookOrigin: String? = null + var replaceRules: List = arrayListOf() + + fun disposeContent(name: String, origin: String?, content: String, enableReplace: Boolean) + : String { + var c = content + synchronized(this) { + if (enableReplace && (bookName != name || bookOrigin != origin)) { + replaceRules = if (origin.isNullOrEmpty()) { + App.db.replaceRuleDao().findEnabledByScope(name) + } else { + App.db.replaceRuleDao().findEnabledByScope(name, origin) + } + } + } + for (item in replaceRules) { + item.pattern?.let { + if (it.isNotEmpty()) { + c = if (item.isRegex) { + c.replace(it.toRegex(), item.replacement ?: "") + } else { + c.replace(it, item.replacement ?: "") + } + } + } + } + val indent = App.INSTANCE.getPrefInt("textIndent", 2) + return c.replace("\\s*\\n+\\s*".toRegex(), "\n" + " ".repeat(indent)) + } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt b/app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt index 443543bc4..7fe1db4fc 100644 --- a/app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt @@ -197,8 +197,16 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { } private fun contentLoadFinish(chapter: BookChapter, content: String) { - if (chapter.index in durChapterIndex - 1..durChapterIndex + 1) { - callBack?.contentLoadFinish(chapter, content) + execute { + if (chapter.index in durChapterIndex - 1..durChapterIndex + 1) { + val c = BookHelp.disposeContent( + bookData.value?.name ?: "", + webBook?.bookSource?.bookSourceUrl, + content, + bookData.value?.useReplaceRule ?: true + ) + callBack?.contentLoadFinish(chapter, c) + } } }