pull/32/head
kunfei 5 years ago
parent 3f018fc2ed
commit cc51eaf123
  1. 18
      app/src/main/java/io/legado/app/ui/bookinfo/BookInfoEditActivity.kt
  2. 19
      app/src/main/java/io/legado/app/ui/bookinfo/BookInfoViewModel.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<BookInfoViewModel>(R.layout.activity_book_info_edit) {
@ -16,6 +17,9 @@ class BookInfoEditActivity : VMBaseActivity<BookInfoViewModel>(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<BookInfoViewModel>(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<BookInfoViewModel>(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) })
}
}
}

@ -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<Book>()
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)
}
}
}
Loading…
Cancel
Save