pull/35/head^2
kunfei 5 years ago
parent d29a46c8ef
commit 0dfd83f30f
  1. 3
      app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt
  2. 2
      app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditActivity.kt
  3. 22
      app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditViewModel.kt

@ -65,6 +65,9 @@ interface BookSourceDao {
@Delete @Delete
fun delete(vararg bookSource: BookSource) 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") @get:Query("select min(customOrder) from book_sources")
val minOrder: Int val minOrder: Int

@ -225,7 +225,7 @@ class BookSourceEditActivity :
} }
private fun getSource(): BookSource? { private fun getSource(): BookSource? {
val source = viewModel.sourceLiveData.value?.copy() ?: BookSource() val source = viewModel.sourceLiveData.value ?: BookSource()
source.enabled = cb_is_enable.isChecked source.enabled = cb_is_enable.isChecked
source.enabledExplore = cb_is_enable_find.isChecked source.enabledExplore = cb_is_enable_find.isChecked
viewModel.sourceLiveData.value?.let { viewModel.sourceLiveData.value?.let {

@ -12,26 +12,34 @@ import io.legado.app.help.storage.OldRule
class BookSourceEditViewModel(application: Application) : BaseViewModel(application) { class BookSourceEditViewModel(application: Application) : BaseViewModel(application) {
val sourceLiveData: MutableLiveData<BookSource> = MutableLiveData() val sourceLiveData: MutableLiveData<BookSource> = MutableLiveData()
var oldSourceUrl: String? = null
fun setBookSource(key: String) { fun setBookSource(key: String?) {
execute { 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) sourceLiveData.postValue(it)
} ?: let {
sourceLiveData.postValue(BookSource().apply {
customOrder = App.db.bookSourceDao().maxOrder + 1
})
} }
} }
} }
fun save(bookSource: BookSource, finally: (() -> Unit)? = null) { fun save(bookSource: BookSource, finally: (() -> Unit)? = null) {
execute { execute {
sourceLiveData.value?.let { oldSourceUrl?.let {
if (it.bookSourceUrl != bookSource.bookSourceUrl) { if (oldSourceUrl != bookSource.bookSourceUrl) {
App.db.bookSourceDao().delete(it) App.db.bookSourceDao().delete(it)
} }
} ?: let {
bookSource.customOrder = App.db.bookSourceDao().maxOrder + 1
} }
oldSourceUrl = bookSource.bookSourceUrl
App.db.bookSourceDao().insert(bookSource) App.db.bookSourceDao().insert(bookSource)
sourceLiveData.postValue(bookSource)
}.onFinally { }.onFinally {
finally?.let { it() } finally?.let { it() }
} }

Loading…
Cancel
Save