|
|
@ -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) { |
|
|
|
val source = App.db.bookSourceDao().findByKey(bookSource.origin) |
|
|
|
launch(IO) { |
|
|
|
if (source == null) { |
|
|
|
val source = App.db.bookSourceDao().findByKey(bookSource.origin) |
|
|
|
bookSource.customOrder = App.db.bookSourceDao().allCount() |
|
|
|
if (source == null) { |
|
|
|
|
|
|
|
bookSource.customOrder = App.db.bookSourceDao().allCount() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
App.db.bookSourceDao().insert(bookSource) |
|
|
|
|
|
|
|
finally?.let { it() } |
|
|
|
} |
|
|
|
} |
|
|
|
App.db.bookSourceDao().insert(bookSource) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|