Ver código fonte

修复源变量会被首页模块变量覆盖的问题

HapeLee 5 meses atrás
pai
commit
4c0a2256c2

+ 11 - 1
app/src/main/java/io/legado/app/data/entities/BaseSource.kt

@@ -265,9 +265,19 @@ interface BaseSource : JsExtensions {
      */
     @JavascriptInterface
     fun getVariable(): String {
+        getTemporaryVariable()?.let {
+            return it
+        }
         return CacheManager.get("sourceVariable_${getKey()}") ?: ""
     }
 
+    fun setTemporaryVariable(variable: String?) {
+    }
+
+    fun getTemporaryVariable(): String? {
+        return null
+    }
+
     /**
      * 保存数据
      */
@@ -341,4 +351,4 @@ interface BaseSource : JsExtensions {
         }
         return RhinoScriptEngine.eval(jsStr, scope)
     }
-}
+}

+ 15 - 1
app/src/main/java/io/legado/app/data/entities/BookSource.kt

@@ -4,6 +4,7 @@ import android.os.Parcelable
 import android.text.TextUtils
 import androidx.room.ColumnInfo
 import androidx.room.Entity
+import androidx.room.Ignore
 import androidx.room.Index
 import androidx.room.PrimaryKey
 import androidx.room.TypeConverter
@@ -19,6 +20,7 @@ import io.legado.app.data.entities.rule.TocRule
 import io.legado.app.utils.GSON
 import io.legado.app.utils.fromJsonObject
 import io.legado.app.utils.splitNotBlank
+import kotlinx.parcelize.IgnoredOnParcel
 import kotlinx.parcelize.Parcelize
 
 @Suppress("unused")
@@ -103,6 +105,10 @@ data class BookSource(
     var homepageModules: String? = null
 ) : Parcelable, BaseSource {
 
+    @Ignore
+    @IgnoredOnParcel
+    private var temporaryVariable: String? = null
+
     override fun getTag(): String {
         return bookSourceName
     }
@@ -111,6 +117,14 @@ data class BookSource(
         return bookSourceUrl
     }
 
+    override fun setTemporaryVariable(variable: String?) {
+        temporaryVariable = variable
+    }
+
+    override fun getTemporaryVariable(): String? {
+        return temporaryVariable
+    }
+
     override fun hashCode(): Int {
         return bookSourceUrl.hashCode()
     }
@@ -317,4 +331,4 @@ data class BookSource(
         fun reviewRuleToString(reviewRule: ReviewRule?): String = "null"
 
     }
-}
+}

+ 28 - 15
app/src/main/java/io/legado/app/domain/usecase/ExploreBooksUseCase.kt

@@ -1,5 +1,6 @@
 package io.legado.app.domain.usecase
 
+import io.legado.app.data.entities.BookSource
 import io.legado.app.data.entities.SearchBook
 import io.legado.app.domain.gateway.ExploreBooksGateway
 import kotlinx.coroutines.Dispatchers
@@ -23,15 +24,9 @@ class ExploreBooksUseCase(
         page: Int = 1,
         key: String? = null,
     ): ExploreResult = withContext(Dispatchers.IO) {
-        val base = gateway.getBookSource(sourceUrl)
-            ?: throw SourceNotFound(sourceUrl)
-        val source = args?.let { base.copy().also { s -> s.setVariable(it) } } ?: base
-        val resolvedUrl = moduleUrl
-            ?: (if (key != null) source.searchUrl else null)
-            ?: source.exploreUrl
-            ?: throw NoExploreUrl(sourceUrl)
-        val books = gateway.exploreBooks(source, resolvedUrl, page, key = key)
-        ExploreResult(resolvedUrl, books)
+        val request = resolveRequest(sourceUrl, moduleUrl, args, key)
+        val books = gateway.exploreBooks(request.source, request.url, page, key = key)
+        ExploreResult(request.url, books)
     }
 
     suspend fun executeForRanking(
@@ -39,16 +34,13 @@ class ExploreBooksUseCase(
         moduleUrl: String?,
         args: String?
     ): List<SearchBook> = withContext(Dispatchers.IO) {
-        val result = execute(sourceUrl, moduleUrl, args)
-        var books = result.books
+        val request = resolveRequest(sourceUrl, moduleUrl, args)
+        var books = gateway.exploreBooks(request.source, request.url, page = 1)
         var page = 1
         while (books.size < MAX_RANKING_BOOKS && page < MAX_RANKING_PAGES) {
             page++
             val next = try {
-                val source = gateway.getBookSource(sourceUrl)
-                    ?.let { s -> args?.let { s.copy().also { x -> x.setVariable(it) } } ?: s }
-                    ?: return@withContext books.take(MAX_RANKING_BOOKS)
-                gateway.exploreBooks(source, result.resolvedUrl, page)
+                gateway.exploreBooks(request.source, request.url, page)
             } catch (_: Exception) {
                 emptyList()
             }
@@ -58,8 +50,29 @@ class ExploreBooksUseCase(
         books.take(MAX_RANKING_BOOKS)
     }
 
+    private suspend fun resolveRequest(
+        sourceUrl: String,
+        moduleUrl: String?,
+        args: String?,
+        key: String? = null,
+    ): ExploreRequest {
+        val base = gateway.getBookSource(sourceUrl)
+            ?: throw SourceNotFound(sourceUrl)
+        val source = args?.let { base.copy().also { s -> s.setTemporaryVariable(it) } } ?: base
+        val url = moduleUrl
+            ?: (if (key != null) source.searchUrl else null)
+            ?: source.exploreUrl
+            ?: throw NoExploreUrl(sourceUrl)
+        return ExploreRequest(source, url)
+    }
+
     data class ExploreResult(val resolvedUrl: String, val books: List<SearchBook>)
 
+    private data class ExploreRequest(
+        val source: BookSource,
+        val url: String,
+    )
+
     class SourceNotFound(url: String) : Exception("Source not found: ${url.take(60)}")
     class NoExploreUrl(url: String) : Exception("No explore URL for source: ${url.take(60)}")
 }