pull/35/head^2
kunfei 5 years ago
parent a62b75832b
commit 9fb795206d
  1. 5
      app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditActivity.kt
  2. 24
      app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditViewModel.kt

@ -49,9 +49,7 @@ class RssSourceEditActivity :
viewModel.sourceLiveData.observe(this, Observer {
upRecyclerView(it)
})
intent.getStringExtra("data")?.let {
viewModel.setSource(it)
}
viewModel.initData(intent)
}
override fun onDestroy() {
@ -98,7 +96,6 @@ class RssSourceEditActivity :
window.decorView.viewTreeObserver.addOnGlobalLayoutListener(KeyboardOnGlobalChangeListener())
recycler_view.layoutManager = LinearLayoutManager(this)
recycler_view.adapter = adapter
upRecyclerView(null)
}
private fun upRecyclerView(rssSource: RssSource?) {

@ -3,6 +3,7 @@ package io.legado.app.ui.rss.source.edit
import android.app.Application
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import androidx.lifecycle.MutableLiveData
import io.legado.app.App
import io.legado.app.base.BaseViewModel
@ -13,26 +14,35 @@ import io.legado.app.utils.fromJsonObject
class RssSourceEditViewModel(application: Application) : BaseViewModel(application) {
val sourceLiveData: MutableLiveData<RssSource> = MutableLiveData()
var oldSourceUrl: String? = null
fun setSource(key: String) {
fun initData(intent: Intent) {
execute {
App.db.rssSourceDao().getByKey(key)?.let {
val key = intent.getStringExtra("data")
var source: RssSource? = null
if (key != null) {
source = App.db.rssSourceDao().getByKey(key)
}
source?.let {
oldSourceUrl = it.sourceUrl
sourceLiveData.postValue(it)
} ?: let {
sourceLiveData.postValue(RssSource().apply {
customOrder = App.db.rssSourceDao().maxOrder + 1
})
}
}
}
fun save(rssSource: RssSource, success: (() -> Unit)) {
execute {
sourceLiveData.value?.let {
if (it.sourceUrl != rssSource.sourceUrl) {
oldSourceUrl?.let {
if (oldSourceUrl != rssSource.sourceUrl) {
App.db.rssSourceDao().delete(it)
}
} ?: let {
rssSource.customOrder = App.db.rssSourceDao().maxOrder + 1
}
oldSourceUrl = rssSource.sourceUrl
App.db.rssSourceDao().insert(rssSource)
sourceLiveData.postValue(rssSource)
}.onSuccess {
success()
}.onError {

Loading…
Cancel
Save