mirror of
https://github.com/gedoor/legado.git
synced 2025-08-10 00:52:30 +00:00
优化
This commit is contained in:
@@ -404,7 +404,7 @@ open class WebDav(
|
||||
val supportBasicAuth = headers.any {
|
||||
it.startsWith("Basic", ignoreCase = true)
|
||||
}
|
||||
if (!supportBasicAuth) {
|
||||
if (headers.isNotEmpty() && !supportBasicAuth) {
|
||||
AppLog.put("服务器不支持BasicAuth认证")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,17 +57,16 @@ object BookContent {
|
||||
analyzeRule.chapter = bookChapter
|
||||
analyzeRule.nextChapterUrl = mNextChapterUrl
|
||||
coroutineContext.ensureActive()
|
||||
contentRule.title?.let {
|
||||
if (it.isNotBlank()) {
|
||||
val title = analyzeRule.runCatching {
|
||||
getString(it)
|
||||
}.onFailure { e ->
|
||||
Debug.log(bookSource.bookSourceUrl, "获取标题出错, ${e.localizedMessage}")
|
||||
}.getOrNull()
|
||||
if (!title.isNullOrBlank()) {
|
||||
bookChapter.title = title
|
||||
appDb.bookChapterDao.upDate(bookChapter)
|
||||
}
|
||||
val titleRule = contentRule.title
|
||||
if (!titleRule.isNullOrBlank()) {
|
||||
val title = analyzeRule.runCatching {
|
||||
getString(titleRule)
|
||||
}.onFailure {
|
||||
Debug.log(bookSource.bookSourceUrl, "获取标题出错, ${it.localizedMessage}")
|
||||
}.getOrNull()
|
||||
if (!title.isNullOrBlank()) {
|
||||
bookChapter.title = title
|
||||
appDb.bookChapterDao.upDate(bookChapter)
|
||||
}
|
||||
}
|
||||
var contentData = analyzeContent(
|
||||
|
||||
@@ -107,7 +107,7 @@ class RssArticlesFragment() : VMBaseFragment<RssArticlesViewModel>(R.layout.frag
|
||||
|
||||
private fun loadArticles() {
|
||||
activityViewModel.rssSource?.let {
|
||||
viewModel.loadContent(it)
|
||||
viewModel.loadArticles(it)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,8 +11,7 @@ import io.legado.app.data.entities.RssArticle
|
||||
import io.legado.app.data.entities.RssSource
|
||||
import io.legado.app.model.rss.Rss
|
||||
import io.legado.app.utils.stackTraceStr
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlinx.coroutines.Dispatchers.IO
|
||||
|
||||
|
||||
class RssArticlesViewModel(application: Application) : BaseViewModel(application) {
|
||||
@@ -32,69 +31,62 @@ class RssArticlesViewModel(application: Application) : BaseViewModel(application
|
||||
}
|
||||
}
|
||||
|
||||
fun loadContent(rssSource: RssSource) {
|
||||
fun loadArticles(rssSource: RssSource) {
|
||||
isLoading = true
|
||||
page = 1
|
||||
order = System.currentTimeMillis()
|
||||
Rss.getArticles(viewModelScope, sortName, sortUrl, rssSource, page)
|
||||
.onSuccess(Dispatchers.IO) {
|
||||
nextPageUrl = it.second
|
||||
it.first.let { list ->
|
||||
list.forEach { rssArticle ->
|
||||
rssArticle.order = order--
|
||||
}
|
||||
appDb.rssArticleDao.insert(*list.toTypedArray())
|
||||
if (!rssSource.ruleNextPage.isNullOrEmpty()) {
|
||||
appDb.rssArticleDao.clearOld(rssSource.sourceUrl, sortName, order)
|
||||
}
|
||||
val hasMore = list.isNotEmpty() && !rssSource.ruleNextPage.isNullOrEmpty()
|
||||
loadFinallyLiveData.postValue(hasMore)
|
||||
isLoading = false
|
||||
}
|
||||
}.onError {
|
||||
loadFinallyLiveData.postValue(false)
|
||||
AppLog.put("rss获取内容失败", it)
|
||||
loadErrorLiveData.postValue(it.stackTraceStr)
|
||||
Rss.getArticles(viewModelScope, sortName, sortUrl, rssSource, page).onSuccess(IO) {
|
||||
nextPageUrl = it.second
|
||||
val articles = it.first
|
||||
articles.forEach { rssArticle ->
|
||||
rssArticle.order = order--
|
||||
}
|
||||
appDb.rssArticleDao.insert(*articles.toTypedArray())
|
||||
if (!rssSource.ruleNextPage.isNullOrEmpty()) {
|
||||
appDb.rssArticleDao.clearOld(rssSource.sourceUrl, sortName, order)
|
||||
}
|
||||
val hasMore = articles.isNotEmpty() && !rssSource.ruleNextPage.isNullOrEmpty()
|
||||
loadFinallyLiveData.postValue(hasMore)
|
||||
isLoading = false
|
||||
}.onError {
|
||||
loadFinallyLiveData.postValue(false)
|
||||
AppLog.put("rss获取内容失败", it)
|
||||
loadErrorLiveData.postValue(it.stackTraceStr)
|
||||
}
|
||||
}
|
||||
|
||||
fun loadMore(rssSource: RssSource) {
|
||||
isLoading = true
|
||||
page++
|
||||
val pageUrl = nextPageUrl
|
||||
if (!pageUrl.isNullOrEmpty()) {
|
||||
Rss.getArticles(viewModelScope, sortName, pageUrl, rssSource, page)
|
||||
.onSuccess(Dispatchers.IO) {
|
||||
nextPageUrl = it.second
|
||||
loadMoreSuccess(it.first)
|
||||
}
|
||||
.onError {
|
||||
loadFinallyLiveData.postValue(false)
|
||||
AppLog.put("rss获取内容失败", it)
|
||||
loadErrorLiveData.postValue(it.stackTraceStr)
|
||||
}
|
||||
} else {
|
||||
if (pageUrl.isNullOrEmpty()) {
|
||||
loadFinallyLiveData.postValue(false)
|
||||
return
|
||||
}
|
||||
Rss.getArticles(viewModelScope, sortName, pageUrl, rssSource, page).onSuccess(IO) {
|
||||
nextPageUrl = it.second
|
||||
loadMoreSuccess(it.first)
|
||||
}.onError {
|
||||
loadFinallyLiveData.postValue(false)
|
||||
AppLog.put("rss获取内容失败", it)
|
||||
loadErrorLiveData.postValue(it.stackTraceStr)
|
||||
}
|
||||
}
|
||||
|
||||
private fun loadMoreSuccess(articles: MutableList<RssArticle>) {
|
||||
articles.let { list ->
|
||||
if (list.isEmpty()) {
|
||||
loadFinallyLiveData.postValue(false)
|
||||
return@let
|
||||
}
|
||||
val firstArticle = list.first()
|
||||
val dbArticle = appDb.rssArticleDao
|
||||
.get(firstArticle.origin, firstArticle.link)
|
||||
if (dbArticle != null) {
|
||||
loadFinallyLiveData.postValue(false)
|
||||
} else {
|
||||
list.forEach { rssArticle ->
|
||||
rssArticle.order = order--
|
||||
}
|
||||
appDb.rssArticleDao.insert(*list.toTypedArray())
|
||||
if (articles.isEmpty()) {
|
||||
loadFinallyLiveData.postValue(false)
|
||||
return
|
||||
}
|
||||
val firstArticle = articles.first()
|
||||
val dbArticle = appDb.rssArticleDao.get(firstArticle.origin, firstArticle.link)
|
||||
if (dbArticle != null) {
|
||||
loadFinallyLiveData.postValue(false)
|
||||
} else {
|
||||
articles.forEach {
|
||||
it.order = order--
|
||||
}
|
||||
appDb.rssArticleDao.insert(*articles.toTypedArray())
|
||||
}
|
||||
isLoading = false
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ import io.legado.app.data.appDb
|
||||
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.TTS
|
||||
import io.legado.app.help.http.newCallResponseBody
|
||||
import io.legado.app.help.http.okHttpClient
|
||||
@@ -42,38 +43,35 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application) {
|
||||
|
||||
fun initData(intent: Intent) {
|
||||
execute {
|
||||
val origin = intent.getStringExtra("origin")
|
||||
val origin = intent.getStringExtra("origin") ?: return@execute
|
||||
val link = intent.getStringExtra("link")
|
||||
origin?.let {
|
||||
rssSource = appDb.rssSourceDao.getByKey(origin)
|
||||
if (link != null) {
|
||||
rssStar = appDb.rssStarDao.get(origin, link)
|
||||
rssArticle = rssStar?.toRssArticle() ?: appDb.rssArticleDao.get(origin, link)
|
||||
rssArticle?.let { rssArticle ->
|
||||
if (!rssArticle.description.isNullOrBlank()) {
|
||||
contentLiveData.postValue(rssArticle.description!!)
|
||||
} else {
|
||||
rssSource?.let {
|
||||
val ruleContent = it.ruleContent
|
||||
if (!ruleContent.isNullOrBlank()) {
|
||||
loadContent(rssArticle, ruleContent)
|
||||
} else {
|
||||
loadUrl(rssArticle.link, rssArticle.origin)
|
||||
}
|
||||
} ?: loadUrl(rssArticle.link, rssArticle.origin)
|
||||
}
|
||||
}
|
||||
rssSource = appDb.rssSourceDao.getByKey(origin)
|
||||
if (link != null) {
|
||||
rssStar = appDb.rssStarDao.get(origin, link)
|
||||
rssArticle = rssStar?.toRssArticle() ?: appDb.rssArticleDao.get(origin, link)
|
||||
val rssArticle = rssArticle ?: return@execute
|
||||
if (!rssArticle.description.isNullOrBlank()) {
|
||||
contentLiveData.postValue(rssArticle.description!!)
|
||||
} else {
|
||||
val ruleContent = rssSource?.ruleContent
|
||||
if (ruleContent.isNullOrBlank()) {
|
||||
loadUrl(origin, origin)
|
||||
} else {
|
||||
val rssArticle = RssArticle()
|
||||
rssArticle.origin = origin
|
||||
rssArticle.link = origin
|
||||
rssArticle.title = rssSource!!.sourceName
|
||||
loadContent(rssArticle, ruleContent)
|
||||
}
|
||||
rssSource?.let {
|
||||
val ruleContent = it.ruleContent
|
||||
if (!ruleContent.isNullOrBlank()) {
|
||||
loadContent(rssArticle, ruleContent)
|
||||
} else {
|
||||
loadUrl(rssArticle.link, rssArticle.origin)
|
||||
}
|
||||
} ?: loadUrl(rssArticle.link, rssArticle.origin)
|
||||
}
|
||||
} else {
|
||||
val ruleContent = rssSource?.ruleContent
|
||||
if (ruleContent.isNullOrBlank()) {
|
||||
loadUrl(origin, origin)
|
||||
} else {
|
||||
val rssArticle = RssArticle()
|
||||
rssArticle.origin = origin
|
||||
rssArticle.link = origin
|
||||
rssArticle.title = rssSource!!.sourceName
|
||||
loadContent(rssArticle, ruleContent)
|
||||
}
|
||||
}
|
||||
}.onFinally {
|
||||
@@ -91,18 +89,17 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application) {
|
||||
}
|
||||
|
||||
private fun loadContent(rssArticle: RssArticle, ruleContent: String) {
|
||||
rssSource?.let { source ->
|
||||
Rss.getContent(viewModelScope, rssArticle, ruleContent, source)
|
||||
.onSuccess(IO) { body ->
|
||||
rssArticle.description = body
|
||||
appDb.rssArticleDao.insert(rssArticle)
|
||||
rssStar?.let {
|
||||
it.description = body
|
||||
appDb.rssStarDao.insert(it)
|
||||
}
|
||||
contentLiveData.postValue(body)
|
||||
val source = rssSource ?: return
|
||||
Rss.getContent(viewModelScope, rssArticle, ruleContent, source)
|
||||
.onSuccess(IO) { body ->
|
||||
rssArticle.description = body
|
||||
appDb.rssArticleDao.insert(rssArticle)
|
||||
rssStar?.let {
|
||||
it.description = body
|
||||
appDb.rssStarDao.insert(it)
|
||||
}
|
||||
}
|
||||
contentLiveData.postValue(body)
|
||||
}
|
||||
}
|
||||
|
||||
fun refresh(finish: () -> Unit) {
|
||||
@@ -139,18 +136,8 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application) {
|
||||
webPic ?: return
|
||||
execute {
|
||||
val fileName = "${AppConst.fileNameFormat.format(Date(System.currentTimeMillis()))}.jpg"
|
||||
webData2bitmap(webPic)?.let { biteArray ->
|
||||
if (uri.isContentScheme()) {
|
||||
DocumentFile.fromTreeUri(context, uri)?.let { doc ->
|
||||
DocumentUtils.createFileIfNotExist(doc, fileName)
|
||||
?.writeBytes(context, biteArray)
|
||||
}
|
||||
} else {
|
||||
val dir = File(uri.path ?: uri.toString())
|
||||
val file = FileUtils.createFileIfNotExist(dir, fileName)
|
||||
file.writeBytes(biteArray)
|
||||
}
|
||||
} ?: throw Throwable("NULL")
|
||||
val byteArray = webData2bitmap(webPic) ?: throw NoStackTraceException("NULL")
|
||||
uri.writeBytes(context, fileName, byteArray)
|
||||
}.onError {
|
||||
context.toastOnUi("保存图片失败:${it.localizedMessage}")
|
||||
}.onSuccess {
|
||||
@@ -178,9 +165,11 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application) {
|
||||
$content
|
||||
""".trimIndent()
|
||||
}
|
||||
|
||||
content.contains("<style>".toRegex()) -> {
|
||||
content
|
||||
}
|
||||
|
||||
else -> {
|
||||
"""
|
||||
<style>
|
||||
|
||||
Reference in New Issue
Block a user