添加通用封面规则

pull/1705/head
kunfei 3 years ago
parent 4c47e4a24a
commit 20b02be801
  1. 4
      app/src/main/assets/defaultData/coverRuleConfig.json
  2. 10
      app/src/main/java/io/legado/app/help/DefaultData.kt
  3. 30
      app/src/main/java/io/legado/app/model/BookCover.kt
  4. 38
      app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt
  5. 15
      app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt
  6. 14
      app/src/main/java/io/legado/app/ui/config/CoverRuleConfigDialog.kt
  7. 6
      app/src/main/res/layout/dialog_cover_rule_config.xml
  8. 2
      app/src/main/res/values-es-rES/strings.xml
  9. 2
      app/src/main/res/values-ja-rJP/strings.xml
  10. 2
      app/src/main/res/values-pt-rBR/strings.xml
  11. 2
      app/src/main/res/values-zh-rHK/strings.xml
  12. 2
      app/src/main/res/values-zh-rTW/strings.xml
  13. 2
      app/src/main/res/values-zh/strings.xml
  14. 2
      app/src/main/res/values/strings.xml

@ -0,0 +1,4 @@
{
"searchUrl": "https://api.yousuu.com/api/search?type=title&value={{key}}&page=1&highlight=0&from=search",
"coverRule": "@js:java.getString(\"$..books[?(@.author == '\" + book.author + \"')].cover\")"
}

@ -6,8 +6,10 @@ import io.legado.app.data.entities.RssSource
import io.legado.app.data.entities.TxtTocRule import io.legado.app.data.entities.TxtTocRule
import io.legado.app.help.config.ReadBookConfig import io.legado.app.help.config.ReadBookConfig
import io.legado.app.help.config.ThemeConfig import io.legado.app.help.config.ThemeConfig
import io.legado.app.model.BookCover
import io.legado.app.utils.GSON import io.legado.app.utils.GSON
import io.legado.app.utils.fromJsonArray import io.legado.app.utils.fromJsonArray
import io.legado.app.utils.fromJsonObject
import splitties.init.appCtx import splitties.init.appCtx
import java.io.File import java.io.File
@ -60,6 +62,14 @@ object DefaultData {
}.getOrDefault(emptyList()) }.getOrDefault(emptyList())
} }
val coverRuleConfig: BookCover.CoverRuleConfig by lazy {
val json = String(
appCtx.assets.open("defaultData${File.separator}coverRuleConfig.json")
.readBytes()
)
GSON.fromJsonObject<BookCover.CoverRuleConfig>(json).getOrThrow()!!
}
fun importDefaultHttpTTS() { fun importDefaultHttpTTS() {
appDb.httpTTSDao.deleteDefault() appDb.httpTTSDao.deleteDefault()
appDb.httpTTSDao.insert(*httpTTS.toTypedArray()) appDb.httpTTSDao.insert(*httpTTS.toTypedArray())

@ -9,14 +9,15 @@ import com.bumptech.glide.request.RequestOptions
import io.legado.app.R import io.legado.app.R
import io.legado.app.constant.PreferKey import io.legado.app.constant.PreferKey
import io.legado.app.data.entities.BaseSource import io.legado.app.data.entities.BaseSource
import io.legado.app.data.entities.Book
import io.legado.app.help.BlurTransformation import io.legado.app.help.BlurTransformation
import io.legado.app.help.CacheManager import io.legado.app.help.CacheManager
import io.legado.app.help.DefaultData
import io.legado.app.help.config.AppConfig import io.legado.app.help.config.AppConfig
import io.legado.app.help.glide.ImageLoader import io.legado.app.help.glide.ImageLoader
import io.legado.app.model.analyzeRule.AnalyzeRule import io.legado.app.model.analyzeRule.AnalyzeRule
import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.utils.* import io.legado.app.utils.*
import kotlinx.coroutines.runBlocking
import splitties.init.appCtx import splitties.init.appCtx
object BookCover { object BookCover {
@ -28,11 +29,9 @@ object BookCover {
private set private set
lateinit var defaultDrawable: Drawable lateinit var defaultDrawable: Drawable
private set private set
var coverRuleConfig: CoverRuleConfig? = var coverRuleConfig: CoverRuleConfig =
GSON.fromJsonObject<CoverRuleConfig>(CacheManager.get(coverRuleConfigKey)).getOrNull() GSON.fromJsonObject<CoverRuleConfig>(CacheManager.get(coverRuleConfigKey)).getOrNull()
private val analyzeRule by lazy { ?: DefaultData.coverRuleConfig
AnalyzeRule()
}
init { init {
upDefaultCover() upDefaultCover()
@ -67,18 +66,23 @@ object BookCover {
.apply(RequestOptions.bitmapTransform(BlurTransformation(context, 25))) .apply(RequestOptions.bitmapTransform(BlurTransformation(context, 25)))
} }
fun searchCover(name: String, author: String): String? { suspend fun searchCover(book: Book): String? {
val config = coverRuleConfig ?: return null val config = coverRuleConfig
if (config.searchUrl.isBlank() || config.coverRule.isBlank()) { if (config.searchUrl.isBlank() || config.coverRule.isBlank()) {
return null return null
} }
val analyzeUrl = val analyzeUrl =
AnalyzeUrl(config.searchUrl, name, source = config, headerMapF = config.getHeaderMap()) AnalyzeUrl(
return runBlocking { config.searchUrl,
analyzeUrl.getStrResponseAwait().body?.let { body -> book.name,
return@let analyzeRule.getString(config.coverRule, body, true) source = config,
} headerMapF = config.getHeaderMap()
} )
val res = analyzeUrl.getStrResponseAwait()
val analyzeRule = AnalyzeRule(book)
analyzeRule.setContent(res.body, config.searchUrl)
analyzeRule.setRedirectUrl(res.url)
return analyzeRule.getString(config.coverRule, isUrl = true)
} }
fun saveCoverRuleConfig(config: CoverRuleConfig) { fun saveCoverRuleConfig(config: CoverRuleConfig) {

@ -182,7 +182,7 @@ class BookInfoActivity :
if (viewModel.inBookshelf) { if (viewModel.inBookshelf) {
viewModel.bookData.value?.let { viewModel.bookData.value?.let {
it.canUpdate = !it.canUpdate it.canUpdate = !it.canUpdate
viewModel.saveBook() viewModel.saveBook(it)
} }
} else { } else {
toastOnUi(R.string.after_add_bookshelf) toastOnUi(R.string.after_add_bookshelf)
@ -306,7 +306,7 @@ class BookInfoActivity :
} }
tvTocView.setOnClickListener { tvTocView.setOnClickListener {
if (!viewModel.inBookshelf) { if (!viewModel.inBookshelf) {
viewModel.saveBook { viewModel.saveBook(viewModel.bookData.value) {
viewModel.saveChapterList { viewModel.saveChapterList {
openChapterList() openChapterList()
} }
@ -366,15 +366,17 @@ class BookInfoActivity :
} }
customView { alertBinding.root } customView { alertBinding.root }
okButton { okButton {
viewModel.bookData.value viewModel.bookData.value?.let { book ->
?.putVariable("custom", alertBinding.editView.text?.toString()) book.putVariable("custom", alertBinding.editView.text?.toString())
viewModel.saveBook() viewModel.saveBook(book)
}
} }
cancelButton() cancelButton()
neutralButton(R.string.delete) { neutralButton(R.string.delete) {
viewModel.bookData.value viewModel.bookData.value?.let { book ->
?.putVariable("custom", null) book.putVariable("custom", null)
viewModel.saveBook() viewModel.saveBook(book)
}
} }
} }
} }
@ -423,13 +425,13 @@ class BookInfoActivity :
private fun readBook(book: Book) { private fun readBook(book: Book) {
if (!viewModel.inBookshelf) { if (!viewModel.inBookshelf) {
viewModel.saveBook { viewModel.saveBook(book) {
viewModel.saveChapterList { viewModel.saveChapterList {
startReadActivity(book) startReadActivity(book)
} }
} }
} else { } else {
viewModel.saveBook { viewModel.saveBook(book) {
startReadActivity(book) startReadActivity(book)
} }
} }
@ -461,23 +463,25 @@ class BookInfoActivity :
} }
override fun coverChangeTo(coverUrl: String) { override fun coverChangeTo(coverUrl: String) {
viewModel.bookData.value?.let { viewModel.bookData.value?.let { book ->
it.coverUrl = coverUrl book.customCoverUrl = coverUrl
viewModel.saveBook() viewModel.saveBook(book)
showCover(it) showCover(book)
} }
} }
override fun upGroup(requestCode: Int, groupId: Long) { override fun upGroup(requestCode: Int, groupId: Long) {
upGroup(groupId) upGroup(groupId)
viewModel.bookData.value?.group = groupId viewModel.bookData.value?.let { book ->
book.group = groupId
if (viewModel.inBookshelf) { if (viewModel.inBookshelf) {
viewModel.saveBook() viewModel.saveBook(book)
} else if (groupId > 0) { } else if (groupId > 0) {
viewModel.saveBook() viewModel.saveBook(book)
viewModel.inBookshelf = true viewModel.inBookshelf = true
upTvBookshelf() upTvBookshelf()
} }
} }
}
} }

@ -15,6 +15,7 @@ import io.legado.app.data.entities.BookSource
import io.legado.app.help.BookHelp import io.legado.app.help.BookHelp
import io.legado.app.help.ContentProcessor import io.legado.app.help.ContentProcessor
import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.coroutine.Coroutine
import io.legado.app.model.BookCover
import io.legado.app.model.NoStackTraceException import io.legado.app.model.NoStackTraceException
import io.legado.app.model.ReadBook import io.legado.app.model.ReadBook
import io.legado.app.model.localBook.LocalBook import io.legado.app.model.localBook.LocalBook
@ -69,9 +70,16 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
} }
} }
private fun setBook(book: Book) { private suspend fun setBook(book: Book) {
durChapterIndex = book.durChapterIndex durChapterIndex = book.durChapterIndex
bookData.postValue(book) bookData.postValue(book)
if (book.customCoverUrl.isNullOrBlank()) {
BookCover.searchCover(book)?.let { coverUrl ->
book.customCoverUrl = coverUrl
bookData.postValue(book)
saveBook(book)
}
}
bookSource = if (book.isLocalBook()) { bookSource = if (book.isLocalBook()) {
null null
} else { } else {
@ -232,9 +240,9 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
} }
} }
fun saveBook(success: (() -> Unit)? = null) { fun saveBook(book: Book?, success: (() -> Unit)? = null) {
book ?: return
execute { execute {
bookData.value?.let { book ->
if (book.order == 0) { if (book.order == 0) {
book.order = appDb.bookDao.minOrder - 1 book.order = appDb.bookDao.minOrder - 1
} }
@ -246,7 +254,6 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
if (ReadBook.book?.name == book.name && ReadBook.book?.author == book.author) { if (ReadBook.book?.name == book.name && ReadBook.book?.author == book.author) {
ReadBook.book = book ReadBook.book = book
} }
}
}.onSuccess { }.onSuccess {
success?.invoke() success?.invoke()
} }

@ -0,0 +1,14 @@
package io.legado.app.ui.config
import android.os.Bundle
import android.view.View
import io.legado.app.R
import io.legado.app.base.BaseDialogFragment
class CoverRuleConfigDialog : BaseDialogFragment(R.layout.dialog_cover_rule_config) {
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
TODO("Not yet implemented")
}
}

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.constraintlayout.widget.ConstraintLayout>

@ -879,7 +879,7 @@
<string name="set_book_variable">设置书籍变量</string> <string name="set_book_variable">设置书籍变量</string>
<string name="summary">注释</string> <string name="summary">注释</string>
<string name="cover_config">封面设置</string> <string name="cover_config">封面设置</string>
<string name="cover_config_summary">设置默认封面样式</string> <string name="cover_config_summary">通用封面规则及默认封面样式</string>
<string name="cover_show_name">显示书名</string> <string name="cover_show_name">显示书名</string>
<string name="cover_show_name_summary">封面上显示书名</string> <string name="cover_show_name_summary">封面上显示书名</string>
<string name="cover_show_author">显示作者</string> <string name="cover_show_author">显示作者</string>

@ -882,7 +882,7 @@
<string name="set_book_variable">设置书籍变量</string> <string name="set_book_variable">设置书籍变量</string>
<string name="summary">注释</string> <string name="summary">注释</string>
<string name="cover_config">封面设置</string> <string name="cover_config">封面设置</string>
<string name="cover_config_summary">设置默认封面样式</string> <string name="cover_config_summary">通用封面规则及默认封面样式</string>
<string name="cover_show_name">显示书名</string> <string name="cover_show_name">显示书名</string>
<string name="cover_show_name_summary">封面上显示书名</string> <string name="cover_show_name_summary">封面上显示书名</string>
<string name="cover_show_author">显示作者</string> <string name="cover_show_author">显示作者</string>

@ -880,7 +880,7 @@
<string name="set_book_variable">设置书籍变量</string> <string name="set_book_variable">设置书籍变量</string>
<string name="summary">注释</string> <string name="summary">注释</string>
<string name="cover_config">封面设置</string> <string name="cover_config">封面设置</string>
<string name="cover_config_summary">设置默认封面样式</string> <string name="cover_config_summary">通用封面规则及默认封面样式</string>
<string name="cover_show_name">显示书名</string> <string name="cover_show_name">显示书名</string>
<string name="cover_show_name_summary">封面上显示书名</string> <string name="cover_show_name_summary">封面上显示书名</string>
<string name="cover_show_author">显示作者</string> <string name="cover_show_author">显示作者</string>

@ -879,7 +879,7 @@
<string name="set_book_variable">設置書籍變量</string> <string name="set_book_variable">設置書籍變量</string>
<string name="summary">注釋</string> <string name="summary">注釋</string>
<string name="cover_config">封面設置</string> <string name="cover_config">封面設置</string>
<string name="cover_config_summary">設置默認封面樣</string> <string name="cover_config_summary">通用封面规则及默认封面样</string>
<string name="cover_show_name">顯示書名</string> <string name="cover_show_name">顯示書名</string>
<string name="cover_show_name_summary">封面上顯示書名</string> <string name="cover_show_name_summary">封面上顯示書名</string>
<string name="cover_show_author">顯示作者</string> <string name="cover_show_author">顯示作者</string>

@ -881,7 +881,7 @@
<string name="set_book_variable">設定書籍變數</string> <string name="set_book_variable">設定書籍變數</string>
<string name="summary">注釋</string> <string name="summary">注釋</string>
<string name="cover_config">封面設定</string> <string name="cover_config">封面設定</string>
<string name="cover_config_summary">設定預設封面樣</string> <string name="cover_config_summary">通用封面规则及默认封面样</string>
<string name="cover_show_name">顯示書名</string> <string name="cover_show_name">顯示書名</string>
<string name="cover_show_name_summary">封面上顯示書名</string> <string name="cover_show_name_summary">封面上顯示書名</string>
<string name="cover_show_author">顯示作者</string> <string name="cover_show_author">顯示作者</string>

@ -883,7 +883,7 @@
<string name="set_book_variable">设置书籍变量</string> <string name="set_book_variable">设置书籍变量</string>
<string name="summary">注释</string> <string name="summary">注释</string>
<string name="cover_config">封面设置</string> <string name="cover_config">封面设置</string>
<string name="cover_config_summary">设置默认封面样式</string> <string name="cover_config_summary">通用封面规则及默认封面样式</string>
<string name="cover_show_name">显示书名</string> <string name="cover_show_name">显示书名</string>
<string name="cover_show_name_summary">封面上显示书名</string> <string name="cover_show_name_summary">封面上显示书名</string>
<string name="cover_show_author">显示作者</string> <string name="cover_show_author">显示作者</string>

@ -882,7 +882,7 @@
<string name="set_book_variable">设置书籍变量</string> <string name="set_book_variable">设置书籍变量</string>
<string name="summary">summary</string> <string name="summary">summary</string>
<string name="cover_config">cover config</string> <string name="cover_config">cover config</string>
<string name="cover_config_summary">设置默认封面样式</string> <string name="cover_config_summary">通用封面规则及默认封面样式</string>
<string name="cover_show_name">show name</string> <string name="cover_show_name">show name</string>
<string name="cover_show_name_summary">封面上显示书名</string> <string name="cover_show_name_summary">封面上显示书名</string>
<string name="cover_show_author">show author</string> <string name="cover_show_author">show author</string>

Loading…
Cancel
Save