From 20b02be8016845bba70efeb131a8fe00c12e38ca Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 8 Mar 2022 21:24:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=80=9A=E7=94=A8=E5=B0=81?= =?UTF-8?q?=E9=9D=A2=E8=A7=84=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../assets/defaultData/coverRuleConfig.json | 4 ++ .../java/io/legado/app/help/DefaultData.kt | 10 ++++ .../java/io/legado/app/model/BookCover.kt | 30 ++++++------ .../app/ui/book/info/BookInfoActivity.kt | 46 ++++++++++--------- .../app/ui/book/info/BookInfoViewModel.kt | 35 ++++++++------ .../app/ui/config/CoverRuleConfigDialog.kt | 14 ++++++ .../res/layout/dialog_cover_rule_config.xml | 6 +++ app/src/main/res/values-es-rES/strings.xml | 2 +- app/src/main/res/values-ja-rJP/strings.xml | 2 +- app/src/main/res/values-pt-rBR/strings.xml | 2 +- app/src/main/res/values-zh-rHK/strings.xml | 2 +- app/src/main/res/values-zh-rTW/strings.xml | 2 +- app/src/main/res/values-zh/strings.xml | 2 +- app/src/main/res/values/strings.xml | 2 +- 14 files changed, 104 insertions(+), 55 deletions(-) create mode 100644 app/src/main/assets/defaultData/coverRuleConfig.json create mode 100644 app/src/main/java/io/legado/app/ui/config/CoverRuleConfigDialog.kt create mode 100644 app/src/main/res/layout/dialog_cover_rule_config.xml diff --git a/app/src/main/assets/defaultData/coverRuleConfig.json b/app/src/main/assets/defaultData/coverRuleConfig.json new file mode 100644 index 000000000..9f086cd65 --- /dev/null +++ b/app/src/main/assets/defaultData/coverRuleConfig.json @@ -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\")" +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/DefaultData.kt b/app/src/main/java/io/legado/app/help/DefaultData.kt index d7a989fa9..5205d2583 100644 --- a/app/src/main/java/io/legado/app/help/DefaultData.kt +++ b/app/src/main/java/io/legado/app/help/DefaultData.kt @@ -6,8 +6,10 @@ import io.legado.app.data.entities.RssSource import io.legado.app.data.entities.TxtTocRule import io.legado.app.help.config.ReadBookConfig import io.legado.app.help.config.ThemeConfig +import io.legado.app.model.BookCover import io.legado.app.utils.GSON import io.legado.app.utils.fromJsonArray +import io.legado.app.utils.fromJsonObject import splitties.init.appCtx import java.io.File @@ -60,6 +62,14 @@ object DefaultData { }.getOrDefault(emptyList()) } + val coverRuleConfig: BookCover.CoverRuleConfig by lazy { + val json = String( + appCtx.assets.open("defaultData${File.separator}coverRuleConfig.json") + .readBytes() + ) + GSON.fromJsonObject(json).getOrThrow()!! + } + fun importDefaultHttpTTS() { appDb.httpTTSDao.deleteDefault() appDb.httpTTSDao.insert(*httpTTS.toTypedArray()) diff --git a/app/src/main/java/io/legado/app/model/BookCover.kt b/app/src/main/java/io/legado/app/model/BookCover.kt index da3f7e298..f50a30b0b 100644 --- a/app/src/main/java/io/legado/app/model/BookCover.kt +++ b/app/src/main/java/io/legado/app/model/BookCover.kt @@ -9,14 +9,15 @@ import com.bumptech.glide.request.RequestOptions import io.legado.app.R import io.legado.app.constant.PreferKey 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.CacheManager +import io.legado.app.help.DefaultData import io.legado.app.help.config.AppConfig import io.legado.app.help.glide.ImageLoader import io.legado.app.model.analyzeRule.AnalyzeRule import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.utils.* -import kotlinx.coroutines.runBlocking import splitties.init.appCtx object BookCover { @@ -28,11 +29,9 @@ object BookCover { private set lateinit var defaultDrawable: Drawable private set - var coverRuleConfig: CoverRuleConfig? = + var coverRuleConfig: CoverRuleConfig = GSON.fromJsonObject(CacheManager.get(coverRuleConfigKey)).getOrNull() - private val analyzeRule by lazy { - AnalyzeRule() - } + ?: DefaultData.coverRuleConfig init { upDefaultCover() @@ -67,18 +66,23 @@ object BookCover { .apply(RequestOptions.bitmapTransform(BlurTransformation(context, 25))) } - fun searchCover(name: String, author: String): String? { - val config = coverRuleConfig ?: return null + suspend fun searchCover(book: Book): String? { + val config = coverRuleConfig if (config.searchUrl.isBlank() || config.coverRule.isBlank()) { return null } val analyzeUrl = - AnalyzeUrl(config.searchUrl, name, source = config, headerMapF = config.getHeaderMap()) - return runBlocking { - analyzeUrl.getStrResponseAwait().body?.let { body -> - return@let analyzeRule.getString(config.coverRule, body, true) - } - } + AnalyzeUrl( + config.searchUrl, + book.name, + 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) { diff --git a/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt b/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt index ba573bed6..46e8efe38 100644 --- a/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt @@ -182,7 +182,7 @@ class BookInfoActivity : if (viewModel.inBookshelf) { viewModel.bookData.value?.let { it.canUpdate = !it.canUpdate - viewModel.saveBook() + viewModel.saveBook(it) } } else { toastOnUi(R.string.after_add_bookshelf) @@ -306,7 +306,7 @@ class BookInfoActivity : } tvTocView.setOnClickListener { if (!viewModel.inBookshelf) { - viewModel.saveBook { + viewModel.saveBook(viewModel.bookData.value) { viewModel.saveChapterList { openChapterList() } @@ -366,15 +366,17 @@ class BookInfoActivity : } customView { alertBinding.root } okButton { - viewModel.bookData.value - ?.putVariable("custom", alertBinding.editView.text?.toString()) - viewModel.saveBook() + viewModel.bookData.value?.let { book -> + book.putVariable("custom", alertBinding.editView.text?.toString()) + viewModel.saveBook(book) + } } cancelButton() neutralButton(R.string.delete) { - viewModel.bookData.value - ?.putVariable("custom", null) - viewModel.saveBook() + viewModel.bookData.value?.let { book -> + book.putVariable("custom", null) + viewModel.saveBook(book) + } } } } @@ -423,13 +425,13 @@ class BookInfoActivity : private fun readBook(book: Book) { if (!viewModel.inBookshelf) { - viewModel.saveBook { + viewModel.saveBook(book) { viewModel.saveChapterList { startReadActivity(book) } } } else { - viewModel.saveBook { + viewModel.saveBook(book) { startReadActivity(book) } } @@ -461,22 +463,24 @@ class BookInfoActivity : } override fun coverChangeTo(coverUrl: String) { - viewModel.bookData.value?.let { - it.coverUrl = coverUrl - viewModel.saveBook() - showCover(it) + viewModel.bookData.value?.let { book -> + book.customCoverUrl = coverUrl + viewModel.saveBook(book) + showCover(book) } } override fun upGroup(requestCode: Int, groupId: Long) { upGroup(groupId) - viewModel.bookData.value?.group = groupId - if (viewModel.inBookshelf) { - viewModel.saveBook() - } else if (groupId > 0) { - viewModel.saveBook() - viewModel.inBookshelf = true - upTvBookshelf() + viewModel.bookData.value?.let { book -> + book.group = groupId + if (viewModel.inBookshelf) { + viewModel.saveBook(book) + } else if (groupId > 0) { + viewModel.saveBook(book) + viewModel.inBookshelf = true + upTvBookshelf() + } } } diff --git a/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt b/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt index f469d5432..91e41d0b6 100644 --- a/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt @@ -15,6 +15,7 @@ import io.legado.app.data.entities.BookSource import io.legado.app.help.BookHelp import io.legado.app.help.ContentProcessor import io.legado.app.help.coroutine.Coroutine +import io.legado.app.model.BookCover import io.legado.app.model.NoStackTraceException import io.legado.app.model.ReadBook 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 bookData.postValue(book) + if (book.customCoverUrl.isNullOrBlank()) { + BookCover.searchCover(book)?.let { coverUrl -> + book.customCoverUrl = coverUrl + bookData.postValue(book) + saveBook(book) + } + } bookSource = if (book.isLocalBook()) { null } else { @@ -232,20 +240,19 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { } } - fun saveBook(success: (() -> Unit)? = null) { + fun saveBook(book: Book?, success: (() -> Unit)? = null) { + book ?: return execute { - bookData.value?.let { book -> - if (book.order == 0) { - book.order = appDb.bookDao.minOrder - 1 - } - appDb.bookDao.getBook(book.name, book.author)?.let { - book.durChapterPos = it.durChapterPos - book.durChapterTitle = it.durChapterTitle - } - book.save() - if (ReadBook.book?.name == book.name && ReadBook.book?.author == book.author) { - ReadBook.book = book - } + if (book.order == 0) { + book.order = appDb.bookDao.minOrder - 1 + } + appDb.bookDao.getBook(book.name, book.author)?.let { + book.durChapterPos = it.durChapterPos + book.durChapterTitle = it.durChapterTitle + } + book.save() + if (ReadBook.book?.name == book.name && ReadBook.book?.author == book.author) { + ReadBook.book = book } }.onSuccess { success?.invoke() diff --git a/app/src/main/java/io/legado/app/ui/config/CoverRuleConfigDialog.kt b/app/src/main/java/io/legado/app/ui/config/CoverRuleConfigDialog.kt new file mode 100644 index 000000000..e7f7b033f --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/config/CoverRuleConfigDialog.kt @@ -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") + } + +} \ No newline at end of file diff --git a/app/src/main/res/layout/dialog_cover_rule_config.xml b/app/src/main/res/layout/dialog_cover_rule_config.xml new file mode 100644 index 000000000..77d9ef65f --- /dev/null +++ b/app/src/main/res/layout/dialog_cover_rule_config.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/values-es-rES/strings.xml b/app/src/main/res/values-es-rES/strings.xml index 077961d1d..7d28ae1c1 100644 --- a/app/src/main/res/values-es-rES/strings.xml +++ b/app/src/main/res/values-es-rES/strings.xml @@ -879,7 +879,7 @@ 设置书籍变量 注释 封面设置 - 设置默认封面样式 + 通用封面规则及默认封面样式 显示书名 封面上显示书名 显示作者 diff --git a/app/src/main/res/values-ja-rJP/strings.xml b/app/src/main/res/values-ja-rJP/strings.xml index 5dd7706a8..7aabfefb1 100644 --- a/app/src/main/res/values-ja-rJP/strings.xml +++ b/app/src/main/res/values-ja-rJP/strings.xml @@ -882,7 +882,7 @@ 设置书籍变量 注释 封面设置 - 设置默认封面样式 + 通用封面规则及默认封面样式 显示书名 封面上显示书名 显示作者 diff --git a/app/src/main/res/values-pt-rBR/strings.xml b/app/src/main/res/values-pt-rBR/strings.xml index b935c1896..25de7cee9 100644 --- a/app/src/main/res/values-pt-rBR/strings.xml +++ b/app/src/main/res/values-pt-rBR/strings.xml @@ -880,7 +880,7 @@ 设置书籍变量 注释 封面设置 - 设置默认封面样式 + 通用封面规则及默认封面样式 显示书名 封面上显示书名 显示作者 diff --git a/app/src/main/res/values-zh-rHK/strings.xml b/app/src/main/res/values-zh-rHK/strings.xml index f0c93b16a..24bd7b232 100644 --- a/app/src/main/res/values-zh-rHK/strings.xml +++ b/app/src/main/res/values-zh-rHK/strings.xml @@ -879,7 +879,7 @@ 設置書籍變量 注釋 封面設置 - 設置默認封面樣式 + 通用封面规则及默认封面样式 顯示書名 封面上顯示書名 顯示作者 diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index 845eb87b9..938ef8dc4 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -881,7 +881,7 @@ 設定書籍變數 注釋 封面設定 - 設定預設封面樣式 + 通用封面规则及默认封面样式 顯示書名 封面上顯示書名 顯示作者 diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xml index 8b337ce1b..4061fc0fc 100644 --- a/app/src/main/res/values-zh/strings.xml +++ b/app/src/main/res/values-zh/strings.xml @@ -883,7 +883,7 @@ 设置书籍变量 注释 封面设置 - 设置默认封面样式 + 通用封面规则及默认封面样式 显示书名 封面上显示书名 显示作者 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 9b5441891..54d690eab 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -882,7 +882,7 @@ 设置书籍变量 summary cover config - 设置默认封面样式 + 通用封面规则及默认封面样式 show name 封面上显示书名 show author