diff --git a/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt b/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt index 033d7f57d..ef7715391 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt @@ -84,7 +84,7 @@ class BookSourceEditActivity : clipboard?.primaryClip = ClipData.newPlainText(null, sourceStr) } } - R.id.menu_paste_source -> viewModel.pasteSource { upRecyclerView() } + R.id.menu_paste_source -> viewModel.pasteSource { upRecyclerView(it) } } return super.onCompatOptionsItemSelected(item) } @@ -142,8 +142,7 @@ class BookSourceEditActivity : recycler_view.scrollToPosition(0) } - private fun upRecyclerView() { - val source = viewModel.bookSource + private fun upRecyclerView(source: BookSource? = viewModel.bookSource) { source?.let { cb_is_enable.isChecked = it.enabled cb_is_enable_find.isChecked = it.enabledExplore 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 f83ed5dbb..49887b3ba 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 @@ -30,15 +30,16 @@ class BookSourceEditViewModel(application: Application) : BaseViewModel(applicat } } - fun save(bookSource: BookSource, success: (() -> Unit)? = null) { + fun save(source: BookSource, success: (() -> Unit)? = null) { execute { oldSourceUrl?.let { - if (oldSourceUrl != bookSource.bookSourceUrl) { + if (oldSourceUrl != source.bookSourceUrl) { App.db.bookSourceDao().delete(it) } } - oldSourceUrl = bookSource.bookSourceUrl - App.db.bookSourceDao().insert(bookSource) + oldSourceUrl = source.bookSourceUrl + App.db.bookSourceDao().insert(source) + bookSource = source }.onSuccess { success?.invoke() }.onError { @@ -47,21 +48,25 @@ class BookSourceEditViewModel(application: Application) : BaseViewModel(applicat } } - fun pasteSource(onSuccess: () -> Unit) { + fun pasteSource(onSuccess: (source: BookSource) -> Unit) { execute { + var source: BookSource? = null val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager? clipboard?.primaryClip?.let { if (it.itemCount > 0) { val json = it.getItemAt(0).text.toString() - OldRule.jsonToBookSource(json)?.let { source -> - bookSource = source - } ?: toast("格式不对") + source = OldRule.jsonToBookSource(json) } } + source }.onError { toast(it.localizedMessage) }.onSuccess { - onSuccess() + if (it != null) { + onSuccess(it) + } else { + toast("格式不对") + } } } } \ No newline at end of file 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 c7b561b2a..2c1d2e066 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 @@ -102,7 +102,7 @@ class RssSourceEditActivity : clipboard?.primaryClip = ClipData.newPlainText(null, sourceStr) } } - R.id.menu_paste_source -> viewModel.pasteSource { upRecyclerView() } + R.id.menu_paste_source -> viewModel.pasteSource { upRecyclerView(it) } } return super.onCompatOptionsItemSelected(item) } @@ -115,8 +115,7 @@ class RssSourceEditActivity : recycler_view.adapter = adapter } - private fun upRecyclerView() { - val rssSource = viewModel.rssSource + private fun upRecyclerView(rssSource: RssSource? = viewModel.rssSource) { rssSource?.let { cb_is_enable.isChecked = rssSource.enabled cb_enable_js.isChecked = rssSource.enableJs 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 e04b19555..79bf0f9f5 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 @@ -31,15 +31,16 @@ class RssSourceEditViewModel(application: Application) : BaseViewModel(applicati } } - fun save(rssSource: RssSource, success: (() -> Unit)) { + fun save(source: RssSource, success: (() -> Unit)) { execute { oldSourceUrl?.let { - if (oldSourceUrl != rssSource.sourceUrl) { + if (oldSourceUrl != source.sourceUrl) { App.db.rssSourceDao().delete(it) } } - oldSourceUrl = rssSource.sourceUrl - App.db.rssSourceDao().insert(rssSource) + oldSourceUrl = source.sourceUrl + App.db.rssSourceDao().insert(source) + rssSource = source }.onSuccess { success() }.onError { @@ -48,21 +49,25 @@ class RssSourceEditViewModel(application: Application) : BaseViewModel(applicati } } - fun pasteSource(onSuccess: () -> Unit) { + fun pasteSource(onSuccess: (source: RssSource) -> Unit) { execute { + var source: RssSource? = null 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 -> - rssSource = source - } ?: toast("格式不对") + source = GSON.fromJsonObject(json) } } + source }.onError { toast(it.localizedMessage) }.onSuccess { - onSuccess() + if (it != null) { + onSuccess(it) + } else { + toast("格式不对") + } } } } \ No newline at end of file