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. 12
      app/src/main/java/io/legado/app/data/dao/SearchBookDao.kt
  3. 4
      app/src/main/java/io/legado/app/data/entities/ReplaceRule.kt
  4. 42
      app/src/main/java/io/legado/app/help/BookHelp.kt
  5. 12
      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)") @Query("SELECT * FROM replace_rules WHERE id in (:ids)")
fun findByIds(vararg ids: Int): List<ReplaceRule> 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> 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") @get:Query("SELECT COUNT(*) - SUM(isEnabled) FROM replace_rules")
val summary: Int val summary: Int

@ -12,13 +12,11 @@ import io.legado.app.data.entities.SearchShow
interface SearchBookDao { interface SearchBookDao {
@Query( @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
max(latestChapterTitle) latestChapterTitle, count(origin) originCount FROM searchBooks where time >= :time
FROM searchBooks where time >= :time group by name, author
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> fun observeShow(key: String, time: Long): DataSource.Factory<Int, SearchShow>

@ -23,6 +23,4 @@ data class ReplaceRule(
var isRegex: Boolean = true, var isRegex: Boolean = true,
@ColumnInfo(name = "sortOrder") @ColumnInfo(name = "sortOrder")
var order: Int = 0 var order: Int = 0
) : Parcelable ) : Parcelable

@ -3,6 +3,8 @@ package io.legado.app.help
import io.legado.app.App import io.legado.app.App
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter 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 io.legado.app.utils.getPrefString
import java.io.BufferedWriter import java.io.BufferedWriter
import java.io.File import java.io.File
@ -11,10 +13,12 @@ import java.io.IOException
object BookHelp { 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() { 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) { fun saveContent(book: Book, bookChapter: BookChapter, content: String) {
@ -69,7 +73,8 @@ object BookHelp {
private fun getChapterPath(book: Book, bookChapter: BookChapter): String { private fun getChapterPath(book: Book, bookChapter: BookChapter): String {
val bookFolder = formatFolderName(book.name + book.bookUrl) 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" return "$downloadPath${File.separator}book_cache${File.separator}$bookFolder${File.separator}$chapterFile.nb"
} }
@ -88,4 +93,35 @@ object BookHelp {
fun getDurChapterIndexByChapterName() { 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) { private fun contentLoadFinish(chapter: BookChapter, content: String) {
if (chapter.index in durChapterIndex - 1..durChapterIndex + 1) { execute {
callBack?.contentLoadFinish(chapter, content) 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)
}
} }
} }

Loading…
Cancel
Save