From c06be92a26050295ef48d63a23bbfcb6220fd98d Mon Sep 17 00:00:00 2001 From: kunfei Date: Fri, 20 Mar 2020 21:20:55 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/data/entities/RssStar.kt | 14 ++++++++++++- .../legado/app/ui/rss/read/ReadRssActivity.kt | 2 +- .../app/ui/rss/read/ReadRssViewModel.kt | 20 +++++++++---------- 3 files changed, 24 insertions(+), 12 deletions(-) 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 5199b586d..1d0c266f8 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 @@ -16,4 +16,16 @@ data class RssStar( var description: String? = null, var content: String? = null, var image: String? = null -) \ No newline at end of file +) { + fun toRssArticle(): RssArticle { + return RssArticle( + origin = origin, + title = title, + 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/ui/rss/read/ReadRssActivity.kt b/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt index 9aa30235b..73c55961b 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 @@ -193,7 +193,7 @@ class ReadRssActivity : VMBaseActivity(R.layout.activity_rss_r } override fun upStarMenu() { - if (viewModel.star) { + if (viewModel.rssStar != null) { starMenuItem?.setIcon(R.drawable.ic_star) starMenuItem?.setTitle(R.string.in_favorites) } else { 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 fd721e09b..cf8cafeef 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 @@ -15,6 +15,7 @@ import io.legado.app.base.BaseViewModel import io.legado.app.constant.AppConst 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.help.http.HttpHelper import io.legado.app.model.Rss import io.legado.app.model.analyzeRule.AnalyzeUrl @@ -34,7 +35,7 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application), var rssArticle: RssArticle? = null val contentLiveData = MutableLiveData() val urlLiveData = MutableLiveData() - var star = false + var rssStar: RssStar? = null var textToSpeech: TextToSpeech? = null private var ttsInitFinish = false private var ttsTextList = arrayListOf() @@ -45,8 +46,8 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application), val link = intent.getStringExtra("link") if (origin != null && link != null) { rssSource = App.db.rssSourceDao().getByKey(origin) - star = App.db.rssStarDao().get(origin, link) != null - rssArticle = App.db.rssArticleDao().get(origin, link) + rssStar = App.db.rssStarDao().get(origin, link) + rssArticle = rssStar?.toRssArticle() ?: App.db.rssArticleDao().get(origin, link) rssArticle?.let { rssArticle -> if (!rssArticle.description.isNullOrBlank()) { contentLiveData.postValue(rssArticle.description) @@ -86,13 +87,12 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application), fun favorite() { execute { - rssArticle?.let { - if (star) { - App.db.rssStarDao().delete(it.origin, it.link) - } else { - App.db.rssStarDao().insert(it.toStar()) - } - star = !star + rssStar?.let { + App.db.rssStarDao().delete(it.origin, it.link) + rssStar = null + } ?: rssArticle?.toStar()?.let { + App.db.rssStarDao().insert(it) + rssStar = it } }.onSuccess { callBack?.upStarMenu()