From 7bd59483d11a9d8c029e9d97b2c894997a4eb946 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 13 Oct 2019 14:41:38 +0800 Subject: [PATCH] up --- .../legado/app/ui/rss/read/ReadRssActivity.kt | 20 +++++++++++++++++-- .../app/ui/rss/read/ReadRssViewModel.kt | 12 ++++++++--- app/src/main/res/values/strings.xml | 2 ++ 3 files changed, 29 insertions(+), 5 deletions(-) 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 ce7812fe9..cef3e5361 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 @@ -18,6 +18,8 @@ class ReadRssActivity : VMBaseActivity(R.layout.activity_rss_r override val viewModel: ReadRssViewModel get() = getViewModel(ReadRssViewModel::class.java) + private var starMenuItem: MenuItem? = null + override fun onActivityCreated(savedInstanceState: Bundle?) { title = intent.getStringExtra("title") initWebView() @@ -27,12 +29,16 @@ class ReadRssActivity : VMBaseActivity(R.layout.activity_rss_r override fun onCompatCreateOptionsMenu(menu: Menu): Boolean { menuInflater.inflate(R.menu.rss_read, menu) + starMenuItem = menu.findItem(R.id.menu_rss_star) + upStarMenu() return super.onCompatCreateOptionsMenu(menu) } override fun onCompatOptionsItemSelected(item: MenuItem): Boolean { when (item.itemId) { - R.id.menu_rss_star -> { + R.id.menu_rss_star -> viewModel.rssArticleLiveData.value?.let { + it.star = !it.star + viewModel.upRssArticle(it) } } return super.onCompatOptionsItemSelected(item) @@ -46,13 +52,14 @@ class ReadRssActivity : VMBaseActivity(R.layout.activity_rss_r @SuppressLint("SetJavaScriptEnabled") private fun initLiveData() { + viewModel.rssArticleLiveData.observe(this, Observer { upStarMenu() }) viewModel.rssSourceLiveData.observe(this, Observer { if (it.enableJs) { webView.settings.javaScriptEnabled = true } }) viewModel.contentLiveData.observe(this, Observer { content -> - viewModel.rssArticle?.let { + viewModel.rssArticleLiveData.value?.let { val url = NetworkUtils.getAbsoluteURL(it.origin, it.link ?: "") if (viewModel.rssSourceLiveData.value?.loadWithBaseUrl == true) { webView.loadDataWithBaseURL( @@ -76,4 +83,13 @@ class ReadRssActivity : VMBaseActivity(R.layout.activity_rss_r }) } + private fun upStarMenu() { + if (viewModel.rssArticleLiveData.value?.star == true) { + starMenuItem?.setIcon(R.drawable.ic_star) + starMenuItem?.setTitle(R.string.y_store_up) + } else { + starMenuItem?.setIcon(R.drawable.ic_star_border) + starMenuItem?.setTitle(R.string.w_store_up) + } + } } \ No newline at end of file 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 3e8737a35..0e063d42b 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 @@ -12,7 +12,7 @@ import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.utils.NetworkUtils class ReadRssViewModel(application: Application) : BaseViewModel(application) { - var rssArticle: RssArticle? = null + var rssArticleLiveData = MutableLiveData() val rssSourceLiveData = MutableLiveData() val contentLiveData = MutableLiveData() val urlLiveData = MutableLiveData() @@ -26,8 +26,8 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application) { rssSourceLiveData.postValue(it) } if (origin != null && title != null) { - rssArticle = App.db.rssArticleDao().get(origin, title) - rssArticle?.let { rssArticle -> + App.db.rssArticleDao().get(origin, title)?.let { rssArticle -> + rssArticleLiveData.postValue(rssArticle) if (!rssArticle.description.isNullOrBlank()) { contentLiveData.postValue(rssArticle.description) } else { @@ -66,4 +66,10 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application) { } } } + + fun upRssArticle(rssArticle: RssArticle) { + execute { + App.db.rssArticleDao().update(rssArticle) + } + } } \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 21f6e3e19..4347783a4 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -35,6 +35,8 @@ 替换净化-搜索 书架 收藏 + 已收藏 + 未收藏 订阅 全部 最近阅读