优化
This commit is contained in:
@@ -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,17 +57,16 @@ 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(
|
||||||
|
|||||||
@@ -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,69 +31,62 @@ 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
|
val articles = it.first
|
||||||
it.first.let { list ->
|
articles.forEach { rssArticle ->
|
||||||
list.forEach { rssArticle ->
|
rssArticle.order = order--
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
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) {
|
fun loadMore(rssSource: RssSource) {
|
||||||
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)
|
|
||||||
.onSuccess(Dispatchers.IO) {
|
|
||||||
nextPageUrl = it.second
|
|
||||||
loadMoreSuccess(it.first)
|
|
||||||
}
|
|
||||||
.onError {
|
|
||||||
loadFinallyLiveData.postValue(false)
|
|
||||||
AppLog.put("rss获取内容失败", it)
|
|
||||||
loadErrorLiveData.postValue(it.stackTraceStr)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
loadFinallyLiveData.postValue(false)
|
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>) {
|
private fun loadMoreSuccess(articles: MutableList<RssArticle>) {
|
||||||
articles.let { list ->
|
if (articles.isEmpty()) {
|
||||||
if (list.isEmpty()) {
|
loadFinallyLiveData.postValue(false)
|
||||||
loadFinallyLiveData.postValue(false)
|
return
|
||||||
return@let
|
}
|
||||||
}
|
val firstArticle = articles.first()
|
||||||
val firstArticle = list.first()
|
val dbArticle = appDb.rssArticleDao.get(firstArticle.origin, firstArticle.link)
|
||||||
val dbArticle = appDb.rssArticleDao
|
if (dbArticle != null) {
|
||||||
.get(firstArticle.origin, firstArticle.link)
|
loadFinallyLiveData.postValue(false)
|
||||||
if (dbArticle != null) {
|
} else {
|
||||||
loadFinallyLiveData.postValue(false)
|
articles.forEach {
|
||||||
} else {
|
it.order = order--
|
||||||
list.forEach { rssArticle ->
|
|
||||||
rssArticle.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,38 +43,35 @@ 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)
|
val rssArticle = rssArticle ?: return@execute
|
||||||
rssArticle?.let { rssArticle ->
|
if (!rssArticle.description.isNullOrBlank()) {
|
||||||
if (!rssArticle.description.isNullOrBlank()) {
|
contentLiveData.postValue(rssArticle.description!!)
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
val ruleContent = rssSource?.ruleContent
|
rssSource?.let {
|
||||||
if (ruleContent.isNullOrBlank()) {
|
val ruleContent = it.ruleContent
|
||||||
loadUrl(origin, origin)
|
if (!ruleContent.isNullOrBlank()) {
|
||||||
} else {
|
loadContent(rssArticle, ruleContent)
|
||||||
val rssArticle = RssArticle()
|
} else {
|
||||||
rssArticle.origin = origin
|
loadUrl(rssArticle.link, rssArticle.origin)
|
||||||
rssArticle.link = origin
|
}
|
||||||
rssArticle.title = rssSource!!.sourceName
|
} ?: loadUrl(rssArticle.link, rssArticle.origin)
|
||||||
loadContent(rssArticle, ruleContent)
|
}
|
||||||
}
|
} 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 {
|
}.onFinally {
|
||||||
@@ -91,18 +89,17 @@ 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
|
||||||
appDb.rssArticleDao.insert(rssArticle)
|
appDb.rssArticleDao.insert(rssArticle)
|
||||||
rssStar?.let {
|
rssStar?.let {
|
||||||
it.description = body
|
it.description = body
|
||||||
appDb.rssStarDao.insert(it)
|
appDb.rssStarDao.insert(it)
|
||||||
}
|
|
||||||
contentLiveData.postValue(body)
|
|
||||||
}
|
}
|
||||||
}
|
contentLiveData.postValue(body)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun refresh(finish: () -> Unit) {
|
fun refresh(finish: () -> Unit) {
|
||||||
@@ -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>
|
||||||
|
|||||||
Reference in New Issue
Block a user