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. 8
      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.Ignore
import androidx.room.Index import androidx.room.Index
import androidx.room.PrimaryKey import androidx.room.PrimaryKey
import com.google.gson.Gson
import io.legado.app.utils.GSON import io.legado.app.utils.GSON
import io.legado.app.utils.fromJson import io.legado.app.utils.fromJson
import kotlinx.android.parcel.IgnoredOnParcel import kotlinx.android.parcel.IgnoredOnParcel

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

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

Loading…
Cancel
Save