kunfei пре 4 година
родитељ
комит
b555a579d4

+ 34 - 23
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<Pair<String, String>> = arrayListOf<Pair<String, String>>().apply {
-        kotlin.runCatching {
-            var a = sortUrl
-            if (sortUrl?.startsWith("<js>", 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<Pair<String, String>> = arrayListOf<Pair<String, String>>().apply {
+        withContext(IO) {
+            kotlin.runCatching {
+                var a = sortUrl
+                if (sortUrl?.startsWith("<js>", 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)
         }
     }
 

+ 4 - 2
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()) {

+ 12 - 9
app/src/main/java/io/legado/app/ui/rss/article/RssSortActivity.kt

@@ -69,6 +69,7 @@ class RssSortActivity : VMBaseActivity<ActivityRssArtivlesBinding, RssSortViewMo
                 putExtra("type", "rssSource")
                 putExtra("key", viewModel.rssSource?.sourceUrl)
             }
+            R.id.menu_refresh_sort -> 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<ActivityRssArtivlesBinding, RssSortViewMo
     }
 
     private fun upFragments() {
-        viewModel.rssSource?.sortUrls()?.let {
-            sortList.clear()
-            sortList.addAll(it)
-        }
-        if (sortList.size == 1) {
-            binding.tabLayout.gone()
-        } else {
-            binding.tabLayout.visible()
+        launch {
+            viewModel.rssSource?.sortUrls()?.let {
+                sortList.clear()
+                sortList.addAll(it)
+            }
+            if (sortList.size == 1) {
+                binding.tabLayout.gone()
+            } else {
+                binding.tabLayout.visible()
+            }
+            adapter.notifyDataSetChanged()
         }
-        adapter.notifyDataSetChanged()
     }
 
     private fun setSourceVariable() {

+ 8 - 0
app/src/main/java/io/legado/app/ui/rss/article/RssSortViewModel.kt

@@ -63,4 +63,12 @@ class RssSortViewModel(application: Application) : BaseViewModel(application) {
         }
     }
 
+    fun clearSortCache(onFinally: () -> Unit) {
+        execute {
+            rssSource?.removeSortCache()
+        }.onFinally {
+            onFinally.invoke()
+        }
+    }
+
 }

+ 5 - 6
app/src/main/java/io/legado/app/ui/rss/source/debug/RssSourceDebugActivity.kt

@@ -38,7 +38,7 @@ class RssSourceDebugActivity : VMBaseActivity<ActivitySourceDebugBinding, RssSou
             }
         }
         viewModel.initData(intent.getStringExtra("key")) {
-            startSearch()
+            startDebug()
         }
     }
 
@@ -65,12 +65,11 @@ class RssSourceDebugActivity : VMBaseActivity<ActivitySourceDebugBinding, RssSou
         binding.titleBar.findViewById<SearchView>(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)
     }
 }

+ 5 - 8
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) {

+ 5 - 0
app/src/main/res/menu/rss_articles.xml

@@ -7,6 +7,11 @@
         android:title="@string/login"
         app:showAsAction="never" />
 
+    <item
+        android:id="@+id/menu_refresh_sort"
+        android:title="@string/refresh_sort"
+        app:showAsAction="never" />
+
     <item
         android:id="@+id/menu_set_source_variable"
         android:title="@string/set_source_variable"

+ 1 - 0
app/src/main/res/values-es-rES/strings.xml

@@ -1031,4 +1031,5 @@
     <string name="async_load_image">异步加载图片</string>
     <string name="ignore_audio_focus_title">忽略音频焦点</string>
     <string name="ignore_audio_focus_summary">允许与其他应用同时播放音频</string>
+    <string name="refresh_sort">刷新分类</string>
 </resources>

+ 1 - 0
app/src/main/res/values-ja-rJP/strings.xml

@@ -1034,4 +1034,5 @@
     <string name="async_load_image">异步加载图片</string>
     <string name="ignore_audio_focus_title">忽略音频焦点</string>
     <string name="ignore_audio_focus_summary">允许与其他应用同时播放音频</string>
+    <string name="refresh_sort">刷新分类</string>
 </resources>

+ 1 - 0
app/src/main/res/values-pt-rBR/strings.xml

@@ -1034,4 +1034,5 @@
     <string name="async_load_image">异步加载图片</string>
     <string name="ignore_audio_focus_title">忽略音频焦点</string>
     <string name="ignore_audio_focus_summary">允许与其他应用同时播放音频</string>
+    <string name="refresh_sort">刷新分类</string>
 </resources>

+ 2 - 1
app/src/main/res/values-zh-rHK/strings.xml

@@ -441,7 +441,7 @@
     <!-- source end-->
 
     <!--error string start-->
-    <string name="error_no_source">沒有書源</string>
+    <string name="error_no_source">沒有源</string>
     <string name="error_get_book_info">書籍信息獲取失敗</string>
     <string name="error_get_content">內容獲取失敗</string>
     <string name="error_get_chapter_list">目錄獲取失敗</string>
@@ -1031,4 +1031,5 @@
     <string name="async_load_image">异步加载图片</string>
     <string name="ignore_audio_focus_title">忽略音频焦点</string>
     <string name="ignore_audio_focus_summary">允许与其他应用同时播放音频</string>
+    <string name="refresh_sort">刷新分类</string>
 </resources>

+ 2 - 1
app/src/main/res/values-zh-rTW/strings.xml

@@ -448,7 +448,7 @@
     <!-- source end-->
 
     <!--error string start-->
-    <string name="error_no_source">沒有書源</string>
+    <string name="error_no_source">沒有源</string>
     <string name="error_get_book_info">書籍訊息獲取失敗</string>
     <string name="error_get_content">內容獲取失敗</string>
     <string name="error_get_chapter_list">目錄獲取失敗</string>
@@ -1033,4 +1033,5 @@
     <string name="async_load_image">异步加载图片</string>
     <string name="ignore_audio_focus_title">忽略音频焦点</string>
     <string name="ignore_audio_focus_summary">允许与其他应用同时播放音频</string>
+    <string name="refresh_sort">刷新分类</string>
 </resources>

+ 2 - 1
app/src/main/res/values-zh/strings.xml

@@ -448,7 +448,7 @@
     <!-- source end-->
 
     <!--error string start-->
-    <string name="error_no_source">没有书源</string>
+    <string name="error_no_source">没有源</string>
     <string name="error_get_book_info">书籍信息获取失败</string>
     <string name="error_get_content">内容获取失败</string>
     <string name="error_get_chapter_list">目录获取失败</string>
@@ -1033,4 +1033,5 @@
     <string name="async_load_image">异步加载图片</string>
     <string name="ignore_audio_focus_title">忽略音频焦点</string>
     <string name="ignore_audio_focus_summary">允许与其他应用同时播放音频</string>
+    <string name="refresh_sort">刷新分类</string>
 </resources>

+ 1 - 0
app/src/main/res/values/strings.xml

@@ -1034,4 +1034,5 @@
     <string name="async_load_image">异步加载图片</string>
     <string name="ignore_audio_focus_title">忽略音频焦点</string>
     <string name="ignore_audio_focus_summary">允许与其他应用同时播放音频</string>
+    <string name="refresh_sort">刷新分类</string>
 </resources>