From 26da15e79a1227222c6a6f2901d81f02ced05611 Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 19 Sep 2022 17:15:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/data/entities/RssSource.kt | 57 +++++++++++-------- .../main/java/io/legado/app/model/Debug.kt | 6 +- .../app/ui/rss/article/RssSortActivity.kt | 21 ++++--- .../app/ui/rss/article/RssSortViewModel.kt | 8 +++ .../source/debug/RssSourceDebugActivity.kt | 11 ++-- .../rss/source/debug/RssSourceDebugModel.kt | 13 ++--- app/src/main/res/menu/rss_articles.xml | 5 ++ 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-zh-rHK/strings.xml | 3 +- app/src/main/res/values-zh-rTW/strings.xml | 3 +- app/src/main/res/values-zh/strings.xml | 3 +- app/src/main/res/values/strings.xml | 1 + 14 files changed, 83 insertions(+), 51 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/entities/RssSource.kt b/app/src/main/java/io/legado/app/data/entities/RssSource.kt index b7811237b..17b270f33 100644 --- a/app/src/main/java/io/legado/app/data/entities/RssSource.kt +++ b/app/src/main/java/io/legado/app/data/entities/RssSource.kt @@ -9,6 +9,8 @@ import androidx.room.PrimaryKey import com.jayway.jsonpath.DocumentContext import io.legado.app.constant.AppPattern import io.legado.app.utils.* +import kotlinx.coroutines.Dispatchers.IO +import kotlinx.coroutines.withContext import kotlinx.parcelize.Parcelize @Parcelize @@ -133,32 +135,41 @@ data class RssSource( return this } - fun sortUrls(): List> = arrayListOf>().apply { - kotlin.runCatching { - var a = sortUrl - if (sortUrl?.startsWith("", false) == true - || sortUrl?.startsWith("@js:", false) == true - ) { - val aCache = ACache.get("rssSortUrl") - a = aCache.getAsString(sourceUrl) ?: "" - if (a.isBlank()) { - val jsStr = if (sortUrl!!.startsWith("@")) { - sortUrl!!.substring(4) - } else { - sortUrl!!.substring(4, sortUrl!!.lastIndexOf("<")) + suspend fun sortUrls(): List> = arrayListOf>().apply { + withContext(IO) { + kotlin.runCatching { + var a = sortUrl + if (sortUrl?.startsWith("", false) == true + || sortUrl?.startsWith("@js:", false) == true + ) { + val aCache = ACache.get("rssSortUrl") + a = aCache.getAsString(sourceUrl) ?: "" + if (a.isBlank()) { + val jsStr = if (sortUrl!!.startsWith("@")) { + sortUrl!!.substring(4) + } else { + sortUrl!!.substring(4, sortUrl!!.lastIndexOf("<")) + } + a = evalJS(jsStr).toString() + aCache.put(sourceUrl, a) } - a = evalJS(jsStr).toString() - aCache.put(sourceUrl, a) + } + a?.split("(&&|\n)+".toRegex())?.forEach { c -> + val d = c.split("::") + if (d.size > 1) + add(Pair(d[0], d[1])) + } + if (isEmpty()) { + add(Pair("", sourceUrl)) } } - a?.split("(&&|\n)+".toRegex())?.forEach { c -> - val d = c.split("::") - if (d.size > 1) - add(Pair(d[0], d[1])) - } - if (isEmpty()) { - add(Pair("", sourceUrl)) - } + } + } + + suspend fun removeSortCache() { + withContext(IO) { + val aCache = ACache.get("rssSortUrl") + aCache.remove(sourceUrl) } } diff --git a/app/src/main/java/io/legado/app/model/Debug.kt b/app/src/main/java/io/legado/app/model/Debug.kt index 30b427788..fef2b45a9 100644 --- a/app/src/main/java/io/legado/app/model/Debug.kt +++ b/app/src/main/java/io/legado/app/model/Debug.kt @@ -11,6 +11,8 @@ import io.legado.app.utils.HtmlFormatter import io.legado.app.utils.isAbsUrl import io.legado.app.utils.stackTraceStr import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers.IO +import kotlinx.coroutines.withContext import java.text.SimpleDateFormat import java.util.* @@ -101,11 +103,11 @@ object Debug { } } - fun startDebug(scope: CoroutineScope, rssSource: RssSource) { + suspend fun startDebug(scope: CoroutineScope, rssSource: RssSource) { cancelDebug() debugSource = rssSource.sourceUrl log(debugSource, "︾开始解析") - val sort = rssSource.sortUrls().first() + val sort = withContext(IO) { rssSource.sortUrls().first() } Rss.getArticles(scope, sort.first, sort.second, rssSource, 1) .onSuccess { if (it.first.isEmpty()) { diff --git a/app/src/main/java/io/legado/app/ui/rss/article/RssSortActivity.kt b/app/src/main/java/io/legado/app/ui/rss/article/RssSortActivity.kt index a2d8cc935..27ef2a11f 100644 --- a/app/src/main/java/io/legado/app/ui/rss/article/RssSortActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/article/RssSortActivity.kt @@ -69,6 +69,7 @@ class RssSortActivity : VMBaseActivity viewModel.clearSortCache { upFragments() } R.id.menu_set_source_variable -> setSourceVariable() R.id.menu_edit_source -> viewModel.rssSource?.sourceUrl?.let { editSourceResult.launch { @@ -89,16 +90,18 @@ class RssSortActivity : VMBaseActivity Unit) { + execute { + rssSource?.removeSortCache() + }.onFinally { + onFinally.invoke() + } + } + } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugActivity.kt b/app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugActivity.kt index 812d536fd..c361a0755 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugActivity.kt @@ -38,7 +38,7 @@ class RssSourceDebugActivity : VMBaseActivity(R.id.search_view).gone() } - private fun startSearch() { + private fun startDebug() { adapter.clearItems() - viewModel.startDebug({ + viewModel.rssSource?.let { binding.rotateLoading.show() - }, { - toastOnUi("未获取到源") - }) + viewModel.startDebug(it) + } ?: toastOnUi(R.string.error_no_source) } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugModel.kt b/app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugModel.kt index 23892dc6f..950f405af 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugModel.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugModel.kt @@ -1,7 +1,6 @@ package io.legado.app.ui.rss.source.debug import android.app.Application -import androidx.lifecycle.viewModelScope import io.legado.app.base.BaseViewModel import io.legado.app.data.appDb import io.legado.app.data.entities.RssSource @@ -9,7 +8,7 @@ import io.legado.app.model.Debug class RssSourceDebugModel(application: Application) : BaseViewModel(application), Debug.Callback { - private var rssSource: RssSource? = null + var rssSource: RssSource? = null private var callback: ((Int, String) -> Unit)? = null var listSrc: String? = null var contentSrc: String? = null @@ -28,12 +27,10 @@ class RssSourceDebugModel(application: Application) : BaseViewModel(application) this.callback = callback } - fun startDebug(start: (() -> Unit)? = null, error: (() -> Unit)? = null) { - rssSource?.let { - start?.invoke() - Debug.callback = this - Debug.startDebug(viewModelScope, it) - } ?: error?.invoke() + fun startDebug(source: RssSource) { + execute { + Debug.startDebug(this, source) + } } override fun printLog(state: Int, msg: String) { diff --git a/app/src/main/res/menu/rss_articles.xml b/app/src/main/res/menu/rss_articles.xml index ae808fb6f..cf0f177a9 100644 --- a/app/src/main/res/menu/rss_articles.xml +++ b/app/src/main/res/menu/rss_articles.xml @@ -7,6 +7,11 @@ android:title="@string/login" app:showAsAction="never" /> + + 异步加载图片 忽略音频焦点 允许与其他应用同时播放音频 + 刷新分类 diff --git a/app/src/main/res/values-ja-rJP/strings.xml b/app/src/main/res/values-ja-rJP/strings.xml index 9268538e6..8d0e7eaa0 100644 --- a/app/src/main/res/values-ja-rJP/strings.xml +++ b/app/src/main/res/values-ja-rJP/strings.xml @@ -1034,4 +1034,5 @@ 异步加载图片 忽略音频焦点 允许与其他应用同时播放音频 + 刷新分类 diff --git a/app/src/main/res/values-pt-rBR/strings.xml b/app/src/main/res/values-pt-rBR/strings.xml index 3b2399cbc..4dbc4974c 100644 --- a/app/src/main/res/values-pt-rBR/strings.xml +++ b/app/src/main/res/values-pt-rBR/strings.xml @@ -1034,4 +1034,5 @@ 异步加载图片 忽略音频焦点 允许与其他应用同时播放音频 + 刷新分类 diff --git a/app/src/main/res/values-zh-rHK/strings.xml b/app/src/main/res/values-zh-rHK/strings.xml index 040b03b55..400033552 100644 --- a/app/src/main/res/values-zh-rHK/strings.xml +++ b/app/src/main/res/values-zh-rHK/strings.xml @@ -441,7 +441,7 @@ - 沒有書源 + 沒有源 書籍信息獲取失敗 內容獲取失敗 目錄獲取失敗 @@ -1031,4 +1031,5 @@ 异步加载图片 忽略音频焦点 允许与其他应用同时播放音频 + 刷新分类 diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index 47ce6617d..8c8f6e7a7 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -448,7 +448,7 @@ - 沒有書源 + 沒有源 書籍訊息獲取失敗 內容獲取失敗 目錄獲取失敗 @@ -1033,4 +1033,5 @@ 异步加载图片 忽略音频焦点 允许与其他应用同时播放音频 + 刷新分类 diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xml index e440f7fa8..ae9d90f4b 100644 --- a/app/src/main/res/values-zh/strings.xml +++ b/app/src/main/res/values-zh/strings.xml @@ -448,7 +448,7 @@ - 没有书源 + 没有源 书籍信息获取失败 内容获取失败 目录获取失败 @@ -1033,4 +1033,5 @@ 异步加载图片 忽略音频焦点 允许与其他应用同时播放音频 + 刷新分类 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index cd09d3602..5b4c2b95a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1034,4 +1034,5 @@ 异步加载图片 忽略音频焦点 允许与其他应用同时播放音频 + 刷新分类