SourceEditViewModel

pull/32/head
Invinciblelee 6 years ago
parent fdf684c431
commit ad6c347b30
  1. 1
      app/src/main/java/io/legado/app/data/entities/SearchBook.kt
  2. 5
      app/src/main/java/io/legado/app/ui/sourceedit/SourceEditActivity.kt
  3. 16
      app/src/main/java/io/legado/app/ui/sourceedit/SourceEditViewModel.kt

@ -6,7 +6,6 @@ import androidx.room.Entity
import androidx.room.Ignore
import androidx.room.Index
import androidx.room.PrimaryKey
import com.google.gson.Gson
import io.legado.app.utils.GSON
import io.legado.app.utils.fromJson
import kotlinx.android.parcel.IgnoredOnParcel

@ -53,10 +53,7 @@ class SourceEditActivity : BaseActivity<SourceEditViewModel>() {
if (bookSource == null) {
toast("书源名称和URL不能为空")
} else {
launch {
withContext(IO) { viewModel.save(bookSource) }
finish()
}
viewModel.save(bookSource) { finish() }
}
}
}

@ -6,6 +6,7 @@ import io.legado.app.App
import io.legado.app.base.BaseViewModel
import io.legado.app.data.entities.BookSource
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.launch
class SourceEditViewModel(application: Application) : BaseViewModel(application) {
@ -13,18 +14,21 @@ class SourceEditViewModel(application: Application) : BaseViewModel(application)
val sourceLiveData: MutableLiveData<BookSource> = MutableLiveData()
fun setBookSource(key: String) {
launch(Dispatchers.IO) {
launch(IO) {
val source = App.db.bookSourceDao().findByKey(key)
sourceLiveData.postValue(source)
}
}
fun save(bookSource: BookSource) {
val source = App.db.bookSourceDao().findByKey(bookSource.origin)
if (source == null) {
bookSource.customOrder = App.db.bookSourceDao().allCount()
fun save(bookSource: BookSource, finally: (() -> Unit)? = null) {
launch(IO) {
val source = App.db.bookSourceDao().findByKey(bookSource.origin)
if (source == null) {
bookSource.customOrder = App.db.bookSourceDao().allCount()
}
App.db.bookSourceDao().insert(bookSource)
finally?.let { it() }
}
App.db.bookSourceDao().insert(bookSource)
}

Loading…
Cancel
Save