This commit is contained in:
Horis
2023-05-18 13:59:58 +08:00
parent 9eb9f23ec9
commit e4cead48f1
5 changed files with 95 additions and 115 deletions
@@ -404,7 +404,7 @@ open class WebDav(
val supportBasicAuth = headers.any { val supportBasicAuth = headers.any {
it.startsWith("Basic", ignoreCase = true) it.startsWith("Basic", ignoreCase = true)
} }
if (!supportBasicAuth) { if (headers.isNotEmpty() && !supportBasicAuth) {
AppLog.put("服务器不支持BasicAuth认证") AppLog.put("服务器不支持BasicAuth认证")
} }
} }
@@ -57,19 +57,18 @@ object BookContent {
analyzeRule.chapter = bookChapter analyzeRule.chapter = bookChapter
analyzeRule.nextChapterUrl = mNextChapterUrl analyzeRule.nextChapterUrl = mNextChapterUrl
coroutineContext.ensureActive() coroutineContext.ensureActive()
contentRule.title?.let { val titleRule = contentRule.title
if (it.isNotBlank()) { if (!titleRule.isNullOrBlank()) {
val title = analyzeRule.runCatching { val title = analyzeRule.runCatching {
getString(it) getString(titleRule)
}.onFailure { e -> }.onFailure {
Debug.log(bookSource.bookSourceUrl, "获取标题出错, ${e.localizedMessage}") Debug.log(bookSource.bookSourceUrl, "获取标题出错, ${it.localizedMessage}")
}.getOrNull() }.getOrNull()
if (!title.isNullOrBlank()) { if (!title.isNullOrBlank()) {
bookChapter.title = title bookChapter.title = title
appDb.bookChapterDao.upDate(bookChapter) appDb.bookChapterDao.upDate(bookChapter)
} }
} }
}
var contentData = analyzeContent( var contentData = analyzeContent(
book, baseUrl, redirectUrl, body, contentRule, bookChapter, bookSource, mNextChapterUrl book, baseUrl, redirectUrl, body, contentRule, bookChapter, bookSource, mNextChapterUrl
) )
@@ -107,7 +107,7 @@ class RssArticlesFragment() : VMBaseFragment<RssArticlesViewModel>(R.layout.frag
private fun loadArticles() { private fun loadArticles() {
activityViewModel.rssSource?.let { 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.data.entities.RssSource
import io.legado.app.model.rss.Rss import io.legado.app.model.rss.Rss
import io.legado.app.utils.stackTraceStr import io.legado.app.utils.stackTraceStr
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.withContext
class RssArticlesViewModel(application: Application) : BaseViewModel(application) { class RssArticlesViewModel(application: Application) : BaseViewModel(application) {
@@ -32,25 +31,23 @@ class RssArticlesViewModel(application: Application) : BaseViewModel(application
} }
} }
fun loadContent(rssSource: RssSource) { fun loadArticles(rssSource: RssSource) {
isLoading = true isLoading = true
page = 1 page = 1
order = System.currentTimeMillis() order = System.currentTimeMillis()
Rss.getArticles(viewModelScope, sortName, sortUrl, rssSource, page) Rss.getArticles(viewModelScope, sortName, sortUrl, rssSource, page).onSuccess(IO) {
.onSuccess(Dispatchers.IO) {
nextPageUrl = it.second nextPageUrl = it.second
it.first.let { list -> val articles = it.first
list.forEach { rssArticle -> articles.forEach { rssArticle ->
rssArticle.order = order-- rssArticle.order = order--
} }
appDb.rssArticleDao.insert(*list.toTypedArray()) appDb.rssArticleDao.insert(*articles.toTypedArray())
if (!rssSource.ruleNextPage.isNullOrEmpty()) { if (!rssSource.ruleNextPage.isNullOrEmpty()) {
appDb.rssArticleDao.clearOld(rssSource.sourceUrl, sortName, order) appDb.rssArticleDao.clearOld(rssSource.sourceUrl, sortName, order)
} }
val hasMore = list.isNotEmpty() && !rssSource.ruleNextPage.isNullOrEmpty() val hasMore = articles.isNotEmpty() && !rssSource.ruleNextPage.isNullOrEmpty()
loadFinallyLiveData.postValue(hasMore) loadFinallyLiveData.postValue(hasMore)
isLoading = false isLoading = false
}
}.onError { }.onError {
loadFinallyLiveData.postValue(false) loadFinallyLiveData.postValue(false)
AppLog.put("rss获取内容失败", it) AppLog.put("rss获取内容失败", it)
@@ -62,39 +59,34 @@ class RssArticlesViewModel(application: Application) : BaseViewModel(application
isLoading = true isLoading = true
page++ page++
val pageUrl = nextPageUrl val pageUrl = nextPageUrl
if (!pageUrl.isNullOrEmpty()) { if (pageUrl.isNullOrEmpty()) {
Rss.getArticles(viewModelScope, sortName, pageUrl, rssSource, page) loadFinallyLiveData.postValue(false)
.onSuccess(Dispatchers.IO) { return
}
Rss.getArticles(viewModelScope, sortName, pageUrl, rssSource, page).onSuccess(IO) {
nextPageUrl = it.second nextPageUrl = it.second
loadMoreSuccess(it.first) loadMoreSuccess(it.first)
} }.onError {
.onError {
loadFinallyLiveData.postValue(false) loadFinallyLiveData.postValue(false)
AppLog.put("rss获取内容失败", it) AppLog.put("rss获取内容失败", it)
loadErrorLiveData.postValue(it.stackTraceStr) loadErrorLiveData.postValue(it.stackTraceStr)
} }
} else {
loadFinallyLiveData.postValue(false)
}
} }
private fun loadMoreSuccess(articles: MutableList<RssArticle>) { private fun loadMoreSuccess(articles: MutableList<RssArticle>) {
articles.let { list -> if (articles.isEmpty()) {
if (list.isEmpty()) {
loadFinallyLiveData.postValue(false) loadFinallyLiveData.postValue(false)
return@let return
} }
val firstArticle = list.first() val firstArticle = articles.first()
val dbArticle = appDb.rssArticleDao val dbArticle = appDb.rssArticleDao.get(firstArticle.origin, firstArticle.link)
.get(firstArticle.origin, firstArticle.link)
if (dbArticle != null) { if (dbArticle != null) {
loadFinallyLiveData.postValue(false) loadFinallyLiveData.postValue(false)
} else { } else {
list.forEach { rssArticle -> articles.forEach {
rssArticle.order = order-- it.order = order--
}
appDb.rssArticleDao.insert(*list.toTypedArray())
} }
appDb.rssArticleDao.insert(*articles.toTypedArray())
} }
isLoading = false 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.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.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
@@ -42,14 +43,13 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application) {
fun initData(intent: Intent) { fun initData(intent: Intent) {
execute { execute {
val origin = intent.getStringExtra("origin") val origin = intent.getStringExtra("origin") ?: return@execute
val link = intent.getStringExtra("link") val link = intent.getStringExtra("link")
origin?.let {
rssSource = appDb.rssSourceDao.getByKey(origin) rssSource = appDb.rssSourceDao.getByKey(origin)
if (link != null) { if (link != null) {
rssStar = appDb.rssStarDao.get(origin, link) rssStar = appDb.rssStarDao.get(origin, link)
rssArticle = rssStar?.toRssArticle() ?: appDb.rssArticleDao.get(origin, link) rssArticle = rssStar?.toRssArticle() ?: appDb.rssArticleDao.get(origin, link)
rssArticle?.let { rssArticle -> val rssArticle = rssArticle ?: return@execute
if (!rssArticle.description.isNullOrBlank()) { if (!rssArticle.description.isNullOrBlank()) {
contentLiveData.postValue(rssArticle.description!!) contentLiveData.postValue(rssArticle.description!!)
} else { } else {
@@ -62,7 +62,6 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application) {
} }
} ?: loadUrl(rssArticle.link, rssArticle.origin) } ?: loadUrl(rssArticle.link, rssArticle.origin)
} }
}
} else { } else {
val ruleContent = rssSource?.ruleContent val ruleContent = rssSource?.ruleContent
if (ruleContent.isNullOrBlank()) { if (ruleContent.isNullOrBlank()) {
@@ -75,7 +74,6 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application) {
loadContent(rssArticle, ruleContent) loadContent(rssArticle, ruleContent)
} }
} }
}
}.onFinally { }.onFinally {
upStarMenuData.postValue(true) upStarMenuData.postValue(true)
} }
@@ -91,7 +89,7 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application) {
} }
private fun loadContent(rssArticle: RssArticle, ruleContent: String) { private fun loadContent(rssArticle: RssArticle, ruleContent: String) {
rssSource?.let { source -> val source = rssSource ?: return
Rss.getContent(viewModelScope, rssArticle, ruleContent, source) Rss.getContent(viewModelScope, rssArticle, ruleContent, source)
.onSuccess(IO) { body -> .onSuccess(IO) { body ->
rssArticle.description = body rssArticle.description = body
@@ -103,7 +101,6 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application) {
contentLiveData.postValue(body) contentLiveData.postValue(body)
} }
} }
}
fun refresh(finish: () -> Unit) { fun refresh(finish: () -> Unit) {
rssArticle?.let { rssArticle -> rssArticle?.let { rssArticle ->
@@ -139,18 +136,8 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application) {
webPic ?: return webPic ?: return
execute { execute {
val fileName = "${AppConst.fileNameFormat.format(Date(System.currentTimeMillis()))}.jpg" val fileName = "${AppConst.fileNameFormat.format(Date(System.currentTimeMillis()))}.jpg"
webData2bitmap(webPic)?.let { biteArray -> val byteArray = webData2bitmap(webPic) ?: throw NoStackTraceException("NULL")
if (uri.isContentScheme()) { uri.writeBytes(context, fileName, byteArray)
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")
}.onError { }.onError {
context.toastOnUi("保存图片失败:${it.localizedMessage}") context.toastOnUi("保存图片失败:${it.localizedMessage}")
}.onSuccess { }.onSuccess {
@@ -178,9 +165,11 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application) {
$content $content
""".trimIndent() """.trimIndent()
} }
content.contains("<style>".toRegex()) -> { content.contains("<style>".toRegex()) -> {
content content
} }
else -> { else -> {
""" """
<style> <style>