fix: 书籍信息界面

pull/95/head
kunfei 5 years ago
parent 790047d609
commit eefb4155da
  1. 52
      app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt
  2. 20
      app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt
  3. 69
      app/src/main/res/layout/activity_book_info.xml

@ -56,8 +56,7 @@ class BookInfoActivity :
title_bar.background.alpha = 0 title_bar.background.alpha = 0
tv_intro.movementMethod = ScrollingMovementMethod.getInstance() tv_intro.movementMethod = ScrollingMovementMethod.getInstance()
viewModel.bookData.observe(this, Observer { showBook(it) }) viewModel.bookData.observe(this, Observer { showBook(it) })
viewModel.isLoadingData.observe(this, Observer { upLoading(it) }) viewModel.chapterListData.observe(this, Observer { upLoading(false, it) })
viewModel.chapterListData.observe(this, Observer { showChapter(it) })
viewModel.groupData.observe(this, Observer { viewModel.groupData.observe(this, Observer {
if (it == null) { if (it == null) {
tv_group.text = getString(R.string.group_s, getString(R.string.no_group)) tv_group.text = getString(R.string.group_s, getString(R.string.no_group))
@ -115,8 +114,9 @@ class BookInfoActivity :
tv_author.text = getString(R.string.author_show, book.author) tv_author.text = getString(R.string.author_show, book.author)
tv_origin.text = getString(R.string.origin_show, book.originName) tv_origin.text = getString(R.string.origin_show, book.originName)
tv_lasted.text = getString(R.string.lasted_show, book.latestChapterTitle) tv_lasted.text = getString(R.string.lasted_show, book.latestChapterTitle)
tv_toc.text = getString(R.string.toc_s, book.latestChapterTitle) tv_toc.text = getString(R.string.toc_s, getString(R.string.loading))
tv_intro.text = book.getDisplayIntro() tv_intro.text = book.getDisplayIntro()
upTvBookshelf()
val kinds = book.getKindList() val kinds = book.getKindList()
if (kinds.isEmpty()) { if (kinds.isEmpty()) {
ll_kind.gone() ll_kind.gone()
@ -164,27 +164,32 @@ class BookInfoActivity :
.apply(bitmapTransform(BlurTransformation(this, 25))) .apply(bitmapTransform(BlurTransformation(this, 25)))
} }
private fun showChapter(chapterList: List<BookChapter>) { private fun upLoading(isLoading: Boolean, chapterList: List<BookChapter>? = null) {
viewModel.bookData.value?.let { when {
if (it.durChapterIndex < chapterList.size) { isLoading -> {
tv_toc.text = getString(R.string.toc_s, chapterList[it.durChapterIndex].title) tv_toc.text = getString(R.string.toc_s, getString(R.string.loading))
} else { }
tv_toc.text = getString(R.string.toc_s, chapterList.last().title) chapterList.isNullOrEmpty() -> {
tv_toc.text = getString(R.string.toc_s, getString(R.string.error_load_toc))
}
else -> {
viewModel.bookData.value?.let {
if (it.durChapterIndex < chapterList.size) {
tv_toc.text =
getString(R.string.toc_s, chapterList[it.durChapterIndex].title)
} else {
tv_toc.text = getString(R.string.toc_s, chapterList.last().title)
}
}
} }
} }
upLoading(false)
} }
private fun upLoading(isLoading: Boolean) { private fun upTvBookshelf() {
if (isLoading) { if (viewModel.inBookshelf) {
tv_loading.visible() tv_shelf.text = getString(R.string.remove_from_bookshelf)
} else { } else {
if (viewModel.inBookshelf) { tv_shelf.text = getString(R.string.add_to_shelf)
tv_shelf.text = getString(R.string.remove_from_bookshelf)
} else {
tv_shelf.text = getString(R.string.add_to_shelf)
}
tv_loading.gone()
} }
} }
@ -202,19 +207,14 @@ class BookInfoActivity :
tv_shelf.onClick { tv_shelf.onClick {
if (viewModel.inBookshelf) { if (viewModel.inBookshelf) {
viewModel.delBook { viewModel.delBook {
tv_shelf.text = getString(R.string.add_to_shelf) upTvBookshelf()
} }
} else { } else {
viewModel.addToBookshelf { viewModel.addToBookshelf {
tv_shelf.text = getString(R.string.remove_from_bookshelf) upTvBookshelf()
} }
} }
} }
tv_loading.onClick {
viewModel.bookData.value?.let {
viewModel.loadBookInfo(it)
}
}
tv_origin.onClick { tv_origin.onClick {
viewModel.bookData.value?.let { viewModel.bookData.value?.let {
startActivity<BookSourceEditActivity>(Pair("data", it.origin)) startActivity<BookSourceEditActivity>(Pair("data", it.origin))

@ -17,7 +17,6 @@ import kotlinx.coroutines.Dispatchers.IO
class BookInfoViewModel(application: Application) : BaseViewModel(application) { class BookInfoViewModel(application: Application) : BaseViewModel(application) {
val bookData = MutableLiveData<Book>() val bookData = MutableLiveData<Book>()
val chapterListData = MutableLiveData<List<BookChapter>>() val chapterListData = MutableLiveData<List<BookChapter>>()
val isLoadingData = MutableLiveData<Boolean>()
var durChapterIndex = 0 var durChapterIndex = 0
var inBookshelf = false var inBookshelf = false
var groupData = MutableLiveData<BookGroup>() var groupData = MutableLiveData<BookGroup>()
@ -45,7 +44,6 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
val chapterList = App.db.bookChapterDao().getChapterList(book.bookUrl) val chapterList = App.db.bookChapterDao().getChapterList(book.bookUrl)
if (chapterList.isNotEmpty()) { if (chapterList.isNotEmpty()) {
chapterListData.postValue(chapterList) chapterListData.postValue(chapterList)
isLoadingData.postValue(false)
} else { } else {
loadChapter(book) loadChapter(book)
} }
@ -60,7 +58,6 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
if (book.isLocalBook()) { if (book.isLocalBook()) {
loadChapter(book, changeDruChapterIndex) loadChapter(book, changeDruChapterIndex)
} else { } else {
isLoadingData.postValue(true)
App.db.bookSourceDao().getBookSource(book.origin)?.let { bookSource -> App.db.bookSourceDao().getBookSource(book.origin)?.let { bookSource ->
WebBook(bookSource).getBookInfo(book, this) WebBook(bookSource).getBookInfo(book, this)
.onSuccess(IO) { .onSuccess(IO) {
@ -72,11 +69,10 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
loadChapter(it, changeDruChapterIndex) loadChapter(it, changeDruChapterIndex)
} }
}.onError { }.onError {
isLoadingData.postValue(false)
toast(R.string.error_get_book_info) toast(R.string.error_get_book_info)
} }
} ?: let { } ?: let {
isLoadingData.postValue(false) chapterListData.postValue(null)
toast(R.string.error_no_source) toast(R.string.error_no_source)
} }
} }
@ -88,17 +84,11 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
changeDruChapterIndex: ((chapters: List<BookChapter>) -> Unit)? = null changeDruChapterIndex: ((chapters: List<BookChapter>) -> Unit)? = null
) { ) {
execute { execute {
isLoadingData.postValue(true)
if (book.isLocalBook()) { if (book.isLocalBook()) {
AnalyzeTxtFile.analyze(context, book).let { AnalyzeTxtFile.analyze(context, book).let {
App.db.bookDao().update(book) App.db.bookDao().update(book)
App.db.bookChapterDao().insert(*it.toTypedArray()) App.db.bookChapterDao().insert(*it.toTypedArray())
if (changeDruChapterIndex == null) { chapterListData.postValue(it)
chapterListData.postValue(it)
isLoadingData.postValue(false)
} else {
changeDruChapterIndex(it)
}
} }
} else { } else {
App.db.bookSourceDao().getBookSource(book.origin)?.let { bookSource -> App.db.bookSourceDao().getBookSource(book.origin)?.let { bookSource ->
@ -112,21 +102,19 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
} }
if (changeDruChapterIndex == null) { if (changeDruChapterIndex == null) {
chapterListData.postValue(it) chapterListData.postValue(it)
isLoadingData.postValue(false)
} else { } else {
changeDruChapterIndex(it) changeDruChapterIndex(it)
} }
} else { } else {
isLoadingData.postValue(false)
toast(R.string.chapter_list_empty) toast(R.string.chapter_list_empty)
} }
} }
}.onError { }.onError {
isLoadingData.postValue(false) chapterListData.postValue(null)
toast(R.string.error_get_chapter_list) toast(R.string.error_get_chapter_list)
} }
} ?: let { } ?: let {
isLoadingData.postValue(false) chapterListData.postValue(null)
toast(R.string.error_no_source) toast(R.string.error_no_source)
} }
} }

@ -229,63 +229,40 @@
</LinearLayout> </LinearLayout>
<FrameLayout <LinearLayout
android:id="@+id/fl_action" android:id="@+id/fl_action"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="50dp" android:layout_height="50dp"
android:background="@color/background_menu"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"> app:layout_constraintBottom_toBottomOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_shelf"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:gravity="center"
android:includeFontPadding="false"
android:text="@string/remove_from_bookshelf"
android:textColor="@color/tv_text_default"
android:textSize="15sp" />
<io.legado.app.lib.theme.view.ATEAccentBgTextView
android:id="@+id/tv_read"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/selector_btn_accent_bg"
android:gravity="center"
android:includeFontPadding="false"
android:text="@string/reading"
android:textColor="@color/tv_text_button_nor"
android:textSize="15sp" />
</LinearLayout>
<TextView <TextView
android:id="@+id/tv_loading" android:id="@+id/tv_shelf"
android:layout_width="match_parent" android:layout_width="0dp"
android:layout_height="match_parent" android:layout_height="match_parent"
android:background="@color/tv_text_button_nor" android:layout_weight="1"
android:background="?attr/selectableItemBackground"
android:clickable="true" android:clickable="true"
android:focusable="true" android:focusable="true"
android:gravity="center" android:gravity="center"
android:text="@string/data_loading" android:includeFontPadding="false"
android:textColor="#767676" android:text="@string/remove_from_bookshelf"
android:textSize="16sp" /> android:textColor="@color/tv_text_default"
android:textSize="15sp" />
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="@color/bg_divider_line" />
</FrameLayout> <io.legado.app.lib.theme.view.ATEAccentBgTextView
android:id="@+id/tv_read"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/selector_btn_accent_bg"
android:gravity="center"
android:includeFontPadding="false"
android:text="@string/reading"
android:textColor="@color/tv_text_button_nor"
android:textSize="15sp" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
Loading…
Cancel
Save