From 10875e11b58d88ffb0f32aaf34c4a43c3d748fd6 Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 21 Nov 2019 17:18:24 +0800 Subject: [PATCH] up --- .../java/io/legado/app/data/dao/RssStarDao.kt | 3 +++ .../io/legado/app/data/entities/RssArticle.kt | 16 ++++++++++++- .../io/legado/app/data/entities/RssStar.kt | 3 +-- .../legado/app/ui/rss/read/ReadRssActivity.kt | 9 ++++---- .../app/ui/rss/read/ReadRssViewModel.kt | 23 +++++++++++++++---- 5 files changed, 42 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/dao/RssStarDao.kt b/app/src/main/java/io/legado/app/data/dao/RssStarDao.kt index 82640a049..87e527f75 100644 --- a/app/src/main/java/io/legado/app/data/dao/RssStarDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/RssStarDao.kt @@ -21,4 +21,7 @@ interface RssStarDao { @Query("delete from rssStars where origin = :origin") fun delete(origin: String) + + @Query("delete from rssStars where origin = :origin and link = :link") + fun delete(origin: String, link: String) } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/data/entities/RssArticle.kt b/app/src/main/java/io/legado/app/data/entities/RssArticle.kt index 76637dc13..c8f70d16e 100644 --- a/app/src/main/java/io/legado/app/data/entities/RssArticle.kt +++ b/app/src/main/java/io/legado/app/data/entities/RssArticle.kt @@ -17,4 +17,18 @@ data class RssArticle( var content: String? = null, var image: String? = null, var read: Boolean = false -) \ No newline at end of file +) { + + fun toStar(): RssStar { + return RssStar( + origin = origin, + title = title, + starTime = System.currentTimeMillis(), + link = link, + pubDate = pubDate, + description = description, + content = content, + image = image + ) + } +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/data/entities/RssStar.kt b/app/src/main/java/io/legado/app/data/entities/RssStar.kt index 39a1f4a95..5199b586d 100644 --- a/app/src/main/java/io/legado/app/data/entities/RssStar.kt +++ b/app/src/main/java/io/legado/app/data/entities/RssStar.kt @@ -15,6 +15,5 @@ data class RssStar( var pubDate: String? = null, var description: String? = null, var content: String? = null, - var image: String? = null, - var read: Boolean = false + var image: String? = null ) \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt b/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt index 2012481b4..d84b81e23 100644 --- a/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt @@ -15,7 +15,8 @@ import io.legado.app.utils.NetworkUtils import io.legado.app.utils.getViewModel import kotlinx.android.synthetic.main.activity_rss_read.* -class ReadRssActivity : VMBaseActivity(R.layout.activity_rss_read) { +class ReadRssActivity : VMBaseActivity(R.layout.activity_rss_read), + ReadRssViewModel.CallBack { override val viewModel: ReadRssViewModel get() = getViewModel(ReadRssViewModel::class.java) @@ -23,6 +24,7 @@ class ReadRssActivity : VMBaseActivity(R.layout.activity_rss_r private var starMenuItem: MenuItem? = null override fun onActivityCreated(savedInstanceState: Bundle?) { + viewModel.callBack = this title = intent.getStringExtra("title") initWebView() initLiveData() @@ -53,9 +55,8 @@ class ReadRssActivity : VMBaseActivity(R.layout.activity_rss_r @SuppressLint("SetJavaScriptEnabled") private fun initLiveData() { - viewModel.rssArticleLiveData.observe(this, Observer { upStarMenu() }) viewModel.contentLiveData.observe(this, Observer { content -> - viewModel.rssArticleLiveData.value?.let { + viewModel.rssArticle?.let { upJavaScriptEnable() val url = NetworkUtils.getAbsoluteURL(it.origin, it.link) if (viewModel.rssSource?.loadWithBaseUrl == true) { @@ -88,7 +89,7 @@ class ReadRssActivity : VMBaseActivity(R.layout.activity_rss_r } } - private fun upStarMenu() { + override fun upStarMenu() { if (viewModel.star) { starMenuItem?.setIcon(R.drawable.ic_star) starMenuItem?.setTitle(R.string.y_store_up) diff --git a/app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt b/app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt index 16e2f6c7a..52c090072 100644 --- a/app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt @@ -11,8 +11,9 @@ import io.legado.app.model.Rss import io.legado.app.model.analyzeRule.AnalyzeUrl class ReadRssViewModel(application: Application) : BaseViewModel(application) { + var callBack: CallBack? = null var rssSource: RssSource? = null - val rssArticleLiveData = MutableLiveData() + var rssArticle: RssArticle? = null val contentLiveData = MutableLiveData() val urlLiveData = MutableLiveData() var star = false @@ -24,8 +25,8 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application) { if (origin != null && link != null) { rssSource = App.db.rssSourceDao().getByKey(origin) star = App.db.rssStarDao().get(origin, link) != null - App.db.rssArticleDao().get(origin, link)?.let { rssArticle -> - rssArticleLiveData.postValue(rssArticle) + rssArticle = App.db.rssArticleDao().get(origin, link) + rssArticle?.let { rssArticle -> if (!rssArticle.description.isNullOrBlank()) { contentLiveData.postValue(rssArticle.description) } else { @@ -40,6 +41,8 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application) { } } } + }.onFinally { + callBack?.upStarMenu() } } @@ -62,9 +65,19 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application) { fun star() { execute { - if (star) { - + rssArticle?.let { + if (star) { + App.db.rssStarDao().delete(it.origin, it.link) + } else { + App.db.rssStarDao().insert(it.toStar()) + } } + }.onSuccess { + callBack?.upStarMenu() } } + + interface CallBack { + fun upStarMenu() + } } \ No newline at end of file