From fa92c468a6c7e25f1acdb9b0c45e9b9de4f9d3de Mon Sep 17 00:00:00 2001 From: kunfei Date: Wed, 6 Nov 2019 09:51:08 +0800 Subject: [PATCH] up --- .../source/edit/BookSourceEditViewModel.kt | 4 ---- .../rss/source/edit/RssSourceEditActivity.kt | 15 +++++++------ .../rss/source/edit/RssSourceEditViewModel.kt | 21 ++++++++++--------- 3 files changed, 18 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditViewModel.kt b/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditViewModel.kt index db62afbce..f83ed5dbb 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditViewModel.kt @@ -24,10 +24,6 @@ class BookSourceEditViewModel(application: Application) : BaseViewModel(applicat source?.let { oldSourceUrl = it.bookSourceUrl bookSource = it - } ?: let { - bookSource = BookSource().apply { - customOrder = App.db.bookSourceDao().maxOrder + 1 - } } }.onFinally { onFinally() 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 5ee6a3e45..35ef24d50 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 @@ -12,7 +12,6 @@ import android.view.MenuItem import android.view.ViewTreeObserver import android.widget.EditText import android.widget.PopupWindow -import androidx.lifecycle.Observer import androidx.recyclerview.widget.LinearLayoutManager import io.legado.app.R import io.legado.app.base.VMBaseActivity @@ -46,10 +45,9 @@ class RssSourceEditActivity : override fun onActivityCreated(savedInstanceState: Bundle?) { initView() - viewModel.sourceLiveData.observe(this, Observer { - upRecyclerView(it) - }) - viewModel.initData(intent) + viewModel.initData(intent) { + upRecyclerView() + } } override fun onDestroy() { @@ -85,7 +83,7 @@ class RssSourceEditActivity : clipboard?.primaryClip = ClipData.newPlainText(null, sourceStr) } } - R.id.menu_paste_source -> viewModel.pasteSource() + R.id.menu_paste_source -> viewModel.pasteSource { upRecyclerView() } } return super.onCompatOptionsItemSelected(item) } @@ -98,7 +96,8 @@ class RssSourceEditActivity : recycler_view.adapter = adapter } - private fun upRecyclerView(rssSource: RssSource?) { + private fun upRecyclerView() { + val rssSource = viewModel.rssSource rssSource?.let { cb_is_enable.isChecked = rssSource.enabled cb_enable_js.isChecked = rssSource.enableJs @@ -136,7 +135,7 @@ class RssSourceEditActivity : } private fun getRssSource(): RssSource? { - val source = viewModel.sourceLiveData.value ?: RssSource() + val source = viewModel.rssSource?.copy() ?: RssSource() source.enabled = cb_is_enable.isChecked source.enableJs = cb_enable_js.isChecked source.loadWithBaseUrl = cb_enable_base_url.isChecked 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 35ae708bc..e04b19555 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 @@ -4,7 +4,6 @@ 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 import io.legado.app.data.entities.RssSource @@ -13,10 +12,10 @@ import io.legado.app.utils.fromJsonObject class RssSourceEditViewModel(application: Application) : BaseViewModel(application) { - val sourceLiveData: MutableLiveData = MutableLiveData() + var rssSource: RssSource? = null private var oldSourceUrl: String? = null - fun initData(intent: Intent) { + fun initData(intent: Intent, onFinally: () -> Unit) { execute { val key = intent.getStringExtra("data") var source: RssSource? = null @@ -25,12 +24,10 @@ class RssSourceEditViewModel(application: Application) : BaseViewModel(applicati } source?.let { oldSourceUrl = it.sourceUrl - sourceLiveData.postValue(it) - } ?: let { - sourceLiveData.postValue(RssSource().apply { - customOrder = App.db.rssSourceDao().maxOrder + 1 - }) + rssSource = it } + }.onFinally { + onFinally() } } @@ -51,17 +48,21 @@ class RssSourceEditViewModel(application: Application) : BaseViewModel(applicati } } - fun pasteSource() { + fun pasteSource(onSuccess: () -> Unit) { execute { val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager? clipboard?.primaryClip?.let { if (it.itemCount > 0) { val json = it.getItemAt(0).text.toString().trim() GSON.fromJsonObject(json)?.let { source -> - sourceLiveData.postValue(source) + rssSource = source } ?: toast("格式不对") } } + }.onError { + toast(it.localizedMessage) + }.onSuccess { + onSuccess() } } } \ No newline at end of file