pull/32/head
kunfei 5 years ago
parent c4245e171c
commit 8499cc7197
  1. 11
      app/src/main/java/io/legado/app/data/dao/ReplaceRuleDao.kt
  2. 6
      app/src/main/java/io/legado/app/data/dao/SearchBookDao.kt
  3. 2
      app/src/main/java/io/legado/app/data/entities/ReplaceRule.kt
  4. 42
      app/src/main/java/io/legado/app/help/BookHelp.kt
  5. 10
      app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt

@ -30,9 +30,18 @@ interface ReplaceRuleDao {
@Query("SELECT * FROM replace_rules WHERE id in (:ids)")
fun findByIds(vararg ids: Int): List<ReplaceRule>
@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<ReplaceRule>
@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<ReplaceRule>
@get:Query("SELECT COUNT(*) - SUM(isEnabled) FROM replace_rules")
val summary: Int

@ -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,
"""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
"""
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<Int, SearchShow>

@ -24,5 +24,3 @@ data class ReplaceRule(
@ColumnInfo(name = "sortOrder")
var order: Int = 0
) : Parcelable

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

@ -197,8 +197,16 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
}
private fun contentLoadFinish(chapter: BookChapter, content: String) {
execute {
if (chapter.index in durChapterIndex - 1..durChapterIndex + 1) {
callBack?.contentLoadFinish(chapter, content)
val c = BookHelp.disposeContent(
bookData.value?.name ?: "",
webBook?.bookSource?.bookSourceUrl,
content,
bookData.value?.useReplaceRule ?: true
)
callBack?.contentLoadFinish(chapter, c)
}
}
}

Loading…
Cancel
Save