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

@ -1,12 +1,9 @@
package io.legado.app.ui.chapterlist package io.legado.app.ui.chapterlist
import android.view.LayoutInflater import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.paging.PagedListAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import io.legado.app.R 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.Book
import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookChapter
import io.legado.app.help.BookHelp 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 kotlinx.android.synthetic.main.item_bookmark.view.*
import org.jetbrains.anko.sdk27.listeners.onClick import org.jetbrains.anko.sdk27.listeners.onClick
class ChapterListAdapter(val callback: Callback) : class ChapterListAdapter(context: Context, val callback: Callback) :
PagedListAdapter<BookChapter, ChapterListAdapter.MyViewHolder>(DIFF_CALLBACK) { SimpleRecyclerAdapter<BookChapter>(context, R.layout.item_chapter_list) {
companion object { override fun convert(holder: ItemViewHolder, item: BookChapter, payloads: MutableList<Any>) {
with(holder.itemView) {
@JvmField if (callback.durChapterIndex() == item.index) {
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) {
tv_chapter_name.setTextColor(context.accentColor) tv_chapter_name.setTextColor(context.accentColor)
} else { } else {
tv_chapter_name.setTextColor(context.getCompatColor(R.color.tv_text_default)) tv_chapter_name.setTextColor(context.getCompatColor(R.color.tv_text_default))
} }
tv_chapter_name.text = bookChapter.title tv_chapter_name.text = item.title
itemView.onClick { this.onClick {
callback.openChapter(bookChapter) callback.openChapter(item)
} }
callback.book()?.let { 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.os.Bundle
import android.view.View import android.view.View
import android.widget.LinearLayout import android.widget.LinearLayout
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
import androidx.paging.LivePagedListBuilder
import androidx.paging.PagedList
import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import io.legado.app.App import io.legado.app.App
@ -26,7 +23,6 @@ class ChapterListFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragme
get() = getViewModelOfActivity(ChapterListViewModel::class.java) get() = getViewModelOfActivity(ChapterListViewModel::class.java)
lateinit var adapter: ChapterListAdapter lateinit var adapter: ChapterListAdapter
private var liveData: LiveData<PagedList<BookChapter>>? = null
private var durChapterIndex = 0 private var durChapterIndex = 0
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
@ -37,7 +33,7 @@ class ChapterListFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragme
} }
private fun initRecyclerView() { private fun initRecyclerView() {
adapter = ChapterListAdapter(this) adapter = ChapterListAdapter(requireContext(), this)
recycler_view.layoutManager = LinearLayoutManager(requireContext()) recycler_view.layoutManager = LinearLayoutManager(requireContext())
recycler_view.addItemDecoration( recycler_view.addItemDecoration(
DividerItemDecoration( DividerItemDecoration(
@ -49,14 +45,17 @@ class ChapterListFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragme
} }
private fun initData() { private fun initData() {
liveData?.removeObservers(viewLifecycleOwner) viewModel.bookDate.observe(viewLifecycleOwner, Observer {
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 {
loadBookFinish(it) 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() { private fun initView() {

Loading…
Cancel
Save