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
tv_intro.movementMethod = ScrollingMovementMethod.getInstance()
viewModel.bookData.observe(this, Observer { showBook(it) })
viewModel.isLoadingData.observe(this, Observer { upLoading(it) })
viewModel.chapterListData.observe(this, Observer { showChapter(it) })
viewModel.chapterListData.observe(this, Observer { upLoading(false, it) })
viewModel.groupData.observe(this, Observer {
if (it == null) {
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_origin.text = getString(R.string.origin_show, book.originName)
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()
upTvBookshelf()
val kinds = book.getKindList()
if (kinds.isEmpty()) {
ll_kind.gone()
@ -164,27 +164,32 @@ class BookInfoActivity :
.apply(bitmapTransform(BlurTransformation(this, 25)))
}
private fun showChapter(chapterList: List<BookChapter>) {
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)
private fun upLoading(isLoading: Boolean, chapterList: List<BookChapter>? = null) {
when {
isLoading -> {
tv_toc.text = getString(R.string.toc_s, getString(R.string.loading))
}
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) {
if (isLoading) {
tv_loading.visible()
private fun upTvBookshelf() {
if (viewModel.inBookshelf) {
tv_shelf.text = getString(R.string.remove_from_bookshelf)
} 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()
tv_shelf.text = getString(R.string.add_to_shelf)
}
}
@ -202,19 +207,14 @@ class BookInfoActivity :
tv_shelf.onClick {
if (viewModel.inBookshelf) {
viewModel.delBook {
tv_shelf.text = getString(R.string.add_to_shelf)
upTvBookshelf()
}
} else {
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 {
viewModel.bookData.value?.let {
startActivity<BookSourceEditActivity>(Pair("data", it.origin))

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

@ -229,63 +229,40 @@
</LinearLayout>
<FrameLayout
<LinearLayout
android:id="@+id/fl_action"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/background_menu"
android:orientation="horizontal"
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
android:id="@+id/tv_loading"
android:layout_width="match_parent"
android:id="@+id/tv_shelf"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="@color/tv_text_button_nor"
android:layout_weight="1"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:gravity="center"
android:text="@string/data_loading"
android:textColor="#767676"
android:textSize="16sp" />
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="@color/bg_divider_line" />
android:includeFontPadding="false"
android:text="@string/remove_from_bookshelf"
android:textColor="@color/tv_text_default"
android:textSize="15sp" />
</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>
Loading…
Cancel
Save