From 669d50b98ce9cb62687d4d62d202394c68210fe2 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 1 Sep 2019 09:54:05 +0800 Subject: [PATCH] up --- .../io/legado/app/data/dao/BookChapterDao.kt | 4 +- .../app/ui/chapterlist/ChapterListAdapter.kt | 51 +++++-------------- .../app/ui/chapterlist/ChapterListFragment.kt | 21 ++++---- 3 files changed, 24 insertions(+), 52 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/dao/BookChapterDao.kt b/app/src/main/java/io/legado/app/data/dao/BookChapterDao.kt index 6473da76f..fdbff2bf1 100644 --- a/app/src/main/java/io/legado/app/data/dao/BookChapterDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/BookChapterDao.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 + fun observeByBook(bookUrl: String): LiveData> @Query("select * from chapters where bookUrl = :bookUrl") fun getChapterList(bookUrl: String): List diff --git a/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListAdapter.kt b/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListAdapter.kt index aa975798a..c7adcf2bb 100644 --- a/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListAdapter.kt @@ -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(DIFF_CALLBACK) { +class ChapterListAdapter(context: Context, val callback: Callback) : + SimpleRecyclerAdapter(context, R.layout.item_chapter_list) { - companion object { - - @JvmField - val DIFF_CALLBACK = object : DiffUtil.ItemCallback() { - 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) { + 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) } } } diff --git a/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListFragment.kt b/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListFragment.kt index d237ea2dd..4d3e947b6 100644 --- a/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListFragment.kt +++ b/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListFragment.kt @@ -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(R.layout.fragme get() = getViewModelOfActivity(ChapterListViewModel::class.java) lateinit var adapter: ChapterListAdapter - private var liveData: LiveData>? = null private var durChapterIndex = 0 override fun onViewCreated(view: View, savedInstanceState: Bundle?) { @@ -37,7 +33,7 @@ class ChapterListFragment : VMBaseFragment(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(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() {