feat: 优化代码

pull/172/head
kunfei 5 years ago
parent bc0a3abf56
commit c06be92a26
  1. 14
      app/src/main/java/io/legado/app/data/entities/RssStar.kt
  2. 2
      app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt
  3. 20
      app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt

@ -16,4 +16,16 @@ data class RssStar(
var description: String? = null, var description: String? = null,
var content: String? = null, var content: String? = null,
var image: String? = null var image: String? = null
) ) {
fun toRssArticle(): RssArticle {
return RssArticle(
origin = origin,
title = title,
link = link,
pubDate = pubDate,
description = description,
content = content,
image = image
)
}
}

@ -193,7 +193,7 @@ class ReadRssActivity : VMBaseActivity<ReadRssViewModel>(R.layout.activity_rss_r
} }
override fun upStarMenu() { override fun upStarMenu() {
if (viewModel.star) { if (viewModel.rssStar != null) {
starMenuItem?.setIcon(R.drawable.ic_star) starMenuItem?.setIcon(R.drawable.ic_star)
starMenuItem?.setTitle(R.string.in_favorites) starMenuItem?.setTitle(R.string.in_favorites)
} else { } else {

@ -15,6 +15,7 @@ import io.legado.app.base.BaseViewModel
import io.legado.app.constant.AppConst import io.legado.app.constant.AppConst
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.help.http.HttpHelper import io.legado.app.help.http.HttpHelper
import io.legado.app.model.Rss import io.legado.app.model.Rss
import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.model.analyzeRule.AnalyzeUrl
@ -34,7 +35,7 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application),
var rssArticle: RssArticle? = null var rssArticle: RssArticle? = null
val contentLiveData = MutableLiveData<String>() val contentLiveData = MutableLiveData<String>()
val urlLiveData = MutableLiveData<AnalyzeUrl>() val urlLiveData = MutableLiveData<AnalyzeUrl>()
var star = false var rssStar: RssStar? = null
var textToSpeech: TextToSpeech? = null var textToSpeech: TextToSpeech? = null
private var ttsInitFinish = false private var ttsInitFinish = false
private var ttsTextList = arrayListOf<String>() private var ttsTextList = arrayListOf<String>()
@ -45,8 +46,8 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application),
val link = intent.getStringExtra("link") val link = intent.getStringExtra("link")
if (origin != null && link != null) { if (origin != null && link != null) {
rssSource = App.db.rssSourceDao().getByKey(origin) rssSource = App.db.rssSourceDao().getByKey(origin)
star = App.db.rssStarDao().get(origin, link) != null rssStar = App.db.rssStarDao().get(origin, link)
rssArticle = App.db.rssArticleDao().get(origin, link) rssArticle = rssStar?.toRssArticle() ?: App.db.rssArticleDao().get(origin, link)
rssArticle?.let { rssArticle -> rssArticle?.let { rssArticle ->
if (!rssArticle.description.isNullOrBlank()) { if (!rssArticle.description.isNullOrBlank()) {
contentLiveData.postValue(rssArticle.description) contentLiveData.postValue(rssArticle.description)
@ -86,13 +87,12 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application),
fun favorite() { fun favorite() {
execute { execute {
rssArticle?.let { rssStar?.let {
if (star) { App.db.rssStarDao().delete(it.origin, it.link)
App.db.rssStarDao().delete(it.origin, it.link) rssStar = null
} else { } ?: rssArticle?.toStar()?.let {
App.db.rssStarDao().insert(it.toStar()) App.db.rssStarDao().insert(it)
} rssStar = it
star = !star
} }
}.onSuccess { }.onSuccess {
callBack?.upStarMenu() callBack?.upStarMenu()

Loading…
Cancel
Save