mirror of
https://github.com/gedoor/legado.git
synced 2025-08-10 00:52:30 +00:00
优化
This commit is contained in:
16
app/src/main/java/io/legado/app/constant/SourceType.kt
Normal file
16
app/src/main/java/io/legado/app/constant/SourceType.kt
Normal file
@@ -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")
|
||||
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> {
|
||||
val groups = linkedSetOf<String>()
|
||||
list.forEach {
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
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.BookSource
|
||||
import io.legado.app.data.entities.RssSource
|
||||
import io.legado.app.model.SharedJsScope
|
||||
import org.mozilla.javascript.Scriptable
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
@@ -8,3 +11,11 @@ import kotlin.coroutines.CoroutineContext
|
||||
fun BaseSource.getShareScope(coroutineContext: CoroutineContext? = null): Scriptable? {
|
||||
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
|
||||
|
||||
import io.legado.app.constant.SourceType
|
||||
import io.legado.app.data.appDb
|
||||
import io.legado.app.data.entities.BaseSource
|
||||
import io.legado.app.data.entities.BookSource
|
||||
@@ -30,6 +31,29 @@ object SourceHelp {
|
||||
?: 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) {
|
||||
val rssSourcesGroup = rssSources.groupBy {
|
||||
is18Plus(it.sourceUrl)
|
||||
|
||||
@@ -82,6 +82,7 @@ object SourceVerificationHelp {
|
||||
putExtra("url", url)
|
||||
putExtra("sourceOrigin", source.getKey())
|
||||
putExtra("sourceName", source.getTag())
|
||||
putExtra("sourceType", source.getSourceType())
|
||||
putExtra("sourceVerificationEnable", saveResult)
|
||||
putExtra("refetchAfterSuccess", refetchAfterSuccess)
|
||||
IntentData.put(getVerificationResultKey(source), Thread.currentThread())
|
||||
|
||||
@@ -13,6 +13,7 @@ import androidx.core.view.isVisible
|
||||
import io.legado.app.R
|
||||
import io.legado.app.databinding.ViewMangaMenuBinding
|
||||
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.theme.bottomBackground
|
||||
import io.legado.app.model.ReadBook
|
||||
@@ -181,6 +182,7 @@ class MangaMenu @JvmOverloads constructor(
|
||||
putExtra("url", url)
|
||||
putExtra("sourceOrigin", bookSource?.bookSourceUrl)
|
||||
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.ReadTipConfig
|
||||
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.lib.dialogs.SelectItem
|
||||
import io.legado.app.lib.dialogs.alert
|
||||
@@ -1301,6 +1302,7 @@ class ReadBookActivity : BaseReadBookActivity(),
|
||||
putExtra("url", it)
|
||||
putExtra("sourceOrigin", bookSource?.bookSourceUrl)
|
||||
putExtra("sourceName", bookSource?.bookSourceName)
|
||||
putExtra("sourceType", bookSource?.getSourceType())
|
||||
}
|
||||
} 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.ThemeConfig
|
||||
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.theme.Selector
|
||||
import io.legado.app.lib.theme.accentColor
|
||||
@@ -354,6 +355,7 @@ class ReadMenu @JvmOverloads constructor(
|
||||
putExtra("url", url)
|
||||
putExtra("sourceOrigin", bookSource?.bookSourceUrl)
|
||||
putExtra("sourceName", bookSource?.bookSourceName)
|
||||
putExtra("sourceType", bookSource?.getSourceType())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,10 +9,12 @@ import android.webkit.WebView
|
||||
import androidx.documentfile.provider.DocumentFile
|
||||
import io.legado.app.base.BaseViewModel
|
||||
import io.legado.app.constant.AppConst
|
||||
import io.legado.app.constant.SourceType
|
||||
import io.legado.app.data.appDb
|
||||
import io.legado.app.exception.NoStackTraceException
|
||||
import io.legado.app.help.http.newCallResponseBody
|
||||
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.model.analyzeRule.AnalyzeUrl
|
||||
import io.legado.app.utils.DocumentUtils
|
||||
@@ -34,6 +36,7 @@ class WebViewModel(application: Application) : BaseViewModel(application) {
|
||||
var refetchAfterSuccess: Boolean = true
|
||||
var sourceName: String = ""
|
||||
var sourceOrigin: String = ""
|
||||
var sourceType = SourceType.book
|
||||
|
||||
fun initData(
|
||||
intent: Intent,
|
||||
@@ -45,9 +48,10 @@ class WebViewModel(application: Application) : BaseViewModel(application) {
|
||||
?: throw NoStackTraceException("url不能为空")
|
||||
sourceName = intent.getStringExtra("sourceName") ?: ""
|
||||
sourceOrigin = intent.getStringExtra("sourceOrigin") ?: ""
|
||||
sourceType = intent.getIntExtra("sourceType", SourceType.book)
|
||||
sourceVerificationEnable = intent.getBooleanExtra("sourceVerificationEnable", false)
|
||||
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)
|
||||
baseUrl = analyzeUrl.url
|
||||
headerMap.putAll(analyzeUrl.headerMap)
|
||||
@@ -127,7 +131,7 @@ class WebViewModel(application: Application) : BaseViewModel(application) {
|
||||
|
||||
fun disableSource(block: () -> Unit) {
|
||||
execute {
|
||||
appDb.bookSourceDao.enable(sourceOrigin, false)
|
||||
SourceHelp.enableSource(sourceOrigin, sourceType, false)
|
||||
}.onSuccess {
|
||||
block.invoke()
|
||||
}
|
||||
@@ -135,7 +139,7 @@ class WebViewModel(application: Application) : BaseViewModel(application) {
|
||||
|
||||
fun deleteSource(block: () -> Unit) {
|
||||
execute {
|
||||
appDb.bookSourceDao.delete(sourceOrigin)
|
||||
SourceHelp.deleteSource(sourceOrigin, sourceType)
|
||||
}.onSuccess {
|
||||
block.invoke()
|
||||
}
|
||||
|
||||
@@ -7,21 +7,19 @@ import android.util.Base64
|
||||
import android.webkit.URLUtil
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.script.rhino.runScriptWithContext
|
||||
import io.legado.app.base.BaseViewModel
|
||||
import io.legado.app.constant.AppConst
|
||||
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.RssSource
|
||||
import io.legado.app.data.entities.RssStar
|
||||
import io.legado.app.exception.NoStackTraceException
|
||||
import io.legado.app.help.JsExtensions
|
||||
import io.legado.app.help.TTS
|
||||
import io.legado.app.help.http.newCallResponseBody
|
||||
import io.legado.app.help.http.okHttpClient
|
||||
import io.legado.app.model.analyzeRule.AnalyzeUrl
|
||||
import io.legado.app.model.rss.Rss
|
||||
import com.script.rhino.runScriptWithContext
|
||||
import io.legado.app.utils.toastOnUi
|
||||
import io.legado.app.utils.writeBytes
|
||||
import kotlinx.coroutines.Dispatchers.IO
|
||||
@@ -30,7 +28,7 @@ import java.util.Date
|
||||
import kotlin.coroutines.coroutineContext
|
||||
|
||||
|
||||
class ReadRssViewModel(application: Application) : BaseViewModel(application), JsExtensions {
|
||||
class ReadRssViewModel(application: Application) : BaseViewModel(application) {
|
||||
var rssSource: RssSource? = null
|
||||
var rssArticle: RssArticle? = null
|
||||
var tts: TTS? = null
|
||||
@@ -41,10 +39,6 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application), J
|
||||
val upStarMenuData = MutableLiveData<Boolean>()
|
||||
var headerMap: Map<String, String> = emptyMap()
|
||||
|
||||
override fun getSource(): BaseSource? {
|
||||
return rssSource
|
||||
}
|
||||
|
||||
fun initData(intent: Intent) {
|
||||
execute {
|
||||
val origin = intent.getStringExtra("origin") ?: return@execute
|
||||
|
||||
Reference in New Issue
Block a user