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

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

Loading…
Cancel
Save