From ad6c347b305419c17de222c52bbbadbd63d7369c Mon Sep 17 00:00:00 2001 From: Invinciblelee <1760316362@qq.com> Date: Wed, 19 Jun 2019 14:25:39 +0800 Subject: [PATCH] SourceEditViewModel --- .../io/legado/app/data/entities/SearchBook.kt | 1 - .../app/ui/sourceedit/SourceEditActivity.kt | 5 +---- .../app/ui/sourceedit/SourceEditViewModel.kt | 16 ++++++++++------ 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/entities/SearchBook.kt b/app/src/main/java/io/legado/app/data/entities/SearchBook.kt index b2ad27533..23f658b3a 100644 --- a/app/src/main/java/io/legado/app/data/entities/SearchBook.kt +++ b/app/src/main/java/io/legado/app/data/entities/SearchBook.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 diff --git a/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditActivity.kt b/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditActivity.kt index bc7aabeec..c8bdd0b33 100644 --- a/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditActivity.kt +++ b/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditActivity.kt @@ -53,10 +53,7 @@ class SourceEditActivity : BaseActivity() { if (bookSource == null) { toast("书源名称和URL不能为空") } else { - launch { - withContext(IO) { viewModel.save(bookSource) } - finish() - } + viewModel.save(bookSource) { finish() } } } } diff --git a/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditViewModel.kt b/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditViewModel.kt index 950eeb0bd..89138d790 100644 --- a/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/sourceedit/SourceEditViewModel.kt @@ -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 = 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) }