From fa1c8ce18e5a899dfc9bb4707f3d9c5663f89b3f Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 3 Oct 2019 15:29:15 +0800 Subject: [PATCH] up --- app/src/main/java/io/legado/app/model/Rss.kt | 24 +++++++++++++++++++ .../legado/app/model/rss/RssParserByRule.kt | 20 ++++++++++++---- .../ui/rss/article/RssArticlesViewModel.kt | 23 +++++++----------- 3 files changed, 48 insertions(+), 19 deletions(-) create mode 100644 app/src/main/java/io/legado/app/model/Rss.kt diff --git a/app/src/main/java/io/legado/app/model/Rss.kt b/app/src/main/java/io/legado/app/model/Rss.kt new file mode 100644 index 000000000..cd76e24fe --- /dev/null +++ b/app/src/main/java/io/legado/app/model/Rss.kt @@ -0,0 +1,24 @@ +package io.legado.app.model + +import io.legado.app.data.entities.RssArticle +import io.legado.app.data.entities.RssSource +import io.legado.app.help.coroutine.Coroutine +import io.legado.app.model.analyzeRule.AnalyzeUrl +import io.legado.app.model.rss.RssParserByRule +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlin.coroutines.CoroutineContext + +object Rss { + + fun getArticles( + rssSource: RssSource, + scope: CoroutineScope = Coroutine.DEFAULT, + context: CoroutineContext = Dispatchers.IO + ): Coroutine> { + return Coroutine.async(scope, context) { + val response = AnalyzeUrl(rssSource.sourceUrl).getResponseAsync().await() + RssParserByRule.parseXML(response, rssSource) + } + } +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/model/rss/RssParserByRule.kt b/app/src/main/java/io/legado/app/model/rss/RssParserByRule.kt index 6880976ab..c1609c852 100644 --- a/app/src/main/java/io/legado/app/model/rss/RssParserByRule.kt +++ b/app/src/main/java/io/legado/app/model/rss/RssParserByRule.kt @@ -1,18 +1,28 @@ package io.legado.app.model.rss +import io.legado.app.App +import io.legado.app.R import io.legado.app.data.entities.RssArticle import io.legado.app.data.entities.RssSource import io.legado.app.model.analyzeRule.AnalyzeRule -import org.xmlpull.v1.XmlPullParserException -import java.io.IOException +import retrofit2.Response object RssParserByRule { - @Throws(XmlPullParserException::class, IOException::class) - fun parseXML(xml: String, rssSource: RssSource): MutableList { - + @Throws(Exception::class) + fun parseXML(response: Response, rssSource: RssSource): MutableList { val articleList = mutableListOf() + val xml = response.body() + if (xml.isNullOrBlank()) { + throw Exception( + App.INSTANCE.getString( + R.string.error_get_web_content, + rssSource.sourceUrl + ) + ) + } + val analyzeRule = AnalyzeRule() analyzeRule.setContent(xml) diff --git a/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesViewModel.kt b/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesViewModel.kt index b2d052c29..5d9470f71 100644 --- a/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/rss/article/RssArticlesViewModel.kt @@ -4,9 +4,8 @@ import android.app.Application import androidx.lifecycle.MutableLiveData import io.legado.app.App import io.legado.app.base.BaseViewModel -import io.legado.app.model.analyzeRule.AnalyzeUrl -import io.legado.app.model.rss.RssParser -import io.legado.app.model.rss.RssParserByRule +import io.legado.app.data.entities.RssSource +import io.legado.app.model.Rss class RssArticlesViewModel(application: Application) : BaseViewModel(application) { @@ -15,19 +14,15 @@ class RssArticlesViewModel(application: Application) : BaseViewModel(application fun loadContent(url: String, onFinally: () -> Unit) { execute { - val rssSource = App.db.rssSourceDao().getByKey(url) - rssSource?.let { + var rssSource = App.db.rssSourceDao().getByKey(url) + if (rssSource == null) { + rssSource = RssSource(sourceUrl = url) + } else { titleLiveData.postValue(rssSource.sourceName) } - AnalyzeUrl(url).getResponseAsync().await().body()?.let { xml -> - if (rssSource == null || rssSource.ruleArticles.isNullOrBlank()) { - RssParser.parseXML(xml, url).let { - App.db.rssArtivleDao().insert(*it.toTypedArray()) - } - } else { - RssParserByRule.parseXML(xml, rssSource).let { - App.db.rssArtivleDao().insert(*it.toTypedArray()) - } + Rss.getArticles(rssSource, this).onSuccess { + it?.let { + App.db.rssArtivleDao().insert(*it.toTypedArray()) } } }.onFinally {