pull/32/head
kunfei 5 years ago
parent b3a9c875ce
commit d4fca01df1
  1. 9
      app/src/main/java/io/legado/app/ui/bookinfo/BookInfoActivity.kt
  2. 3
      app/src/main/java/io/legado/app/ui/bookinfo/BookInfoViewModel.kt
  3. 12
      app/src/main/java/io/legado/app/ui/bookinfo/ChapterListAdapter.kt

@ -9,6 +9,7 @@ import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.VMBaseActivity 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.Book
import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookChapter
import io.legado.app.help.ImageLoader import io.legado.app.help.ImageLoader
@ -19,6 +20,7 @@ import io.legado.app.ui.readbook.ReadBookActivity
import io.legado.app.ui.sourceedit.SourceEditActivity import io.legado.app.ui.sourceedit.SourceEditActivity
import io.legado.app.utils.getViewModel import io.legado.app.utils.getViewModel
import io.legado.app.utils.gone import io.legado.app.utils.gone
import io.legado.app.utils.postEvent
import io.legado.app.utils.visible import io.legado.app.utils.visible
import kotlinx.android.synthetic.main.activity_book_info.* import kotlinx.android.synthetic.main.activity_book_info.*
import kotlinx.android.synthetic.main.view_title_bar.* import kotlinx.android.synthetic.main.view_title_bar.*
@ -127,6 +129,7 @@ class BookInfoActivity : VMBaseActivity<BookInfoViewModel>(R.layout.activity_boo
} }
adapter.clearItems() adapter.clearItems()
adapter.addItems(chapterList) adapter.addItems(chapterList)
rv_chapter_list.scrollToPosition(viewModel.durChapterIndex)
} }
private fun upLoading(isLoading: Boolean) { private fun upLoading(isLoading: Boolean) {
@ -206,7 +209,11 @@ class BookInfoActivity : VMBaseActivity<BookInfoViewModel>(R.layout.activity_boo
viewModel.changeTo(book) viewModel.changeTo(book)
} }
override fun skipToChapter(index: Int) { override fun openChapter(chapter: BookChapter) {
postEvent(Bus.OPEN_CHAPTER, chapter)
}
override fun durChapterIndex(): Int {
return viewModel.durChapterIndex
} }
} }

@ -16,6 +16,7 @@ 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>() val isLoadingData = MutableLiveData<Boolean>()
var durChapterIndex = 0
var inBookshelf = false var inBookshelf = false
fun loadBook(intent: Intent) { fun loadBook(intent: Intent) {
@ -23,6 +24,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
intent.getStringExtra("bookUrl")?.let { intent.getStringExtra("bookUrl")?.let {
App.db.bookDao().getBook(it)?.let { book -> App.db.bookDao().getBook(it)?.let { book ->
inBookshelf = true inBookshelf = true
durChapterIndex = book.durChapterIndex
bookData.postValue(book) bookData.postValue(book)
val chapterList = App.db.bookChapterDao().getChapterList(it) val chapterList = App.db.bookChapterDao().getChapterList(it)
if (chapterList.isNotEmpty()) { if (chapterList.isNotEmpty()) {
@ -34,6 +36,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
} }
} ?: intent.getStringExtra("searchBookUrl")?.let { } ?: intent.getStringExtra("searchBookUrl")?.let {
App.db.searchBookDao().getSearchBook(it)?.toBook()?.let { book -> App.db.searchBookDao().getSearchBook(it)?.toBook()?.let { book ->
durChapterIndex = book.durChapterIndex
bookData.postValue(book) bookData.postValue(book)
if (book.tocUrl.isEmpty()) { if (book.tocUrl.isEmpty()) {
loadBookInfo(book) loadBookInfo(book)

@ -5,8 +5,10 @@ import io.legado.app.R
import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.ItemViewHolder
import io.legado.app.base.adapter.SimpleRecyclerAdapter import io.legado.app.base.adapter.SimpleRecyclerAdapter
import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookChapter
import io.legado.app.lib.theme.accentColor
import kotlinx.android.synthetic.main.item_chapter_list.view.* import kotlinx.android.synthetic.main.item_chapter_list.view.*
import org.jetbrains.anko.sdk27.listeners.onClick import org.jetbrains.anko.sdk27.listeners.onClick
import org.jetbrains.anko.textColorResource
class ChapterListAdapter(context: Context, var callBack: CallBack) : class ChapterListAdapter(context: Context, var callBack: CallBack) :
SimpleRecyclerAdapter<BookChapter>(context, R.layout.item_chapter_list) { SimpleRecyclerAdapter<BookChapter>(context, R.layout.item_chapter_list) {
@ -14,13 +16,19 @@ class ChapterListAdapter(context: Context, var callBack: CallBack) :
override fun convert(holder: ItemViewHolder, item: BookChapter, payloads: MutableList<Any>) { override fun convert(holder: ItemViewHolder, item: BookChapter, payloads: MutableList<Any>) {
holder.itemView.apply { holder.itemView.apply {
tv_chapter_name.text = item.title tv_chapter_name.text = item.title
if (item.index == callBack.durChapterIndex()) {
tv_chapter_name.setTextColor(context.accentColor)
} else {
tv_chapter_name.textColorResource = R.color.tv_text_default
}
this.onClick { this.onClick {
callBack.skipToChapter(item.index) callBack.openChapter(item)
} }
} }
} }
interface CallBack { interface CallBack {
fun skipToChapter(index: Int) fun openChapter(chapter: BookChapter)
fun durChapterIndex(): Int
} }
} }
Loading…
Cancel
Save