diff --git a/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoEditActivity.kt b/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoEditActivity.kt index 5e632cd6c..61387502d 100644 --- a/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoEditActivity.kt +++ b/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoEditActivity.kt @@ -8,6 +8,7 @@ import io.legado.app.R import io.legado.app.base.VMBaseActivity import io.legado.app.data.entities.Book import io.legado.app.utils.getViewModel +import io.legado.app.utils.toast import kotlinx.android.synthetic.main.activity_book_info_edit.* class BookInfoEditActivity : VMBaseActivity(R.layout.activity_book_info_edit) { @@ -16,6 +17,9 @@ class BookInfoEditActivity : VMBaseActivity(R.layout.activity override fun onActivityCreated(savedInstanceState: Bundle?) { viewModel.bookData.observe(this, Observer { upView(it) }) + intent.getStringExtra("bookUrl")?.let { + viewModel.loadBook(it) + } } override fun onCompatCreateOptionsMenu(menu: Menu): Boolean { @@ -25,8 +29,7 @@ class BookInfoEditActivity : VMBaseActivity(R.layout.activity override fun onCompatOptionsItemSelected(item: MenuItem): Boolean { when (item.itemId) { - R.id.action_save -> { - } + R.id.action_save -> saveData() } return super.onCompatOptionsItemSelected(item) } @@ -38,5 +41,14 @@ class BookInfoEditActivity : VMBaseActivity(R.layout.activity tie_book_intro.setText(book.getDisplayIntro()) } - + private fun saveData() { + viewModel.bookData.value?.let { book -> + book.name = tie_book_name.text?.toString() + book.author = tie_book_author.text?.toString() + val customCoverUrl = tie_cover_url.text?.toString() + book.customCoverUrl = if (customCoverUrl == book.coverUrl) null else customCoverUrl + book.customIntro = tie_book_intro.text?.toString() + viewModel.saveBook(book, success = { finish() }, error = { toast(it) }) + } + } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoViewModel.kt b/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoViewModel.kt index d47df4b70..8b4c0cb71 100644 --- a/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoViewModel.kt @@ -2,6 +2,7 @@ package io.legado.app.ui.bookinfo import android.app.Application import androidx.lifecycle.MutableLiveData +import io.legado.app.App import io.legado.app.base.BaseViewModel import io.legado.app.data.entities.Book @@ -9,4 +10,22 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { val bookData = MutableLiveData() + + fun loadBook(bookUrl: String) { + execute { + App.db.bookDao().getBook(bookUrl)?.let { + bookData.postValue(it) + } + } + } + + fun saveBook(book: Book, success: (() -> Unit)?, error: ((msg: String) -> Unit)?) { + execute { + App.db.bookDao().insert(book) + }.onSuccess { + success?.invoke() + }.onError { + error?.invoke(it.localizedMessage) + } + } } \ No newline at end of file