From 0dfd83f30fe6d59352d1500eb37d79365c12b7be Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 3 Oct 2019 15:55:52 +0800 Subject: [PATCH] up --- .../io/legado/app/data/dao/BookSourceDao.kt | 3 +++ .../source/edit/BookSourceEditActivity.kt | 2 +- .../source/edit/BookSourceEditViewModel.kt | 22 +++++++++++++------ 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt b/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt index 0c8e4d65e..e1864eb5b 100644 --- a/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt @@ -65,6 +65,9 @@ interface BookSourceDao { @Delete fun delete(vararg bookSource: BookSource) + @Query("delete from book_sources where bookSourceUrl = :key") + fun delete(key: String) + @get:Query("select min(customOrder) from book_sources") val minOrder: Int 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 b1c659b9b..6362c947f 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 @@ -225,7 +225,7 @@ class BookSourceEditActivity : } private fun getSource(): BookSource? { - val source = viewModel.sourceLiveData.value?.copy() ?: BookSource() + val source = viewModel.sourceLiveData.value ?: BookSource() source.enabled = cb_is_enable.isChecked source.enabledExplore = cb_is_enable_find.isChecked viewModel.sourceLiveData.value?.let { 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 910c461a7..1202f74f2 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 @@ -12,26 +12,34 @@ import io.legado.app.help.storage.OldRule class BookSourceEditViewModel(application: Application) : BaseViewModel(application) { val sourceLiveData: MutableLiveData = MutableLiveData() + var oldSourceUrl: String? = null - fun setBookSource(key: String) { + fun setBookSource(key: String?) { execute { - App.db.bookSourceDao().getBookSource(key)?.let { + var source: BookSource? = null + if (key != null) { + source = App.db.bookSourceDao().getBookSource(key) + } + source?.let { + oldSourceUrl = it.bookSourceUrl sourceLiveData.postValue(it) + } ?: let { + sourceLiveData.postValue(BookSource().apply { + customOrder = App.db.bookSourceDao().maxOrder + 1 + }) } } } fun save(bookSource: BookSource, finally: (() -> Unit)? = null) { execute { - sourceLiveData.value?.let { - if (it.bookSourceUrl != bookSource.bookSourceUrl) { + oldSourceUrl?.let { + if (oldSourceUrl != bookSource.bookSourceUrl) { App.db.bookSourceDao().delete(it) } - } ?: let { - bookSource.customOrder = App.db.bookSourceDao().maxOrder + 1 } + oldSourceUrl = bookSource.bookSourceUrl App.db.bookSourceDao().insert(bookSource) - sourceLiveData.postValue(bookSource) }.onFinally { finally?.let { it() } }