feat: 优化代码

pull/172/head
kunfei 5 years ago
parent bc0a3abf56
commit c06be92a26
  1. 12
      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. 18
      app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt

@ -16,4 +16,16 @@ data class RssStar(
var description: String? = null,
var content: 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() {
if (viewModel.star) {
if (viewModel.rssStar != null) {
starMenuItem?.setIcon(R.drawable.ic_star)
starMenuItem?.setTitle(R.string.in_favorites)
} else {

@ -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<String>()
val urlLiveData = MutableLiveData<AnalyzeUrl>()
var star = false
var rssStar: RssStar? = null
var textToSpeech: TextToSpeech? = null
private var ttsInitFinish = false
private var ttsTextList = arrayListOf<String>()
@ -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) {
rssStar?.let {
App.db.rssStarDao().delete(it.origin, it.link)
} else {
App.db.rssStarDao().insert(it.toStar())
}
star = !star
rssStar = null
} ?: rssArticle?.toStar()?.let {
App.db.rssStarDao().insert(it)
rssStar = it
}
}.onSuccess {
callBack?.upStarMenu()

Loading…
Cancel
Save