From 6c3f5aaff3850733e8210b83fa89d941f27d0d10 Mon Sep 17 00:00:00 2001 From: gedoor Date: Sat, 8 May 2021 11:20:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/data/dao/BookSourceDao.kt | 4 -- .../io/legado/app/data/dao/BookmarkDao.kt | 6 +- .../io/legado/app/data/dao/SearchBookDao.kt | 7 --- .../legado/app/ui/book/toc/BookmarkAdapter.kt | 61 +++++++------------ .../app/ui/book/toc/BookmarkFragment.kt | 22 ++----- 5 files changed, 32 insertions(+), 68 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt b/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt index 7a40d4cc8..2d2dcff40 100644 --- a/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt @@ -1,7 +1,6 @@ package io.legado.app.data.dao import androidx.lifecycle.LiveData -import androidx.paging.DataSource import androidx.room.* import io.legado.app.data.entities.BookSource @@ -41,9 +40,6 @@ interface BookSourceDao { @Query("select distinct bookSourceGroup from book_sources where enabledExplore = 1 and trim(exploreUrl) <> '' and trim(bookSourceGroup) <> ''") fun liveExploreGroup(): LiveData> - @Query("select * from book_sources where enabledExplore = 1 order by customOrder asc") - fun observeFind(): DataSource.Factory - @Query("select * from book_sources where bookSourceGroup like '%' || :group || '%'") fun getByGroup(group: String): List diff --git a/app/src/main/java/io/legado/app/data/dao/BookmarkDao.kt b/app/src/main/java/io/legado/app/data/dao/BookmarkDao.kt index 16fdff455..633c1187c 100644 --- a/app/src/main/java/io/legado/app/data/dao/BookmarkDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/BookmarkDao.kt @@ -1,6 +1,6 @@ package io.legado.app.data.dao -import androidx.paging.DataSource +import androidx.lifecycle.LiveData import androidx.room.* import io.legado.app.data.entities.Bookmark @@ -16,10 +16,10 @@ interface BookmarkDao { bookUrl: String, bookName: String, bookAuthor: String - ): DataSource.Factory + ): LiveData> @Query("SELECT * FROM bookmarks where bookUrl = :bookUrl and chapterName like '%'||:key||'%' or content like '%'||:key||'%'") - fun liveDataSearch(bookUrl: String, key: String): DataSource.Factory + fun liveDataSearch(bookUrl: String, key: String): LiveData> @Insert(onConflict = OnConflictStrategy.REPLACE) fun insert(vararg bookmark: Bookmark) diff --git a/app/src/main/java/io/legado/app/data/dao/SearchBookDao.kt b/app/src/main/java/io/legado/app/data/dao/SearchBookDao.kt index 9618ad317..8f53d0170 100644 --- a/app/src/main/java/io/legado/app/data/dao/SearchBookDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/SearchBookDao.kt @@ -1,6 +1,5 @@ package io.legado.app.data.dao -import androidx.paging.DataSource import androidx.room.Dao import androidx.room.Insert import androidx.room.OnConflictStrategy @@ -10,12 +9,6 @@ import io.legado.app.data.entities.SearchBook @Dao interface SearchBookDao { - @Query("SELECT * FROM searchBooks") - fun observeAll(): DataSource.Factory - - @Query("SELECT * FROM searchBooks where time >= :time") - fun observeNew(time: Long): DataSource.Factory - @Query("select * from searchBooks where bookUrl = :bookUrl") fun getSearchBook(bookUrl: String): SearchBook? diff --git a/app/src/main/java/io/legado/app/ui/book/toc/BookmarkAdapter.kt b/app/src/main/java/io/legado/app/ui/book/toc/BookmarkAdapter.kt index 8300aa3da..73fe4fe93 100644 --- a/app/src/main/java/io/legado/app/ui/book/toc/BookmarkAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/toc/BookmarkAdapter.kt @@ -1,54 +1,38 @@ package io.legado.app.ui.book.toc -import android.view.LayoutInflater +import android.content.Context import android.view.ViewGroup -import androidx.paging.PagedListAdapter -import androidx.recyclerview.widget.DiffUtil -import androidx.recyclerview.widget.RecyclerView +import io.legado.app.base.adapter.ItemViewHolder +import io.legado.app.base.adapter.RecyclerAdapter import io.legado.app.data.entities.Bookmark import io.legado.app.databinding.ItemBookmarkBinding import splitties.views.onLongClick -class BookmarkAdapter(val callback: Callback) : PagedListAdapter(DIFF_CALLBACK) { +class BookmarkAdapter(context: Context, val callback: Callback) : + RecyclerAdapter(context) { - companion object { - - @JvmField - val DIFF_CALLBACK = object : DiffUtil.ItemCallback() { - override fun areItemsTheSame(oldItem: Bookmark, newItem: Bookmark): Boolean = - oldItem.time == newItem.time - - override fun areContentsTheSame(oldItem: Bookmark, newItem: Bookmark): Boolean = - oldItem.time == newItem.time - && oldItem.bookUrl == newItem.bookUrl - && oldItem.chapterName == newItem.chapterName - && oldItem.content == newItem.content - } - } - - override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder { - val binding = - ItemBookmarkBinding.inflate(LayoutInflater.from(parent.context), parent, false) - return MyViewHolder(binding) + override fun getViewBinding(parent: ViewGroup): ItemBookmarkBinding { + return ItemBookmarkBinding.inflate(inflater, parent, false) } - override fun onBindViewHolder(holder: MyViewHolder, position: Int) { - getItem(position)?.let { - holder.bind(it, callback) - } + override fun convert( + holder: ItemViewHolder, + binding: ItemBookmarkBinding, + item: Bookmark, + payloads: MutableList + ) { + binding.tvChapterName.text = item.chapterName + binding.tvBookText.text = item.bookText + binding.tvContent.text = item.content } - class MyViewHolder(val binding: ItemBookmarkBinding) : RecyclerView.ViewHolder(binding.root) { - - fun bind(bookmark: Bookmark, callback: Callback?) = with(binding) { - tvChapterName.text = bookmark.chapterName - tvBookText.text = bookmark.bookText - tvContent.text = bookmark.content - itemView.setOnClickListener { - callback?.onClick(bookmark) + override fun registerListener(holder: ItemViewHolder, binding: ItemBookmarkBinding) { + getItem(holder.layoutPosition)?.let { bookmark -> + binding.root.setOnClickListener { + callback.onClick(bookmark) } - itemView.onLongClick { - callback?.onLongClick(bookmark) + binding.root.onLongClick { + callback.onLongClick(bookmark) } } } @@ -57,4 +41,5 @@ class BookmarkAdapter(val callback: Callback) : PagedListAdapter(R.layout.fragment_ override val viewModel: ChapterListViewModel by activityViewModels() private val binding by viewBinding(FragmentBookmarkBinding::bind) private lateinit var adapter: BookmarkAdapter - private var bookmarkLiveData: LiveData>? = null + private var bookmarkLiveData: LiveData>? = null override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { viewModel.bookMarkCallBack = this @@ -43,7 +41,7 @@ class BookmarkFragment : VMBaseFragment(R.layout.fragment_ private fun initRecyclerView() { ATH.applyEdgeEffectColor(binding.recyclerView) - adapter = BookmarkAdapter(this) + adapter = BookmarkAdapter(requireContext(), this) binding.recyclerView.layoutManager = LinearLayoutManager(requireContext()) binding.recyclerView.addItemDecoration(VerticalDivider(requireContext())) binding.recyclerView.adapter = adapter @@ -51,11 +49,8 @@ class BookmarkFragment : VMBaseFragment(R.layout.fragment_ private fun initData(book: Book) { bookmarkLiveData?.removeObservers(viewLifecycleOwner) - bookmarkLiveData = - LivePagedListBuilder( - appDb.bookmarkDao.observeByBook(book.bookUrl, book.name, book.author), 20 - ).build() - bookmarkLiveData?.observe(viewLifecycleOwner, { adapter.submitList(it) }) + bookmarkLiveData = appDb.bookmarkDao.observeByBook(book.bookUrl, book.name, book.author) + bookmarkLiveData?.observe(viewLifecycleOwner, { adapter.setItems(it) }) } override fun startBookmarkSearch(newText: String?) { @@ -65,13 +60,8 @@ class BookmarkFragment : VMBaseFragment(R.layout.fragment_ } } else { bookmarkLiveData?.removeObservers(viewLifecycleOwner) - bookmarkLiveData = LivePagedListBuilder( - appDb.bookmarkDao.liveDataSearch( - viewModel.bookUrl, - newText - ), 20 - ).build() - bookmarkLiveData?.observe(viewLifecycleOwner, { adapter.submitList(it) }) + bookmarkLiveData = appDb.bookmarkDao.liveDataSearch(viewModel.bookUrl, newText) + bookmarkLiveData?.observe(viewLifecycleOwner, { adapter.setItems(it) }) } }