pull/32/head
kunfei 5 years ago
parent 61d2f90d8e
commit 669d50b98c
  1. 4
      app/src/main/java/io/legado/app/data/dao/BookChapterDao.kt
  2. 51
      app/src/main/java/io/legado/app/ui/chapterlist/ChapterListAdapter.kt
  3. 21
      app/src/main/java/io/legado/app/ui/chapterlist/ChapterListFragment.kt

@ -1,6 +1,6 @@
package io.legado.app.data.dao
import androidx.paging.DataSource
import androidx.lifecycle.LiveData
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
@ -11,7 +11,7 @@ import io.legado.app.data.entities.BookChapter
interface BookChapterDao {
@Query("select * from chapters where bookUrl = :bookUrl")
fun observeByBook(bookUrl: String): DataSource.Factory<Int, BookChapter>
fun observeByBook(bookUrl: String): LiveData<List<BookChapter>>
@Query("select * from chapters where bookUrl = :bookUrl")
fun getChapterList(bookUrl: String): List<BookChapter>

@ -1,12 +1,9 @@
package io.legado.app.ui.chapterlist
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.paging.PagedListAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import android.content.Context
import io.legado.app.R
import io.legado.app.base.adapter.ItemViewHolder
import io.legado.app.base.adapter.SimpleRecyclerAdapter
import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter
import io.legado.app.help.BookHelp
@ -15,46 +12,22 @@ import io.legado.app.utils.getCompatColor
import kotlinx.android.synthetic.main.item_bookmark.view.*
import org.jetbrains.anko.sdk27.listeners.onClick
class ChapterListAdapter(val callback: Callback) :
PagedListAdapter<BookChapter, ChapterListAdapter.MyViewHolder>(DIFF_CALLBACK) {
class ChapterListAdapter(context: Context, val callback: Callback) :
SimpleRecyclerAdapter<BookChapter>(context, R.layout.item_chapter_list) {
companion object {
@JvmField
val DIFF_CALLBACK = object : DiffUtil.ItemCallback<BookChapter>() {
override fun areItemsTheSame(oldItem: BookChapter, newItem: BookChapter): Boolean =
oldItem.bookUrl == newItem.bookUrl
&& oldItem.url == newItem.url
override fun areContentsTheSame(oldItem: BookChapter, newItem: BookChapter): Boolean =
oldItem.title == newItem.title
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
return MyViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_chapter_list, parent, false))
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
getItem(position)?.let {
holder.bind(it, callback)
}
}
class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
fun bind(bookChapter: BookChapter, callback: Callback) = with(itemView) {
if (callback.durChapterIndex() == bookChapter.index) {
override fun convert(holder: ItemViewHolder, item: BookChapter, payloads: MutableList<Any>) {
with(holder.itemView) {
if (callback.durChapterIndex() == item.index) {
tv_chapter_name.setTextColor(context.accentColor)
} else {
tv_chapter_name.setTextColor(context.getCompatColor(R.color.tv_text_default))
}
tv_chapter_name.text = bookChapter.title
itemView.onClick {
callback.openChapter(bookChapter)
tv_chapter_name.text = item.title
this.onClick {
callback.openChapter(item)
}
callback.book()?.let {
tv_chapter_name.paint.isFakeBoldText = BookHelp.hasContent(it, bookChapter)
tv_chapter_name.paint.isFakeBoldText = BookHelp.hasContent(it, item)
}
}
}

@ -3,10 +3,7 @@ package io.legado.app.ui.chapterlist
import android.os.Bundle
import android.view.View
import android.widget.LinearLayout
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import androidx.paging.LivePagedListBuilder
import androidx.paging.PagedList
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import io.legado.app.App
@ -26,7 +23,6 @@ class ChapterListFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragme
get() = getViewModelOfActivity(ChapterListViewModel::class.java)
lateinit var adapter: ChapterListAdapter
private var liveData: LiveData<PagedList<BookChapter>>? = null
private var durChapterIndex = 0
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
@ -37,7 +33,7 @@ class ChapterListFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragme
}
private fun initRecyclerView() {
adapter = ChapterListAdapter(this)
adapter = ChapterListAdapter(requireContext(), this)
recycler_view.layoutManager = LinearLayoutManager(requireContext())
recycler_view.addItemDecoration(
DividerItemDecoration(
@ -49,14 +45,17 @@ class ChapterListFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragme
}
private fun initData() {
liveData?.removeObservers(viewLifecycleOwner)
liveData = LivePagedListBuilder(App.db.bookChapterDao().observeByBook(viewModel.bookUrl ?: ""), 30).build()
liveData?.observe(viewLifecycleOwner, Observer { adapter.submitList(it) })
viewModel.bookDate.value?.let {
loadBookFinish(it)
} ?: viewModel.bookDate.observe(viewLifecycleOwner, Observer {
viewModel.bookDate.observe(viewLifecycleOwner, Observer {
loadBookFinish(it)
})
viewModel.bookUrl?.let { bookUrl ->
App.db.bookChapterDao().observeByBook(bookUrl).observe(viewLifecycleOwner, Observer {
adapter.setItems(it)
viewModel.bookDate.value?.let { book ->
loadBookFinish(book)
}
})
}
}
private fun initView() {

Loading…
Cancel
Save