From 66ea00b41e8ca0ae410f140ae05ab176072be7ff Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 13 Aug 2019 09:48:03 +0800 Subject: [PATCH] up --- .../app/ui/bookinfo/BookInfoActivity.kt | 33 +++++++++++++++---- .../app/ui/bookinfo/BookInfoViewModel.kt | 20 +++++++++-- .../main/res/layout/activity_book_info.xml | 2 +- app/src/main/res/values/strings.xml | 1 + 4 files changed, 45 insertions(+), 11 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 a06f8f2d7..89333ef25 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 @@ -10,6 +10,7 @@ import io.legado.app.utils.getViewModel import io.legado.app.utils.gone import io.legado.app.utils.visible import kotlinx.android.synthetic.main.activity_book_info.* +import org.jetbrains.anko.sdk27.listeners.onClick class BookInfoActivity : VMBaseActivity(R.layout.activity_book_info) { override val viewModel: BookInfoViewModel @@ -17,14 +18,9 @@ class BookInfoActivity : VMBaseActivity(R.layout.activity_boo override fun onActivityCreated(savedInstanceState: Bundle?) { viewModel.bookData.observe(this, Observer { showBook(it) }) - viewModel.isLoadingData.observe(this, Observer { - if (it) { - tv_loading.visible() - } else { - tv_loading.gone() - } - }) + viewModel.isLoadingData.observe(this, Observer { upLoading(it) }) viewModel.loadBook(intent) + initView() } private fun showBook(book: Book) { @@ -72,5 +68,28 @@ class BookInfoActivity : VMBaseActivity(R.layout.activity_boo } } + private fun upLoading(isLoading: Boolean) { + if (isLoading) { + tv_loading.visible() + } else { + if (viewModel.inBookshelf) { + tv_shelf.text = getString(R.string.remove_from_bookshelf) + } else { + tv_shelf.text = getString(R.string.add_to_shelf) + } + tv_loading.gone() + } + } + + private fun initView() { + tv_read.onClick { + } + tv_shelf.onClick { + viewModel.saveBook { + tv_shelf.text = getString(R.string.remove_from_bookshelf) + } + } + tv_loading.onClick { } + } } \ 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 235e57483..af4291452 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 @@ -6,10 +6,12 @@ import androidx.lifecycle.MutableLiveData import io.legado.app.App import io.legado.app.base.BaseViewModel import io.legado.app.data.entities.Book +import io.legado.app.data.entities.BookChapter class BookInfoViewModel(application: Application) : BaseViewModel(application) { val bookData = MutableLiveData() + val chapterListData = MutableLiveData>() val isLoadingData = MutableLiveData() var inBookshelf = false @@ -23,7 +25,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { } ?: intent.getStringExtra("searchBookUrl")?.let { App.db.searchBookDao().getSearchBook(it)?.toBook()?.let { book -> bookData.postValue(book) - if (book.tocUrl.isNullOrEmpty()) { + if (book.tocUrl.isEmpty()) { loadBookInfo() } else { loadChapter() @@ -34,11 +36,23 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { } fun loadBookInfo() { - + isLoadingData.postValue(false) } fun loadChapter() { - + isLoadingData.postValue(false) } + fun saveBook(success: (() -> Unit)?) { + execute { + bookData.value?.let { book -> + App.db.bookDao().insert(book) + } + chapterListData.value?.let { + App.db.bookChapterDao().insert(*it.toTypedArray()) + } + }.onSuccess { + success?.invoke() + } + } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_book_info.xml b/app/src/main/res/layout/activity_book_info.xml index a905af79f..d12e9980e 100644 --- a/app/src/main/res/layout/activity_book_info.xml +++ b/app/src/main/res/layout/activity_book_info.xml @@ -224,7 +224,7 @@ android:focusable="true" android:gravity="center" android:includeFontPadding="false" - android:text="@string/start_read" + android:text="@string/reading" android:textColor="@color/tv_text_default" android:textSize="15sp" /> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 10b7bec83..6caf136b6 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -473,4 +473,5 @@ 我的 ]]> + 阅读