pull/53/head
kunfei 5 years ago
parent f929aa9833
commit 3651422291
  1. 27
      app/src/main/java/io/legado/app/ui/book/info/edit/BookInfoEditActivity.kt
  2. 10
      app/src/main/java/io/legado/app/ui/book/info/edit/BookInfoEditViewModel.kt

@ -8,11 +8,11 @@ import androidx.lifecycle.Observer
import io.legado.app.R
import io.legado.app.base.VMBaseActivity
import io.legado.app.data.entities.Book
import io.legado.app.help.ImageLoader
import io.legado.app.ui.changecover.ChangeCoverDialog
import io.legado.app.utils.getViewModel
import kotlinx.android.synthetic.main.activity_book_info_edit.*
import org.jetbrains.anko.sdk27.listeners.onClick
import org.jetbrains.anko.toast
class BookInfoEditActivity :
VMBaseActivity<BookInfoEditViewModel>(R.layout.activity_book_info_edit),
@ -55,6 +55,15 @@ class BookInfoEditActivity :
tie_book_author.setText(book.author)
tie_cover_url.setText(book.getDisplayCover())
tie_book_intro.setText(book.getDisplayIntro())
upCover()
}
private fun upCover() {
viewModel.book?.getDisplayCover()?.let {
ImageLoader.load(this, it)
.centerCrop()
.into(iv_cover)
}
}
private fun saveData() {
@ -64,18 +73,16 @@ class BookInfoEditActivity :
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 = {
setResult(Activity.RESULT_OK)
finish()
},
error = {
toast(it)
})
viewModel.saveBook(book) {
setResult(Activity.RESULT_OK)
finish()
}
}
}
override fun coverChangeTo(coverUrl: String) {
viewModel.book?.customCoverUrl = coverUrl
tie_cover_url.setText(coverUrl)
upCover()
}
}

@ -7,25 +7,23 @@ import io.legado.app.base.BaseViewModel
import io.legado.app.data.entities.Book
class BookInfoEditViewModel(application: Application) : BaseViewModel(application) {
var book: Book? = null
val bookData = MutableLiveData<Book>()
fun loadBook(bookUrl: String) {
execute {
App.db.bookDao().getBook(bookUrl)?.let {
book = App.db.bookDao().getBook(bookUrl)
book?.let {
bookData.postValue(it)
}
}
}
fun saveBook(book: Book, success: (() -> Unit)?, error: ((msg: String) -> Unit)?) {
fun saveBook(book: Book, success: (() -> Unit)?) {
execute {
App.db.bookDao().insert(book)
}.onSuccess {
success?.invoke()
}.onError {
error?.invoke(it.localizedMessage)
}
}
}
Loading…
Cancel
Save