From 720ced5291b48ba6e65d20156ca1d199ee8545d1 Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 22 Aug 2019 14:32:43 +0800 Subject: [PATCH] up --- .../app/ui/bookinfo/BookInfoActivity.kt | 40 ++++++++++++------- .../app/ui/bookinfo/BookInfoViewModel.kt | 21 ++++++++++ 2 files changed, 47 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoActivity.kt b/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoActivity.kt index a387b1e0f..b618f29d6 100644 --- a/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoActivity.kt +++ b/app/src/main/java/io/legado/app/ui/bookinfo/BookInfoActivity.kt @@ -9,7 +9,6 @@ import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.LinearLayoutManager import io.legado.app.R import io.legado.app.base.VMBaseActivity -import io.legado.app.constant.Bus import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter import io.legado.app.help.ImageLoader @@ -20,7 +19,6 @@ import io.legado.app.ui.readbook.ReadBookActivity import io.legado.app.ui.sourceedit.SourceEditActivity import io.legado.app.utils.getViewModel import io.legado.app.utils.gone -import io.legado.app.utils.postEvent import io.legado.app.utils.visible import kotlinx.android.synthetic.main.activity_book_info.* import kotlinx.android.synthetic.main.view_title_bar.* @@ -156,16 +154,7 @@ class BookInfoActivity : VMBaseActivity(R.layout.activity_boo private fun initOnClick() { tv_read.onClick { viewModel.bookData.value?.let { - if (!viewModel.inBookshelf) { - viewModel.saveBook { - startActivity( - Pair("bookUrl", it.bookUrl), - Pair("inBookshelf", false) - ) - } - } else { - startActivity(Pair("bookUrl", it.bookUrl)) - } + readBook(it) } } tv_shelf.onClick { @@ -174,7 +163,7 @@ class BookInfoActivity : VMBaseActivity(R.layout.activity_boo tv_shelf.text = getString(R.string.add_to_shelf) } } else { - viewModel.saveBook { + viewModel.addToBookshelf { tv_shelf.text = getString(R.string.remove_from_bookshelf) } } @@ -209,6 +198,23 @@ class BookInfoActivity : VMBaseActivity(R.layout.activity_boo } } + private fun readBook(book: Book) { + if (!viewModel.inBookshelf) { + viewModel.saveBook { + viewModel.saveChapterList { + startActivity( + Pair("bookUrl", book.bookUrl), + Pair("inBookshelf", false) + ) + } + } + } else { + viewModel.saveBook { + startActivity(Pair("bookUrl", book.bookUrl)) + } + } + } + override fun oldBook(): Book? { return viewModel.bookData.value } @@ -218,7 +224,13 @@ class BookInfoActivity : VMBaseActivity(R.layout.activity_boo } override fun openChapter(chapter: BookChapter) { - postEvent(Bus.OPEN_CHAPTER, chapter) + if (chapter.index != viewModel.durChapterIndex) { + viewModel.bookData.value?.let { + it.durChapterIndex = chapter.index + it.durChapterPos = 0 + readBook(it) + } + } } override fun durChapterIndex(): Int { 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 8bdbf18a4..7b6c093bd 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 @@ -102,6 +102,26 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { } fun saveBook(success: (() -> Unit)?) { + execute { + bookData.value?.let { book -> + App.db.bookDao().insert(book) + } + }.onSuccess { + success?.invoke() + } + } + + fun saveChapterList(success: (() -> Unit)?) { + execute { + chapterListData.value?.let { + App.db.bookChapterDao().insert(*it.toTypedArray()) + } + }.onSuccess { + success?.invoke() + } + } + + fun addToBookshelf(success: (() -> Unit)?) { execute { bookData.value?.let { book -> App.db.bookDao().insert(book) @@ -120,6 +140,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { bookData.value?.let { App.db.bookDao().delete(it.bookUrl) } + inBookshelf = false }.onSuccess { success?.invoke() }