pull/981/head
gedoor 4 years ago
parent bf2813f9e3
commit 6c3f5aaff3
  1. 4
      app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt
  2. 6
      app/src/main/java/io/legado/app/data/dao/BookmarkDao.kt
  3. 7
      app/src/main/java/io/legado/app/data/dao/SearchBookDao.kt
  4. 61
      app/src/main/java/io/legado/app/ui/book/toc/BookmarkAdapter.kt
  5. 22
      app/src/main/java/io/legado/app/ui/book/toc/BookmarkFragment.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<List<String>>
@Query("select * from book_sources where enabledExplore = 1 order by customOrder asc")
fun observeFind(): DataSource.Factory<Int, BookSource>
@Query("select * from book_sources where bookSourceGroup like '%' || :group || '%'")
fun getByGroup(group: String): List<BookSource>

@ -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<Int, Bookmark>
): LiveData<List<Bookmark>>
@Query("SELECT * FROM bookmarks where bookUrl = :bookUrl and chapterName like '%'||:key||'%' or content like '%'||:key||'%'")
fun liveDataSearch(bookUrl: String, key: String): DataSource.Factory<Int, Bookmark>
fun liveDataSearch(bookUrl: String, key: String): LiveData<List<Bookmark>>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(vararg bookmark: Bookmark)

@ -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<Int, SearchBook>
@Query("SELECT * FROM searchBooks where time >= :time")
fun observeNew(time: Long): DataSource.Factory<Int, SearchBook>
@Query("select * from searchBooks where bookUrl = :bookUrl")
fun getSearchBook(bookUrl: String): SearchBook?

@ -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<Bookmark, BookmarkAdapter.MyViewHolder>(DIFF_CALLBACK) {
class BookmarkAdapter(context: Context, val callback: Callback) :
RecyclerAdapter<Bookmark, ItemBookmarkBinding>(context) {
companion object {
@JvmField
val DIFF_CALLBACK = object : DiffUtil.ItemCallback<Bookmark>() {
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<Any>
) {
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<Bookmark, Bookm
fun onClick(bookmark: Bookmark)
fun onLongClick(bookmark: Bookmark)
}
}

@ -7,8 +7,6 @@ import android.os.Bundle
import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.LiveData
import androidx.paging.LivePagedListBuilder
import androidx.paging.PagedList
import androidx.recyclerview.widget.LinearLayoutManager
import io.legado.app.R
import io.legado.app.base.VMBaseFragment
@ -31,7 +29,7 @@ class BookmarkFragment : VMBaseFragment<ChapterListViewModel>(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<PagedList<Bookmark>>? = null
private var bookmarkLiveData: LiveData<List<Bookmark>>? = null
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
viewModel.bookMarkCallBack = this
@ -43,7 +41,7 @@ class BookmarkFragment : VMBaseFragment<ChapterListViewModel>(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<ChapterListViewModel>(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<ChapterListViewModel>(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) })
}
}

Loading…
Cancel
Save