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 0041591e3..24e632b0d 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,20 +1,20 @@ package io.legado.app.data.dao -import androidx.lifecycle.LiveData import androidx.room.Dao import androidx.room.Insert import androidx.room.OnConflictStrategy import androidx.room.Query import io.legado.app.data.entities.BookChapter +import kotlinx.coroutines.flow.Flow @Dao interface BookChapterDao { @Query("select * from chapters where bookUrl = :bookUrl order by `index`") - fun observeByBook(bookUrl: String): LiveData> + fun observeByBook(bookUrl: String): Flow> @Query("SELECT * FROM chapters where bookUrl = :bookUrl and title like '%'||:key||'%' order by `index`") - fun liveDataSearch(bookUrl: String, key: String): LiveData> + fun liveDataSearch(bookUrl: String, key: String): Flow> @Query("select * from chapters where bookUrl = :bookUrl order by `index`") fun getChapterList(bookUrl: 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 4ce8b05bb..052884126 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,8 +1,8 @@ package io.legado.app.data.dao -import androidx.lifecycle.LiveData import androidx.room.* import io.legado.app.data.entities.Bookmark +import kotlinx.coroutines.flow.Flow @Dao @@ -15,7 +15,7 @@ interface BookmarkDao { fun observeByBook( bookName: String, bookAuthor: String - ): LiveData> + ): Flow> @Query( """ @@ -27,7 +27,7 @@ interface BookmarkDao { bookName: String, bookAuthor: String, key: String - ): LiveData> + ): Flow> @Insert(onConflict = OnConflictStrategy.REPLACE) fun insert(vararg bookmark: Bookmark) diff --git a/app/src/main/java/io/legado/app/ui/book/toc/BookmarkFragment.kt b/app/src/main/java/io/legado/app/ui/book/toc/BookmarkFragment.kt index bac6fc8d2..a0933af30 100644 --- a/app/src/main/java/io/legado/app/ui/book/toc/BookmarkFragment.kt +++ b/app/src/main/java/io/legado/app/ui/book/toc/BookmarkFragment.kt @@ -5,12 +5,10 @@ import android.content.Intent import android.os.Bundle import android.view.View import androidx.fragment.app.activityViewModels -import androidx.lifecycle.LiveData import androidx.recyclerview.widget.LinearLayoutManager import io.legado.app.R import io.legado.app.base.VMBaseFragment import io.legado.app.data.appDb -import io.legado.app.data.entities.Book import io.legado.app.data.entities.Bookmark import io.legado.app.databinding.DialogBookmarkBinding import io.legado.app.databinding.FragmentBookmarkBinding @@ -20,6 +18,9 @@ import io.legado.app.lib.theme.ATH import io.legado.app.ui.widget.recycler.VerticalDivider import io.legado.app.utils.requestInputMethod import io.legado.app.utils.viewbindingdelegate.viewBinding +import kotlinx.coroutines.Job +import kotlinx.coroutines.flow.collect +import kotlinx.coroutines.launch class BookmarkFragment : VMBaseFragment(R.layout.fragment_bookmark), @@ -28,13 +29,13 @@ class BookmarkFragment : VMBaseFragment(R.layout.fragment_bookmark override val viewModel by activityViewModels() private val binding by viewBinding(FragmentBookmarkBinding::bind) private lateinit var adapter: BookmarkAdapter - private var bookmarkLiveData: LiveData>? = null + private var bookmarkFlowJob: Job? = null override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { viewModel.bookMarkCallBack = this initRecyclerView() viewModel.bookData.observe(this) { - initData(it) + upBookmark(null) } } @@ -46,20 +47,15 @@ class BookmarkFragment : VMBaseFragment(R.layout.fragment_bookmark binding.recyclerView.adapter = adapter } - private fun initData(book: Book) { - bookmarkLiveData?.removeObservers(viewLifecycleOwner) - bookmarkLiveData = appDb.bookmarkDao.observeByBook(book.name, book.author) - bookmarkLiveData?.observe(viewLifecycleOwner, { adapter.setItems(it) }) - } - - override fun startBookmarkSearch(newText: String?) { - viewModel.bookData.value?.let { book -> - if (newText.isNullOrBlank()) { - initData(book) - } else { - bookmarkLiveData?.removeObservers(viewLifecycleOwner) - bookmarkLiveData = appDb.bookmarkDao.liveDataSearch(book.name, book.author, newText) - bookmarkLiveData?.observe(viewLifecycleOwner, { adapter.setItems(it) }) + override fun upBookmark(searchKey: String?) { + val book = viewModel.bookData.value ?: return + bookmarkFlowJob?.cancel() + bookmarkFlowJob = launch { + when { + searchKey.isNullOrBlank() -> appDb.bookmarkDao.observeByBook(book.name, book.author) + else -> appDb.bookmarkDao.liveDataSearch(book.name, book.author, searchKey) + }.collect { + adapter.setItems(it) } } } diff --git a/app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt b/app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt index 417b740ca..feef7aa82 100644 --- a/app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt +++ b/app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt @@ -6,7 +6,6 @@ import android.content.Intent import android.os.Bundle import android.view.View import androidx.fragment.app.activityViewModels -import androidx.lifecycle.LiveData import io.legado.app.R import io.legado.app.base.VMBaseFragment import io.legado.app.constant.EventBus @@ -24,6 +23,8 @@ import io.legado.app.utils.observeEvent import io.legado.app.utils.viewbindingdelegate.viewBinding import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.Main +import kotlinx.coroutines.Job +import kotlinx.coroutines.flow.collect import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import kotlin.math.min @@ -36,7 +37,7 @@ class ChapterListFragment : VMBaseFragment(R.layout.fragment_chapt lateinit var adapter: ChapterListAdapter private var durChapterIndex = 0 private lateinit var mLayoutManager: UpLinearLayoutManager - private var tocLiveData: LiveData>? = null + private var tocFlowJob: Job? = null private var scrollToDurChapter = false override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) = binding.run { @@ -77,7 +78,7 @@ class ChapterListFragment : VMBaseFragment(R.layout.fragment_chapt @SuppressLint("SetTextI18n") private fun initBook(book: Book) { launch { - initToc() + upChapterList(null) durChapterIndex = book.durChapterIndex binding.tvCurrentChapterInfo.text = "${book.durChapterTitle}(${book.durChapterIndex + 1}/${book.totalChapterNum})" @@ -85,18 +86,6 @@ class ChapterListFragment : VMBaseFragment(R.layout.fragment_chapt } } - private fun initToc() { - tocLiveData?.removeObservers(this@ChapterListFragment) - tocLiveData = appDb.bookChapterDao.observeByBook(viewModel.bookUrl) - tocLiveData?.observe(viewLifecycleOwner, { - adapter.setItems(it) - if (!scrollToDurChapter) { - mLayoutManager.scrollToPositionWithOffset(durChapterIndex, 0) - scrollToDurChapter = true - } - }) - } - private fun initCacheFileNames(book: Book) { launch(IO) { adapter.cacheFileNames.addAll(BookHelp.getChapterFiles(book)) @@ -117,15 +106,19 @@ class ChapterListFragment : VMBaseFragment(R.layout.fragment_chapt } } - override fun startChapterListSearch(newText: String?) { - if (newText.isNullOrBlank()) { - initToc() - } else { - tocLiveData?.removeObservers(this) - tocLiveData = appDb.bookChapterDao.liveDataSearch(viewModel.bookUrl, newText) - tocLiveData?.observe(viewLifecycleOwner, { + override fun upChapterList(searchKey: String?) { + tocFlowJob?.cancel() + tocFlowJob = launch { + when { + searchKey.isNullOrBlank() -> appDb.bookChapterDao.observeByBook(viewModel.bookUrl) + else -> appDb.bookChapterDao.liveDataSearch(viewModel.bookUrl, searchKey) + }.collect { adapter.setItems(it) - }) + if (searchKey.isNullOrBlank() && !scrollToDurChapter) { + mLayoutManager.scrollToPositionWithOffset(durChapterIndex, 0) + scrollToDurChapter = true + } + } } } diff --git a/app/src/main/java/io/legado/app/ui/book/toc/TocViewModel.kt b/app/src/main/java/io/legado/app/ui/book/toc/TocViewModel.kt index 992101490..1fdb66fd6 100644 --- a/app/src/main/java/io/legado/app/ui/book/toc/TocViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/toc/TocViewModel.kt @@ -39,18 +39,18 @@ class TocViewModel(application: Application) : BaseViewModel(application) { } fun startChapterListSearch(newText: String?) { - chapterCallBack?.startChapterListSearch(newText) + chapterCallBack?.upChapterList(newText) } fun startBookmarkSearch(newText: String?) { - bookMarkCallBack?.startBookmarkSearch(newText) + bookMarkCallBack?.upBookmark(newText) } interface ChapterListCallBack { - fun startChapterListSearch(newText: String?) + fun upChapterList(searchKey: String?) } interface BookmarkCallBack { - fun startBookmarkSearch(newText: String?) + fun upBookmark(searchKey: String?) } } \ No newline at end of file