This commit is contained in:
Horis
2025-04-12 16:15:55 +08:00
parent ff8b55b7ac
commit abe3e5fa3e
10 changed files with 70 additions and 11 deletions
@@ -0,0 +1,16 @@
package io.legado.app.constant
import androidx.annotation.IntDef
@Suppress("ConstPropertyName")
object SourceType {
const val book = 0
const val rss = 1
@Target(AnnotationTarget.VALUE_PARAMETER)
@Retention(AnnotationRetention.SOURCE)
@IntDef(book, rss)
annotation class Type
}
@@ -125,6 +125,9 @@ interface RssSourceDao {
@Query("select 1 from rssSources where sourceUrl = :key") @Query("select 1 from rssSources where sourceUrl = :key")
fun has(key: String): Boolean? fun has(key: String): Boolean?
@Query("update rssSources set enabled = :enable where sourceUrl = :sourceUrl")
fun enable(sourceUrl: String, enable: Boolean)
private fun dealGroups(list: List<String>): List<String> { private fun dealGroups(list: List<String>): List<String> {
val groups = linkedSetOf<String>() val groups = linkedSetOf<String>()
list.forEach { list.forEach {
@@ -1,6 +1,9 @@
package io.legado.app.help.source package io.legado.app.help.source
import io.legado.app.constant.SourceType
import io.legado.app.data.entities.BaseSource import io.legado.app.data.entities.BaseSource
import io.legado.app.data.entities.BookSource
import io.legado.app.data.entities.RssSource
import io.legado.app.model.SharedJsScope import io.legado.app.model.SharedJsScope
import org.mozilla.javascript.Scriptable import org.mozilla.javascript.Scriptable
import kotlin.coroutines.CoroutineContext import kotlin.coroutines.CoroutineContext
@@ -8,3 +11,11 @@ import kotlin.coroutines.CoroutineContext
fun BaseSource.getShareScope(coroutineContext: CoroutineContext? = null): Scriptable? { fun BaseSource.getShareScope(coroutineContext: CoroutineContext? = null): Scriptable? {
return SharedJsScope.getScope(jsLib, coroutineContext) return SharedJsScope.getScope(jsLib, coroutineContext)
} }
fun BaseSource.getSourceType(): Int {
return when (this) {
is BookSource -> SourceType.book
is RssSource -> SourceType.rss
else -> error("unknown source type.")
}
}
@@ -1,5 +1,6 @@
package io.legado.app.help.source package io.legado.app.help.source
import io.legado.app.constant.SourceType
import io.legado.app.data.appDb import io.legado.app.data.appDb
import io.legado.app.data.entities.BaseSource import io.legado.app.data.entities.BaseSource
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
@@ -30,6 +31,29 @@ object SourceHelp {
?: appDb.rssSourceDao.getByKey(key) ?: appDb.rssSourceDao.getByKey(key)
} }
fun getSource(key: String?, @SourceType.Type type: Int): BaseSource? {
key ?: return null
return when (type) {
SourceType.book -> appDb.bookSourceDao.getBookSource(key)
SourceType.rss -> appDb.rssSourceDao.getByKey(key)
else -> null
}
}
fun deleteSource(key: String, @SourceType.Type type: Int) {
when (type) {
SourceType.book -> appDb.bookSourceDao.delete(key)
SourceType.rss -> appDb.rssSourceDao.delete(key)
}
}
fun enableSource(key: String, @SourceType.Type type: Int, enable: Boolean) {
when (type) {
SourceType.book -> appDb.bookSourceDao.enable(key, enable)
SourceType.rss -> appDb.rssSourceDao.enable(key, enable)
}
}
fun insertRssSource(vararg rssSources: RssSource) { fun insertRssSource(vararg rssSources: RssSource) {
val rssSourcesGroup = rssSources.groupBy { val rssSourcesGroup = rssSources.groupBy {
is18Plus(it.sourceUrl) is18Plus(it.sourceUrl)
@@ -82,6 +82,7 @@ object SourceVerificationHelp {
putExtra("url", url) putExtra("url", url)
putExtra("sourceOrigin", source.getKey()) putExtra("sourceOrigin", source.getKey())
putExtra("sourceName", source.getTag()) putExtra("sourceName", source.getTag())
putExtra("sourceType", source.getSourceType())
putExtra("sourceVerificationEnable", saveResult) putExtra("sourceVerificationEnable", saveResult)
putExtra("refetchAfterSuccess", refetchAfterSuccess) putExtra("refetchAfterSuccess", refetchAfterSuccess)
IntentData.put(getVerificationResultKey(source), Thread.currentThread()) IntentData.put(getVerificationResultKey(source), Thread.currentThread())
@@ -13,6 +13,7 @@ import androidx.core.view.isVisible
import io.legado.app.R import io.legado.app.R
import io.legado.app.databinding.ViewMangaMenuBinding import io.legado.app.databinding.ViewMangaMenuBinding
import io.legado.app.help.config.AppConfig import io.legado.app.help.config.AppConfig
import io.legado.app.help.source.getSourceType
import io.legado.app.lib.dialogs.alert import io.legado.app.lib.dialogs.alert
import io.legado.app.lib.theme.bottomBackground import io.legado.app.lib.theme.bottomBackground
import io.legado.app.model.ReadBook import io.legado.app.model.ReadBook
@@ -181,6 +182,7 @@ class MangaMenu @JvmOverloads constructor(
putExtra("url", url) putExtra("url", url)
putExtra("sourceOrigin", bookSource?.bookSourceUrl) putExtra("sourceOrigin", bookSource?.bookSourceUrl)
putExtra("sourceName", bookSource?.bookSourceName) putExtra("sourceName", bookSource?.bookSourceName)
putExtra("sourceType", bookSource?.getSourceType())
} }
} }
} }
@@ -52,6 +52,7 @@ import io.legado.app.help.config.AppConfig
import io.legado.app.help.config.ReadBookConfig import io.legado.app.help.config.ReadBookConfig
import io.legado.app.help.config.ReadTipConfig import io.legado.app.help.config.ReadTipConfig
import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.coroutine.Coroutine
import io.legado.app.help.source.getSourceType
import io.legado.app.help.storage.Backup import io.legado.app.help.storage.Backup
import io.legado.app.lib.dialogs.SelectItem import io.legado.app.lib.dialogs.SelectItem
import io.legado.app.lib.dialogs.alert import io.legado.app.lib.dialogs.alert
@@ -1301,6 +1302,7 @@ class ReadBookActivity : BaseReadBookActivity(),
putExtra("url", it) putExtra("url", it)
putExtra("sourceOrigin", bookSource?.bookSourceUrl) putExtra("sourceOrigin", bookSource?.bookSourceUrl)
putExtra("sourceName", bookSource?.bookSourceName) putExtra("sourceName", bookSource?.bookSourceName)
putExtra("sourceType", bookSource?.getSourceType())
} }
} else if (it.isTrue()) { } else if (it.isTrue()) {
//购买成功后刷新目录 //购买成功后刷新目录
@@ -23,6 +23,7 @@ import io.legado.app.help.config.LocalConfig
import io.legado.app.help.config.ReadBookConfig import io.legado.app.help.config.ReadBookConfig
import io.legado.app.help.config.ThemeConfig import io.legado.app.help.config.ThemeConfig
import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.coroutine.Coroutine
import io.legado.app.help.source.getSourceType
import io.legado.app.lib.dialogs.alert import io.legado.app.lib.dialogs.alert
import io.legado.app.lib.theme.Selector import io.legado.app.lib.theme.Selector
import io.legado.app.lib.theme.accentColor import io.legado.app.lib.theme.accentColor
@@ -354,6 +355,7 @@ class ReadMenu @JvmOverloads constructor(
putExtra("url", url) putExtra("url", url)
putExtra("sourceOrigin", bookSource?.bookSourceUrl) putExtra("sourceOrigin", bookSource?.bookSourceUrl)
putExtra("sourceName", bookSource?.bookSourceName) putExtra("sourceName", bookSource?.bookSourceName)
putExtra("sourceType", bookSource?.getSourceType())
} }
} }
} }
@@ -9,10 +9,12 @@ import android.webkit.WebView
import androidx.documentfile.provider.DocumentFile import androidx.documentfile.provider.DocumentFile
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.constant.AppConst import io.legado.app.constant.AppConst
import io.legado.app.constant.SourceType
import io.legado.app.data.appDb import io.legado.app.data.appDb
import io.legado.app.exception.NoStackTraceException import io.legado.app.exception.NoStackTraceException
import io.legado.app.help.http.newCallResponseBody import io.legado.app.help.http.newCallResponseBody
import io.legado.app.help.http.okHttpClient import io.legado.app.help.http.okHttpClient
import io.legado.app.help.source.SourceHelp
import io.legado.app.help.source.SourceVerificationHelp import io.legado.app.help.source.SourceVerificationHelp
import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.utils.DocumentUtils import io.legado.app.utils.DocumentUtils
@@ -34,6 +36,7 @@ class WebViewModel(application: Application) : BaseViewModel(application) {
var refetchAfterSuccess: Boolean = true var refetchAfterSuccess: Boolean = true
var sourceName: String = "" var sourceName: String = ""
var sourceOrigin: String = "" var sourceOrigin: String = ""
var sourceType = SourceType.book
fun initData( fun initData(
intent: Intent, intent: Intent,
@@ -45,9 +48,10 @@ class WebViewModel(application: Application) : BaseViewModel(application) {
?: throw NoStackTraceException("url不能为空") ?: throw NoStackTraceException("url不能为空")
sourceName = intent.getStringExtra("sourceName") ?: "" sourceName = intent.getStringExtra("sourceName") ?: ""
sourceOrigin = intent.getStringExtra("sourceOrigin") ?: "" sourceOrigin = intent.getStringExtra("sourceOrigin") ?: ""
sourceType = intent.getIntExtra("sourceType", SourceType.book)
sourceVerificationEnable = intent.getBooleanExtra("sourceVerificationEnable", false) sourceVerificationEnable = intent.getBooleanExtra("sourceVerificationEnable", false)
refetchAfterSuccess = intent.getBooleanExtra("refetchAfterSuccess", true) refetchAfterSuccess = intent.getBooleanExtra("refetchAfterSuccess", true)
val source = appDb.bookSourceDao.getBookSource(sourceOrigin) val source = SourceHelp.getSource(sourceOrigin, sourceType)
val analyzeUrl = AnalyzeUrl(url, source = source, coroutineContext = coroutineContext) val analyzeUrl = AnalyzeUrl(url, source = source, coroutineContext = coroutineContext)
baseUrl = analyzeUrl.url baseUrl = analyzeUrl.url
headerMap.putAll(analyzeUrl.headerMap) headerMap.putAll(analyzeUrl.headerMap)
@@ -127,7 +131,7 @@ class WebViewModel(application: Application) : BaseViewModel(application) {
fun disableSource(block: () -> Unit) { fun disableSource(block: () -> Unit) {
execute { execute {
appDb.bookSourceDao.enable(sourceOrigin, false) SourceHelp.enableSource(sourceOrigin, sourceType, false)
}.onSuccess { }.onSuccess {
block.invoke() block.invoke()
} }
@@ -135,7 +139,7 @@ class WebViewModel(application: Application) : BaseViewModel(application) {
fun deleteSource(block: () -> Unit) { fun deleteSource(block: () -> Unit) {
execute { execute {
appDb.bookSourceDao.delete(sourceOrigin) SourceHelp.deleteSource(sourceOrigin, sourceType)
}.onSuccess { }.onSuccess {
block.invoke() block.invoke()
} }
@@ -7,21 +7,19 @@ import android.util.Base64
import android.webkit.URLUtil import android.webkit.URLUtil
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import com.script.rhino.runScriptWithContext
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.constant.AppConst import io.legado.app.constant.AppConst
import io.legado.app.data.appDb import io.legado.app.data.appDb
import io.legado.app.data.entities.BaseSource
import io.legado.app.data.entities.RssArticle import io.legado.app.data.entities.RssArticle
import io.legado.app.data.entities.RssSource import io.legado.app.data.entities.RssSource
import io.legado.app.data.entities.RssStar import io.legado.app.data.entities.RssStar
import io.legado.app.exception.NoStackTraceException import io.legado.app.exception.NoStackTraceException
import io.legado.app.help.JsExtensions
import io.legado.app.help.TTS import io.legado.app.help.TTS
import io.legado.app.help.http.newCallResponseBody import io.legado.app.help.http.newCallResponseBody
import io.legado.app.help.http.okHttpClient import io.legado.app.help.http.okHttpClient
import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.model.rss.Rss import io.legado.app.model.rss.Rss
import com.script.rhino.runScriptWithContext
import io.legado.app.utils.toastOnUi import io.legado.app.utils.toastOnUi
import io.legado.app.utils.writeBytes import io.legado.app.utils.writeBytes
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
@@ -30,7 +28,7 @@ import java.util.Date
import kotlin.coroutines.coroutineContext import kotlin.coroutines.coroutineContext
class ReadRssViewModel(application: Application) : BaseViewModel(application), JsExtensions { class ReadRssViewModel(application: Application) : BaseViewModel(application) {
var rssSource: RssSource? = null var rssSource: RssSource? = null
var rssArticle: RssArticle? = null var rssArticle: RssArticle? = null
var tts: TTS? = null var tts: TTS? = null
@@ -41,10 +39,6 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application), J
val upStarMenuData = MutableLiveData<Boolean>() val upStarMenuData = MutableLiveData<Boolean>()
var headerMap: Map<String, String> = emptyMap() var headerMap: Map<String, String> = emptyMap()
override fun getSource(): BaseSource? {
return rssSource
}
fun initData(intent: Intent) { fun initData(intent: Intent) {
execute { execute {
val origin = intent.getStringExtra("origin") ?: return@execute val origin = intent.getStringExtra("origin") ?: return@execute