From 9fb795206d4a31d3c10b9c0c0b9dcc118bba1dfc Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 3 Oct 2019 16:01:57 +0800 Subject: [PATCH] up --- .../rss/source/edit/RssSourceEditActivity.kt | 5 +--- .../rss/source/edit/RssSourceEditViewModel.kt | 24 +++++++++++++------ 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditActivity.kt b/app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditActivity.kt index c30e8b9ff..45374b1ea 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditActivity.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?) { diff --git a/app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditViewModel.kt b/app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditViewModel.kt index f87438b9c..8b0f2cd11 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/edit/RssSourceEditViewModel.kt @@ -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 = 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 {