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 package io.legado.app.data.dao
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.paging.DataSource
import androidx.room.* import androidx.room.*
import io.legado.app.data.entities.BookSource 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) <> ''") @Query("select distinct bookSourceGroup from book_sources where enabledExplore = 1 and trim(exploreUrl) <> '' and trim(bookSourceGroup) <> ''")
fun liveExploreGroup(): LiveData<List<String>> 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 || '%'") @Query("select * from book_sources where bookSourceGroup like '%' || :group || '%'")
fun getByGroup(group: String): List<BookSource> fun getByGroup(group: String): List<BookSource>

@ -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.* import androidx.room.*
import io.legado.app.data.entities.Bookmark import io.legado.app.data.entities.Bookmark
@ -16,10 +16,10 @@ interface BookmarkDao {
bookUrl: String, bookUrl: String,
bookName: String, bookName: String,
bookAuthor: 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||'%'") @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) @Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(vararg bookmark: Bookmark) fun insert(vararg bookmark: Bookmark)

@ -1,6 +1,5 @@
package io.legado.app.data.dao package io.legado.app.data.dao
import androidx.paging.DataSource
import androidx.room.Dao import androidx.room.Dao
import androidx.room.Insert import androidx.room.Insert
import androidx.room.OnConflictStrategy import androidx.room.OnConflictStrategy
@ -10,12 +9,6 @@ import io.legado.app.data.entities.SearchBook
@Dao @Dao
interface SearchBookDao { 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") @Query("select * from searchBooks where bookUrl = :bookUrl")
fun getSearchBook(bookUrl: String): SearchBook? fun getSearchBook(bookUrl: String): SearchBook?

@ -1,54 +1,38 @@
package io.legado.app.ui.book.toc package io.legado.app.ui.book.toc
import android.view.LayoutInflater import android.content.Context
import android.view.ViewGroup import android.view.ViewGroup
import androidx.paging.PagedListAdapter import io.legado.app.base.adapter.ItemViewHolder
import androidx.recyclerview.widget.DiffUtil import io.legado.app.base.adapter.RecyclerAdapter
import androidx.recyclerview.widget.RecyclerView
import io.legado.app.data.entities.Bookmark import io.legado.app.data.entities.Bookmark
import io.legado.app.databinding.ItemBookmarkBinding import io.legado.app.databinding.ItemBookmarkBinding
import splitties.views.onLongClick 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 { override fun getViewBinding(parent: ViewGroup): ItemBookmarkBinding {
return ItemBookmarkBinding.inflate(inflater, parent, false)
@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 onBindViewHolder(holder: MyViewHolder, position: Int) { override fun convert(
getItem(position)?.let { holder: ItemViewHolder,
holder.bind(it, callback) 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) { override fun registerListener(holder: ItemViewHolder, binding: ItemBookmarkBinding) {
getItem(holder.layoutPosition)?.let { bookmark ->
fun bind(bookmark: Bookmark, callback: Callback?) = with(binding) { binding.root.setOnClickListener {
tvChapterName.text = bookmark.chapterName callback.onClick(bookmark)
tvBookText.text = bookmark.bookText
tvContent.text = bookmark.content
itemView.setOnClickListener {
callback?.onClick(bookmark)
} }
itemView.onLongClick { binding.root.onLongClick {
callback?.onLongClick(bookmark) callback.onLongClick(bookmark)
} }
} }
} }
@ -57,4 +41,5 @@ class BookmarkAdapter(val callback: Callback) : PagedListAdapter<Bookmark, Bookm
fun onClick(bookmark: Bookmark) fun onClick(bookmark: Bookmark)
fun onLongClick(bookmark: Bookmark) fun onLongClick(bookmark: Bookmark)
} }
} }

@ -7,8 +7,6 @@ import android.os.Bundle
import android.view.View import android.view.View
import androidx.fragment.app.activityViewModels import androidx.fragment.app.activityViewModels
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.paging.LivePagedListBuilder
import androidx.paging.PagedList
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.VMBaseFragment import io.legado.app.base.VMBaseFragment
@ -31,7 +29,7 @@ class BookmarkFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragment_
override val viewModel: ChapterListViewModel by activityViewModels() override val viewModel: ChapterListViewModel by activityViewModels()
private val binding by viewBinding(FragmentBookmarkBinding::bind) private val binding by viewBinding(FragmentBookmarkBinding::bind)
private lateinit var adapter: BookmarkAdapter 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?) { override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
viewModel.bookMarkCallBack = this viewModel.bookMarkCallBack = this
@ -43,7 +41,7 @@ class BookmarkFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragment_
private fun initRecyclerView() { private fun initRecyclerView() {
ATH.applyEdgeEffectColor(binding.recyclerView) ATH.applyEdgeEffectColor(binding.recyclerView)
adapter = BookmarkAdapter(this) adapter = BookmarkAdapter(requireContext(), this)
binding.recyclerView.layoutManager = LinearLayoutManager(requireContext()) binding.recyclerView.layoutManager = LinearLayoutManager(requireContext())
binding.recyclerView.addItemDecoration(VerticalDivider(requireContext())) binding.recyclerView.addItemDecoration(VerticalDivider(requireContext()))
binding.recyclerView.adapter = adapter binding.recyclerView.adapter = adapter
@ -51,11 +49,8 @@ class BookmarkFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragment_
private fun initData(book: Book) { private fun initData(book: Book) {
bookmarkLiveData?.removeObservers(viewLifecycleOwner) bookmarkLiveData?.removeObservers(viewLifecycleOwner)
bookmarkLiveData = bookmarkLiveData = appDb.bookmarkDao.observeByBook(book.bookUrl, book.name, book.author)
LivePagedListBuilder( bookmarkLiveData?.observe(viewLifecycleOwner, { adapter.setItems(it) })
appDb.bookmarkDao.observeByBook(book.bookUrl, book.name, book.author), 20
).build()
bookmarkLiveData?.observe(viewLifecycleOwner, { adapter.submitList(it) })
} }
override fun startBookmarkSearch(newText: String?) { override fun startBookmarkSearch(newText: String?) {
@ -65,13 +60,8 @@ class BookmarkFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragment_
} }
} else { } else {
bookmarkLiveData?.removeObservers(viewLifecycleOwner) bookmarkLiveData?.removeObservers(viewLifecycleOwner)
bookmarkLiveData = LivePagedListBuilder( bookmarkLiveData = appDb.bookmarkDao.liveDataSearch(viewModel.bookUrl, newText)
appDb.bookmarkDao.liveDataSearch( bookmarkLiveData?.observe(viewLifecycleOwner, { adapter.setItems(it) })
viewModel.bookUrl,
newText
), 20
).build()
bookmarkLiveData?.observe(viewLifecycleOwner, { adapter.submitList(it) })
} }
} }

Loading…
Cancel
Save