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
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

@ -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 {

@ -12,26 +12,34 @@ import io.legado.app.help.storage.OldRule
class BookSourceEditViewModel(application: Application) : BaseViewModel(application) {
val sourceLiveData: MutableLiveData<BookSource> = 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() }
}

Loading…
Cancel
Save