修复源变量会被首页模块变量覆盖的问题
This commit is contained in:
3 files changed
+52
-15
No files matched your search
@@ -265,9 +265,19 @@ interface BaseSource : JsExtensions {
|
|||||||
*/
|
*/
|
||||||
@JavascriptInterface
|
@JavascriptInterface
|
||||||
fun getVariable(): String {
|
fun getVariable(): String {
|
||||||
|
getTemporaryVariable()?.let {
|
||||||
|
return it
|
||||||
|
}
|
||||||
return CacheManager.get("sourceVariable_${getKey()}") ?: ""
|
return CacheManager.get("sourceVariable_${getKey()}") ?: ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun setTemporaryVariable(variable: String?) {
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getTemporaryVariable(): String? {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 保存数据
|
* 保存数据
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import android.os.Parcelable
|
|||||||
import android.text.TextUtils
|
import android.text.TextUtils
|
||||||
import androidx.room.ColumnInfo
|
import androidx.room.ColumnInfo
|
||||||
import androidx.room.Entity
|
import androidx.room.Entity
|
||||||
|
import androidx.room.Ignore
|
||||||
import androidx.room.Index
|
import androidx.room.Index
|
||||||
import androidx.room.PrimaryKey
|
import androidx.room.PrimaryKey
|
||||||
import androidx.room.TypeConverter
|
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.GSON
|
||||||
import io.legado.app.utils.fromJsonObject
|
import io.legado.app.utils.fromJsonObject
|
||||||
import io.legado.app.utils.splitNotBlank
|
import io.legado.app.utils.splitNotBlank
|
||||||
|
import kotlinx.parcelize.IgnoredOnParcel
|
||||||
import kotlinx.parcelize.Parcelize
|
import kotlinx.parcelize.Parcelize
|
||||||
|
|
||||||
@Suppress("unused")
|
@Suppress("unused")
|
||||||
@@ -103,6 +105,10 @@ data class BookSource(
|
|||||||
var homepageModules: String? = null
|
var homepageModules: String? = null
|
||||||
) : Parcelable, BaseSource {
|
) : Parcelable, BaseSource {
|
||||||
|
|
||||||
|
@Ignore
|
||||||
|
@IgnoredOnParcel
|
||||||
|
private var temporaryVariable: String? = null
|
||||||
|
|
||||||
override fun getTag(): String {
|
override fun getTag(): String {
|
||||||
return bookSourceName
|
return bookSourceName
|
||||||
}
|
}
|
||||||
@@ -111,6 +117,14 @@ data class BookSource(
|
|||||||
return bookSourceUrl
|
return bookSourceUrl
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun setTemporaryVariable(variable: String?) {
|
||||||
|
temporaryVariable = variable
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getTemporaryVariable(): String? {
|
||||||
|
return temporaryVariable
|
||||||
|
}
|
||||||
|
|
||||||
override fun hashCode(): Int {
|
override fun hashCode(): Int {
|
||||||
return bookSourceUrl.hashCode()
|
return bookSourceUrl.hashCode()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package io.legado.app.domain.usecase
|
package io.legado.app.domain.usecase
|
||||||
|
|
||||||
|
import io.legado.app.data.entities.BookSource
|
||||||
import io.legado.app.data.entities.SearchBook
|
import io.legado.app.data.entities.SearchBook
|
||||||
import io.legado.app.domain.gateway.ExploreBooksGateway
|
import io.legado.app.domain.gateway.ExploreBooksGateway
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
@@ -23,15 +24,9 @@ class ExploreBooksUseCase(
|
|||||||
page: Int = 1,
|
page: Int = 1,
|
||||||
key: String? = null,
|
key: String? = null,
|
||||||
): ExploreResult = withContext(Dispatchers.IO) {
|
): ExploreResult = withContext(Dispatchers.IO) {
|
||||||
val base = gateway.getBookSource(sourceUrl)
|
val request = resolveRequest(sourceUrl, moduleUrl, args, key)
|
||||||
?: throw SourceNotFound(sourceUrl)
|
val books = gateway.exploreBooks(request.source, request.url, page, key = key)
|
||||||
val source = args?.let { base.copy().also { s -> s.setVariable(it) } } ?: base
|
ExploreResult(request.url, books)
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun executeForRanking(
|
suspend fun executeForRanking(
|
||||||
@@ -39,16 +34,13 @@ class ExploreBooksUseCase(
|
|||||||
moduleUrl: String?,
|
moduleUrl: String?,
|
||||||
args: String?
|
args: String?
|
||||||
): List<SearchBook> = withContext(Dispatchers.IO) {
|
): List<SearchBook> = withContext(Dispatchers.IO) {
|
||||||
val result = execute(sourceUrl, moduleUrl, args)
|
val request = resolveRequest(sourceUrl, moduleUrl, args)
|
||||||
var books = result.books
|
var books = gateway.exploreBooks(request.source, request.url, page = 1)
|
||||||
var page = 1
|
var page = 1
|
||||||
while (books.size < MAX_RANKING_BOOKS && page < MAX_RANKING_PAGES) {
|
while (books.size < MAX_RANKING_BOOKS && page < MAX_RANKING_PAGES) {
|
||||||
page++
|
page++
|
||||||
val next = try {
|
val next = try {
|
||||||
val source = gateway.getBookSource(sourceUrl)
|
gateway.exploreBooks(request.source, request.url, page)
|
||||||
?.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)
|
|
||||||
} catch (_: Exception) {
|
} catch (_: Exception) {
|
||||||
emptyList()
|
emptyList()
|
||||||
}
|
}
|
||||||
@@ -58,8 +50,29 @@ class ExploreBooksUseCase(
|
|||||||
books.take(MAX_RANKING_BOOKS)
|
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>)
|
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 SourceNotFound(url: String) : Exception("Source not found: ${url.take(60)}")
|
||||||
class NoExploreUrl(url: String) : Exception("No explore URL for source: ${url.take(60)}")
|
class NoExploreUrl(url: String) : Exception("No explore URL for source: ${url.take(60)}")
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user