From fb9189ac0ca48743823e24a0d922e30fa611b566 Mon Sep 17 00:00:00 2001 From: Horis <821938089@qq.com> Date: Sat, 14 Oct 2023 16:15:14 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/ui/book/source/edit/BookSourceEditViewModel.kt | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditViewModel.kt b/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditViewModel.kt index 92af5a825..9588e018d 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditViewModel.kt @@ -41,8 +41,8 @@ class BookSourceEditViewModel(application: Application) : BaseViewModel(applicat if (source.bookSourceUrl.isBlank() || source.bookSourceName.isBlank()) { throw NoStackTraceException(context.getString(R.string.non_null_name_url)) } - if (!source.equal(bookSource ?: BookSource())) { - source.lastUpdateTime = System.currentTimeMillis() + if (source.equal(bookSource ?: BookSource())) { + return@execute source } bookSource?.let { appDb.bookSourceDao.delete(it) @@ -91,6 +91,7 @@ class BookSourceEditViewModel(application: Application) : BaseViewModel(applicat val text1 = okHttpClient.newCallStrResponse { url(text) }.body importSource(text1!!) } + text.isJsonArray() -> { if (text.contains("ruleSearchUrl") || text.contains("ruleFindUrl")) { val items: List> = jsonPath.parse(text).read("$") @@ -100,6 +101,7 @@ class BookSourceEditViewModel(application: Application) : BaseViewModel(applicat GSON.fromJsonArray(text).getOrThrow()[0] } } + text.isJsonObject() -> { if (text.contains("ruleSearchUrl") || text.contains("ruleFindUrl")) { val jsonItem = jsonPath.parse(text) @@ -108,6 +110,7 @@ class BookSourceEditViewModel(application: Application) : BaseViewModel(applicat GSON.fromJsonObject(text).getOrThrow() } } + else -> throw NoStackTraceException("格式不对") } } From da0ca1b93fb7da85f882e7a4c5d90411112d661b Mon Sep 17 00:00:00 2001 From: Horis <821938089@qq.com> Date: Sat, 14 Oct 2023 17:27:21 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/io/legado/app/constant/PreferKey.kt | 1 + .../main/java/io/legado/app/help/config/AppConfig.kt | 5 +++++ .../app/ui/association/ImportBookSourceDialog.kt | 7 +++++++ .../app/ui/association/ImportBookSourceViewModel.kt | 5 +++++ .../ui/book/source/edit/BookSourceEditActivity.kt | 12 ++++++------ app/src/main/res/menu/import_source.xml | 8 +++++++- app/src/main/res/values-es-rES/strings.xml | 1 + app/src/main/res/values-ja-rJP/strings.xml | 1 + app/src/main/res/values-pt-rBR/strings.xml | 1 + app/src/main/res/values-vi/strings.xml | 1 + app/src/main/res/values-zh-rHK/strings.xml | 1 + app/src/main/res/values-zh-rTW/strings.xml | 1 + app/src/main/res/values-zh/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 14 files changed, 39 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/io/legado/app/constant/PreferKey.kt b/app/src/main/java/io/legado/app/constant/PreferKey.kt index 9a215a0f3..03353c557 100644 --- a/app/src/main/java/io/legado/app/constant/PreferKey.kt +++ b/app/src/main/java/io/legado/app/constant/PreferKey.kt @@ -137,6 +137,7 @@ object PreferKey { const val openBookInfoByClickTitle = "openBookInfoByClickTitle" const val defaultHomePage = "defaultHomePage" const val showBookshelfFastScroller = "showBookshelfFastScroller" + const val importKeepEnable = "importKeepEnable" const val cPrimary = "colorPrimary" const val cAccent = "colorAccent" diff --git a/app/src/main/java/io/legado/app/help/config/AppConfig.kt b/app/src/main/java/io/legado/app/help/config/AppConfig.kt index 4b573c73e..8ec9f27ca 100644 --- a/app/src/main/java/io/legado/app/help/config/AppConfig.kt +++ b/app/src/main/java/io/legado/app/help/config/AppConfig.kt @@ -420,6 +420,11 @@ object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener { val importKeepName get() = appCtx.getPrefBoolean(PreferKey.importKeepName) val importKeepGroup get() = appCtx.getPrefBoolean(PreferKey.importKeepGroup) + var importKeepEnable: Boolean + get() = appCtx.getPrefBoolean(PreferKey.importKeepEnable, false) + set(value) { + appCtx.putPrefBoolean(PreferKey.importKeepEnable, value) + } var preDownloadNum get() = appCtx.getPrefInt(PreferKey.preDownloadNum, 10) diff --git a/app/src/main/java/io/legado/app/ui/association/ImportBookSourceDialog.kt b/app/src/main/java/io/legado/app/ui/association/ImportBookSourceDialog.kt index 1637f901e..a1ff32655 100644 --- a/app/src/main/java/io/legado/app/ui/association/ImportBookSourceDialog.kt +++ b/app/src/main/java/io/legado/app/ui/association/ImportBookSourceDialog.kt @@ -142,6 +142,8 @@ class ImportBookSourceDialog() : BaseDialogFragment(R.layout.dialog_recycler_vie ?.isChecked = AppConfig.importKeepName binding.toolBar.menu.findItem(R.id.menu_keep_group) ?.isChecked = AppConfig.importKeepGroup + binding.toolBar.menu.findItem(R.id.menu_keep_enable) + ?.isChecked = AppConfig.importKeepEnable } @SuppressLint("InflateParams", "NotifyDataSetChanged") @@ -179,6 +181,11 @@ class ImportBookSourceDialog() : BaseDialogFragment(R.layout.dialog_recycler_vie item.isChecked = !item.isChecked putPrefBoolean(PreferKey.importKeepGroup, item.isChecked) } + + R.id.menu_keep_enable -> { + item.isChecked = !item.isChecked + AppConfig.importKeepEnable = item.isChecked + } } return false } diff --git a/app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt index ca62fc15b..e419826ee 100644 --- a/app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt @@ -87,6 +87,7 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) { val group = groupName?.trim() val keepName = AppConfig.importKeepName val keepGroup = AppConfig.importKeepGroup + val keepEnable = AppConfig.importKeepEnable val selectSource = arrayListOf() selectStatus.forEachIndexed { index, b -> if (b) { @@ -98,6 +99,10 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) { if (keepGroup) { source.bookSourceGroup = it.bookSourceGroup } + if (keepEnable) { + source.enabled = it.enabled + source.enabledExplore = it.enabledExplore + } source.customOrder = it.customOrder } if (!group.isNullOrEmpty()) { diff --git a/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt b/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt index 149720d7c..35e69375d 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt @@ -410,8 +410,8 @@ class BookSourceEditActivity : "intro" -> searchRule.intro = viewModel.ruleComplete(it.value, searchRule.bookList) - "updateTime" -> searchRule.updateTime = - viewModel.ruleComplete(it.value, searchRule.bookList) +// "updateTime" -> searchRule.updateTime = +// viewModel.ruleComplete(it.value, searchRule.bookList) "wordCount" -> searchRule.wordCount = viewModel.ruleComplete(it.value, searchRule.bookList) @@ -442,8 +442,8 @@ class BookSourceEditActivity : "intro" -> exploreRule.intro = viewModel.ruleComplete(it.value, exploreRule.bookList) - "updateTime" -> exploreRule.updateTime = - viewModel.ruleComplete(it.value, exploreRule.bookList) +// "updateTime" -> exploreRule.updateTime = +// viewModel.ruleComplete(it.value, exploreRule.bookList) "wordCount" -> exploreRule.wordCount = viewModel.ruleComplete(it.value, exploreRule.bookList) @@ -471,8 +471,8 @@ class BookSourceEditActivity : "intro" -> bookInfoRule.intro = viewModel.ruleComplete(it.value, bookInfoRule.init) - "updateTime" -> bookInfoRule.updateTime = - viewModel.ruleComplete(it.value, bookInfoRule.init) +// "updateTime" -> bookInfoRule.updateTime = +// viewModel.ruleComplete(it.value, bookInfoRule.init) "wordCount" -> bookInfoRule.wordCount = viewModel.ruleComplete(it.value, bookInfoRule.init) diff --git a/app/src/main/res/menu/import_source.xml b/app/src/main/res/menu/import_source.xml index dae1d41a4..e0836ab9b 100644 --- a/app/src/main/res/menu/import_source.xml +++ b/app/src/main/res/menu/import_source.xml @@ -31,4 +31,10 @@ android:checkable="true" app:showAsAction="never" /> - \ 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 b3412bd1e..e66b7816b 100644 --- a/app/src/main/res/values-es-rES/strings.xml +++ b/app/src/main/res/values-es-rES/strings.xml @@ -1131,4 +1131,5 @@ 默认主页 显示快速滚动条 导出所有书的书源 + 保留启用状态 diff --git a/app/src/main/res/values-ja-rJP/strings.xml b/app/src/main/res/values-ja-rJP/strings.xml index b95316f2d..49df7b727 100644 --- a/app/src/main/res/values-ja-rJP/strings.xml +++ b/app/src/main/res/values-ja-rJP/strings.xml @@ -1134,4 +1134,5 @@ 默认主页 显示快速滚动条 导出所有书的书源 + 保留启用状态 diff --git a/app/src/main/res/values-pt-rBR/strings.xml b/app/src/main/res/values-pt-rBR/strings.xml index 4a571f5d3..cf610bc15 100644 --- a/app/src/main/res/values-pt-rBR/strings.xml +++ b/app/src/main/res/values-pt-rBR/strings.xml @@ -1134,4 +1134,5 @@ 默认主页 显示快速滚动条 导出所有书的书源 + 保留启用状态 diff --git a/app/src/main/res/values-vi/strings.xml b/app/src/main/res/values-vi/strings.xml index 982048876..e119fe851 100644 --- a/app/src/main/res/values-vi/strings.xml +++ b/app/src/main/res/values-vi/strings.xml @@ -1129,4 +1129,5 @@ Còn 等待导出 显示快速滚动条 导出所有书的书源 + 保留启用状态 diff --git a/app/src/main/res/values-zh-rHK/strings.xml b/app/src/main/res/values-zh-rHK/strings.xml index bf9dc1e1a..d0c136f33 100644 --- a/app/src/main/res/values-zh-rHK/strings.xml +++ b/app/src/main/res/values-zh-rHK/strings.xml @@ -1130,4 +1130,5 @@ 等待导出 显示快速滚动条 导出所有书的书源 + 保留启用状态 diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index df17cc071..71de0175c 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -1132,4 +1132,5 @@ 等待导出 显示快速滚动条 导出所有书的书源 + 保留启用状态 diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xml index fa28465db..c9c19e1d3 100644 --- a/app/src/main/res/values-zh/strings.xml +++ b/app/src/main/res/values-zh/strings.xml @@ -1132,4 +1132,5 @@ 等待导出 显示快速滚动条 导出所有书的书源 + 保留启用状态 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ac7bd5c1c..780d330b9 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1134,4 +1134,5 @@ 默认主页 显示快速滚动条 导出所有书的书源 + 保留启用状态 From 1f3bdf0587d4642f19f85c665434bfdce2f67a4c Mon Sep 17 00:00:00 2001 From: Horis <821938089@qq.com> Date: Sun, 15 Oct 2023 11:35:55 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../changesource/ChangeBookSourceAdapter.kt | 2 + .../changesource/ChangeBookSourceDialog.kt | 3 +- .../changesource/ChangeBookSourceViewModel.kt | 73 ++++++++++++------- .../source/edit/BookSourceEditActivity.kt | 3 +- .../main/res/layout/item_change_source.xml | 4 +- 5 files changed, 54 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceAdapter.kt b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceAdapter.kt index fa0cdf0ac..f1c8c6d0e 100644 --- a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceAdapter.kt @@ -36,6 +36,8 @@ class ChangeBookSourceAdapter( override fun areContentsTheSame(oldItem: SearchBook, newItem: SearchBook): Boolean { return oldItem.originName == newItem.originName && oldItem.getDisplayLastChapterTitle() == newItem.getDisplayLastChapterTitle() + && oldItem.chapterWordCountText == newItem.chapterWordCountText + && oldItem.respondTime == newItem.respondTime } } diff --git a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceDialog.kt b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceDialog.kt index a910d4747..17abc34ae 100644 --- a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceDialog.kt @@ -72,7 +72,8 @@ class ChangeBookSourceDialog() : BaseDialogFragment(R.layout.dialog_book_change_ private val adapter by lazy { ChangeBookSourceAdapter(requireContext(), viewModel, this) } private val editSourceResult = registerForActivityResult(StartActivityContract(BookSourceEditActivity::class.java)) { - viewModel.startSearch() + val origin = it.data?.getStringExtra("origin") ?: return@registerForActivityResult + viewModel.startSearch(origin) } private val searchFinishCallback: (isEmpty: Boolean) -> Unit = { if (it) { diff --git a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceViewModel.kt index 21b38e701..c88f9c61b 100644 --- a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceViewModel.kt @@ -16,6 +16,7 @@ import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.SearchBook import io.legado.app.exception.NoStackTraceException import io.legado.app.help.book.BookHelp +import io.legado.app.help.book.ContentProcessor import io.legado.app.help.config.AppConfig import io.legado.app.help.config.SourceConfig import io.legado.app.help.coroutine.CompositeCoroutine @@ -50,8 +51,25 @@ open class ChangeBookSourceViewModel(application: Application) : BaseViewModel(a private var searchBookList = arrayListOf() private val searchBooks = Collections.synchronizedList(arrayListOf()) private val tocMap = ConcurrentHashMap>() + private val contentProcessor by lazy { + ContentProcessor.get(oldBook!!) + } private var searchCallback: SourceCallback? = null private val emptyBookSource = BookSource() + private val chapterNumRegex = "^\\[(\\d+)]".toRegex() + private val comparatorBase by lazy { + compareByDescending { getBookScore(it) } + .thenByDescending { SourceConfig.getSourceScore(it.origin) } + } + private val defaultComparator by lazy { + comparatorBase.thenBy { it.originOrder } + } + private val wordCountComparator by lazy { + comparatorBase.thenByDescending { it.chapterWordCount > 1000 } + .thenByDescending { getChapterNum(it.chapterWordCountText) } + .thenByDescending { it.chapterWordCount } + .thenBy { it.originOrder } + } val bookMap = ConcurrentHashMap() val searchDataFlow = callbackFlow { @@ -88,26 +106,12 @@ open class ChangeBookSourceViewModel(application: Application) : BaseViewModel(a } }.map { kotlin.runCatching { - searchBooks.sortedWith { o1, o2 -> - val o1bs = SourceConfig.getBookScore(o1.origin, o1.name, o1.author) - val o2bs = SourceConfig.getBookScore(o2.origin, o2.name, o2.author) - when { - o1bs - o2bs > 0 -> -1 - o1bs - o2bs < 0 -> 1 - else -> { - val o1ss = SourceConfig.getSourceScore(o1.origin) - val o2ss = SourceConfig.getSourceScore(o2.origin) - when { - o1ss - o2ss > 0 -> -1 - o1ss - o2ss < 0 -> 1 - else -> { - val n = o1.originOrder - o2.originOrder - if (n == 0) -1 else n - } - } - } - } + val comparator = if (AppConfig.changeSourceLoadWordCount) { + wordCountComparator + } else { + defaultComparator } + searchBooks.sortedWith(comparator) }.onFailure { AppLog.put("换源排序出错\n${it.localizedMessage}", it) }.getOrDefault(searchBooks) @@ -153,9 +157,18 @@ open class ChangeBookSourceViewModel(application: Application) : BaseViewModel(a /** * 搜索书籍 */ - fun startSearch() { + fun startSearch(origin: String? = null) { execute { stopSearch() + origin?.let { + bookSourceList.clear() + bookSourceList.add(appDb.bookSourceDao.getBookSource(origin)!!) + searchStateData.postValue(true) + searchBooks.removeIf { it.origin == origin } + initSearchPool() + search() + return@execute + } appDb.searchBookDao.clear(name, author) searchBooks.clear() searchCallback?.upAdapter() @@ -182,7 +195,7 @@ open class ChangeBookSourceViewModel(application: Application) : BaseViewModel(a private fun search() { val searchIndex = synchronized(this) { - if (searchIndex >= bookSourceList.lastIndex) { + if (searchIndex > bookSourceList.lastIndex) { return } searchIndex++ @@ -256,17 +269,18 @@ open class ChangeBookSourceViewModel(application: Application) : BaseViewModel(a ) } ?: chapters.lastIndex } else chapters.lastIndex - val bookChapter = chapters.getOrNull(chapterIndex) + val bookChapter = chapters[chapterIndex] + val title = bookChapter.title.trim() val startTime = System.currentTimeMillis() val pair = try { - if (bookChapter == null) throw NoStackTraceException("章节缺失,总章节数${chapters.size}") val nextChapterUrl = chapters.getOrNull(chapterIndex + 1)?.url - WebBook.getContentAwait(source, book, bookChapter, nextChapterUrl, false).length.let { - it to "第${chapterIndex + 1}章 字数:${it}" - } + var content = WebBook.getContentAwait(source, book, bookChapter, nextChapterUrl, false) + content = contentProcessor.getContent(oldBook!!, bookChapter, content, false).toString() + val len = content.length + len to "[${chapterIndex + 1}] ${title}\n字数:${len}" } catch (t: Throwable) { if (t is CancellationException) throw t - -1 to "第${chapterIndex + 1}章 获取字数失败:${t.localizedMessage}" + -1 to "[${chapterIndex + 1}] ${title}\n获取字数失败:${t.localizedMessage}" } val endTime = System.currentTimeMillis() val searchBook = book.toSearchBook().apply { @@ -526,6 +540,11 @@ open class ChangeBookSourceViewModel(application: Application) : BaseViewModel(a return SourceConfig.getBookScore(searchBook.origin, searchBook.name, searchBook.author) } + private fun getChapterNum(wordCountText: String?): Int { + wordCountText ?: return -1 + return chapterNumRegex.find(wordCountText)?.groupValues?.get(1)?.toIntOrNull() ?: -1 + } + interface SourceCallback { fun searchSuccess(searchBook: SearchBook) diff --git a/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt b/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt index 35e69375d..961ae73c8 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt @@ -1,6 +1,7 @@ package io.legado.app.ui.book.source.edit import android.app.Activity +import android.content.Intent import android.os.Bundle import android.view.Menu import android.view.MenuItem @@ -119,7 +120,7 @@ class BookSourceEditActivity : override fun onCompatOptionsItemSelected(item: MenuItem): Boolean { when (item.itemId) { R.id.menu_save -> viewModel.save(getSource()) { - setResult(Activity.RESULT_OK) + setResult(Activity.RESULT_OK, Intent().putExtra("origin", it.bookSourceUrl)) finish() } diff --git a/app/src/main/res/layout/item_change_source.xml b/app/src/main/res/layout/item_change_source.xml index 4a84f9692..d2e9f7ae2 100644 --- a/app/src/main/res/layout/item_change_source.xml +++ b/app/src/main/res/layout/item_change_source.xml @@ -82,7 +82,7 @@ tools:text="word count" android:textColor="@color/secondaryText" app:layout_constraintLeft_toLeftOf="parent" - app:layout_constraintRight_toRightOf="parent" + app:layout_constraintRight_toLeftOf="@+id/iv_checked" app:layout_constraintTop_toBottomOf="@+id/tv_last" />