mirror of
https://github.com/gedoor/legado.git
synced 2024-08-30 09:23:26 +08:00
fix: viewModel hold activity may cause memory leak
This commit is contained in:
parent
1b6ec86984
commit
34083b6765
@ -107,7 +107,6 @@ class BookInfoActivity :
|
|||||||
binding.flAction.setBackgroundColor(bottomBackground)
|
binding.flAction.setBackgroundColor(bottomBackground)
|
||||||
binding.tvShelf.setTextColor(getPrimaryTextColor(ColorUtils.isColorLight(bottomBackground)))
|
binding.tvShelf.setTextColor(getPrimaryTextColor(ColorUtils.isColorLight(bottomBackground)))
|
||||||
binding.tvToc.text = getString(R.string.toc_s, getString(R.string.loading))
|
binding.tvToc.text = getString(R.string.toc_s, getString(R.string.loading))
|
||||||
viewModel.callBack = this
|
|
||||||
viewModel.bookData.observe(this) { showBook(it) }
|
viewModel.bookData.observe(this) { showBook(it) }
|
||||||
viewModel.chapterListData.observe(this) { upLoading(false, it) }
|
viewModel.chapterListData.observe(this) { upLoading(false, it) }
|
||||||
viewModel.waitDialogData.observe(this) { upWaitDialogStatus(it) }
|
viewModel.waitDialogData.observe(this) { upWaitDialogStatus(it) }
|
||||||
|
@ -34,8 +34,7 @@ import java.net.URLDecoder
|
|||||||
/**
|
/**
|
||||||
* rss阅读界面
|
* rss阅读界面
|
||||||
*/
|
*/
|
||||||
class ReadRssActivity : VMBaseActivity<ActivityRssReadBinding, ReadRssViewModel>(false),
|
class ReadRssActivity : VMBaseActivity<ActivityRssReadBinding, ReadRssViewModel>(false) {
|
||||||
ReadRssViewModel.CallBack {
|
|
||||||
|
|
||||||
override val binding by viewBinding(ActivityRssReadBinding::inflate)
|
override val binding by viewBinding(ActivityRssReadBinding::inflate)
|
||||||
override val viewModel by viewModels<ReadRssViewModel>()
|
override val viewModel by viewModels<ReadRssViewModel>()
|
||||||
@ -50,7 +49,8 @@ class ReadRssActivity : VMBaseActivity<ActivityRssReadBinding, ReadRssViewModel>
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onActivityCreated(savedInstanceState: Bundle?) {
|
override fun onActivityCreated(savedInstanceState: Bundle?) {
|
||||||
viewModel.callBack = this
|
viewModel.upStarMenuData.observe(this) { upStarMenu() }
|
||||||
|
viewModel.upTtsMenuData.observe(this) { upTtsMenu(it) }
|
||||||
binding.titleBar.title = intent.getStringExtra("title")
|
binding.titleBar.title = intent.getStringExtra("title")
|
||||||
initWebView()
|
initWebView()
|
||||||
initLiveData()
|
initLiveData()
|
||||||
@ -223,7 +223,7 @@ class ReadRssActivity : VMBaseActivity<ActivityRssReadBinding, ReadRssViewModel>
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun upStarMenu() {
|
private fun upStarMenu() {
|
||||||
starMenuItem?.isVisible = viewModel.rssArticle != null
|
starMenuItem?.isVisible = viewModel.rssArticle != null
|
||||||
if (viewModel.rssStar != null) {
|
if (viewModel.rssStar != null) {
|
||||||
starMenuItem?.setIcon(R.drawable.ic_star)
|
starMenuItem?.setIcon(R.drawable.ic_star)
|
||||||
@ -235,7 +235,7 @@ class ReadRssActivity : VMBaseActivity<ActivityRssReadBinding, ReadRssViewModel>
|
|||||||
starMenuItem?.icon?.setTintMutate(primaryTextColor)
|
starMenuItem?.icon?.setTintMutate(primaryTextColor)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun upTtsMenu(isPlaying: Boolean) {
|
private fun upTtsMenu(isPlaying: Boolean) {
|
||||||
launch {
|
launch {
|
||||||
if (isPlaying) {
|
if (isPlaying) {
|
||||||
ttsMenuItem?.setIcon(R.drawable.ic_stop_black_24dp)
|
ttsMenuItem?.setIcon(R.drawable.ic_stop_black_24dp)
|
||||||
|
@ -27,13 +27,14 @@ import java.util.*
|
|||||||
|
|
||||||
|
|
||||||
class ReadRssViewModel(application: Application) : BaseViewModel(application) {
|
class ReadRssViewModel(application: Application) : BaseViewModel(application) {
|
||||||
var callBack: CallBack? = null
|
|
||||||
var rssSource: RssSource? = null
|
var rssSource: RssSource? = null
|
||||||
var rssArticle: RssArticle? = null
|
var rssArticle: RssArticle? = null
|
||||||
var tts: TTS? = null
|
var tts: TTS? = null
|
||||||
val contentLiveData = MutableLiveData<String>()
|
val contentLiveData = MutableLiveData<String>()
|
||||||
val urlLiveData = MutableLiveData<AnalyzeUrl>()
|
val urlLiveData = MutableLiveData<AnalyzeUrl>()
|
||||||
var rssStar: RssStar? = null
|
var rssStar: RssStar? = null
|
||||||
|
val upTtsMenuData = MutableLiveData<Boolean>()
|
||||||
|
val upStarMenuData = MutableLiveData<Boolean>()
|
||||||
|
|
||||||
fun initData(intent: Intent) {
|
fun initData(intent: Intent) {
|
||||||
execute {
|
execute {
|
||||||
@ -72,7 +73,7 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.onFinally {
|
}.onFinally {
|
||||||
callBack?.upStarMenu()
|
upStarMenuData.postValue(true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -126,7 +127,7 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application) {
|
|||||||
rssStar = it
|
rssStar = it
|
||||||
}
|
}
|
||||||
}.onSuccess {
|
}.onSuccess {
|
||||||
callBack?.upStarMenu()
|
upStarMenuData.postValue(true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -196,11 +197,11 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application) {
|
|||||||
tts = TTS().apply {
|
tts = TTS().apply {
|
||||||
setSpeakStateListener(object : TTS.SpeakStateListener {
|
setSpeakStateListener(object : TTS.SpeakStateListener {
|
||||||
override fun onStart() {
|
override fun onStart() {
|
||||||
callBack?.upTtsMenu(true)
|
upTtsMenuData.postValue(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDone() {
|
override fun onDone() {
|
||||||
callBack?.upTtsMenu(false)
|
upTtsMenuData.postValue(false)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -213,8 +214,4 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application) {
|
|||||||
tts?.clearTts()
|
tts?.clearTts()
|
||||||
}
|
}
|
||||||
|
|
||||||
interface CallBack {
|
|
||||||
fun upStarMenu()
|
|
||||||
fun upTtsMenu(isPlaying: Boolean)
|
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user