From b484a1096e8ee8ab0cf72ee9edcf856278ce4e0b Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 19 Sep 2022 18:29:59 +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/BookSource.kt | 100 +++++++++++------- .../legado/app/service/CheckSourceService.kt | 2 +- .../source/debug/BookSourceDebugActivity.kt | 22 ++-- .../app/ui/main/explore/ExploreAdapter.kt | 7 +- .../app/ui/main/explore/ExploreFragment.kt | 4 - 5 files changed, 78 insertions(+), 57 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/entities/BookSource.kt b/app/src/main/java/io/legado/app/data/entities/BookSource.kt index 0184b8688..f3c38aaec 100644 --- a/app/src/main/java/io/legado/app/data/entities/BookSource.kt +++ b/app/src/main/java/io/legado/app/data/entities/BookSource.kt @@ -8,6 +8,10 @@ import io.legado.app.constant.BookType import io.legado.app.data.entities.rule.* import io.legado.app.help.SourceAnalyzer import io.legado.app.utils.* +import kotlinx.coroutines.Dispatchers.IO +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.sync.withLock +import kotlinx.coroutines.withContext import kotlinx.parcelize.IgnoredOnParcel import kotlinx.parcelize.Parcelize import java.io.InputStream @@ -89,46 +93,66 @@ data class BookSource( return bookSourceUrl } - @delegate:Transient - @delegate:Ignore + @Ignore @IgnoredOnParcel - val exploreKinds: List by lazy { - val exploreUrl = exploreUrl ?: return@lazy emptyList() - val kinds = arrayListOf() - var ruleStr = exploreUrl - if (ruleStr.isNotBlank()) { - kotlin.runCatching { - if (exploreUrl.startsWith("", false) - || exploreUrl.startsWith("@js:", false) - ) { - val aCache = ACache.get("explore") - ruleStr = aCache.getAsString(bookSourceUrl) ?: "" - if (ruleStr.isBlank()) { - val jsStr = if (exploreUrl.startsWith("@")) { - exploreUrl.substring(4) - } else { - exploreUrl.substring(4, exploreUrl.lastIndexOf("<")) - } - ruleStr = evalJS(jsStr).toString().trim() - aCache.put(bookSourceUrl, ruleStr) - } - } - if (ruleStr.isJsonArray()) { - GSON.fromJsonArray(ruleStr).getOrThrow()?.let { - kinds.addAll(it) - } - } else { - ruleStr.split("(&&|\n)+".toRegex()).forEach { kindStr -> - val kindCfg = kindStr.split("::") - kinds.add(ExploreKind(kindCfg.first(), kindCfg.getOrNull(1))) - } - } - }.onFailure { - kinds.add(ExploreKind("ERROR:${it.localizedMessage}", it.stackTraceToString())) - it.printOnDebug() - } + private var exploreKinds: List? = null + + @Ignore + @IgnoredOnParcel + private val mutex = Mutex() + + suspend fun exploreKinds(): List { + exploreKinds?.let { return it } + val exploreUrl = exploreUrl + if (exploreUrl.isNullOrBlank()) { + return emptyList() + } + mutex.withLock { + exploreKinds?.let { return it } + val kinds = arrayListOf() + var ruleStr: String = exploreUrl + withContext(IO) { + kotlin.runCatching { + if (exploreUrl.startsWith("", false) + || exploreUrl.startsWith("@js:", false) + ) { + val aCache = ACache.get("explore") + ruleStr = aCache.getAsString(bookSourceUrl) ?: "" + if (ruleStr.isBlank()) { + val jsStr = if (exploreUrl.startsWith("@")) { + exploreUrl.substring(4) + } else { + exploreUrl.substring(4, exploreUrl.lastIndexOf("<")) + } + ruleStr = evalJS(jsStr).toString().trim() + aCache.put(bookSourceUrl, ruleStr) + } + } + if (ruleStr.isJsonArray()) { + GSON.fromJsonArray(ruleStr).getOrThrow()?.let { + kinds.addAll(it) + } + } else { + ruleStr.split("(&&|\n)+".toRegex()).forEach { kindStr -> + val kindCfg = kindStr.split("::") + kinds.add(ExploreKind(kindCfg.first(), kindCfg.getOrNull(1))) + } + } + }.onFailure { + kinds.add(ExploreKind("ERROR:${it.localizedMessage}", it.stackTraceToString())) + it.printOnDebug() + } + } + exploreKinds = kinds + return kinds + } + } + + suspend fun clearExploreKindsCache() { + withContext(IO) { + ACache.get("explore").remove(bookSourceUrl) + exploreKinds = null } - return@lazy kinds } override fun hashCode(): Int { diff --git a/app/src/main/java/io/legado/app/service/CheckSourceService.kt b/app/src/main/java/io/legado/app/service/CheckSourceService.kt index d8863b2e7..e54012d9d 100644 --- a/app/src/main/java/io/legado/app/service/CheckSourceService.kt +++ b/app/src/main/java/io/legado/app/service/CheckSourceService.kt @@ -155,7 +155,7 @@ class CheckSourceService : BaseService() { } //校验发现书籍 if (CheckSource.checkDiscovery) { - val exs = source.exploreKinds + val exs = source.exploreKinds() var url: String? = null for (ex in exs) { url = ex.url diff --git a/app/src/main/java/io/legado/app/ui/book/source/debug/BookSourceDebugActivity.kt b/app/src/main/java/io/legado/app/ui/book/source/debug/BookSourceDebugActivity.kt index 1aa54a875..72300d96d 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/debug/BookSourceDebugActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/debug/BookSourceDebugActivity.kt @@ -88,16 +88,6 @@ class BookSourceDebugActivity : VMBaseActivity upKindList(flexbox, item.bookSourceUrl, kindList) }.onFinally { @@ -167,9 +167,9 @@ class ExploreAdapter(context: Context, val callBack: CallBack) : putExtra("key", source.bookSourceUrl) } R.id.menu_refresh -> Coroutine.async(callBack.scope) { - ACache.get("explore").remove(source.bookSourceUrl) + source.clearExploreKindsCache() }.onSuccess { - callBack.refreshData() + notifyItemChanged(position) } R.id.menu_del -> callBack.deleteSource(source) } @@ -181,7 +181,6 @@ class ExploreAdapter(context: Context, val callBack: CallBack) : interface CallBack { val scope: CoroutineScope - fun refreshData() fun scrollTo(pos: Int) fun openExplore(sourceUrl: String, title: String, exploreUrl: String?) fun editSource(sourceUrl: String) diff --git a/app/src/main/java/io/legado/app/ui/main/explore/ExploreFragment.kt b/app/src/main/java/io/legado/app/ui/main/explore/ExploreFragment.kt index d9d29b4b0..7622a2d40 100644 --- a/app/src/main/java/io/legado/app/ui/main/explore/ExploreFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/explore/ExploreFragment.kt @@ -157,10 +157,6 @@ class ExploreFragment : VMBaseFragment(R.layout.fragment_explo } } - override fun refreshData() { - upExploreData(searchView.query?.toString()) - } - override fun scrollTo(pos: Int) { (binding.rvFind.layoutManager as LinearLayoutManager).scrollToPositionWithOffset(pos, 0) }