pull/1155/head
gedoor 3 years ago
parent 1fba49fa48
commit 91bdedf0c0
  1. 6
      app/src/main/java/io/legado/app/data/dao/BookChapterDao.kt
  2. 6
      app/src/main/java/io/legado/app/data/dao/BookmarkDao.kt
  3. 32
      app/src/main/java/io/legado/app/ui/book/toc/BookmarkFragment.kt
  4. 39
      app/src/main/java/io/legado/app/ui/book/toc/ChapterListFragment.kt
  5. 8
      app/src/main/java/io/legado/app/ui/book/toc/TocViewModel.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<List<BookChapter>>
fun observeByBook(bookUrl: String): Flow<List<BookChapter>>
@Query("SELECT * FROM chapters where bookUrl = :bookUrl and title like '%'||:key||'%' order by `index`")
fun liveDataSearch(bookUrl: String, key: String): LiveData<List<BookChapter>>
fun liveDataSearch(bookUrl: String, key: String): Flow<List<BookChapter>>
@Query("select * from chapters where bookUrl = :bookUrl order by `index`")
fun getChapterList(bookUrl: String): List<BookChapter>

@ -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<List<Bookmark>>
): Flow<List<Bookmark>>
@Query(
"""
@ -27,7 +27,7 @@ interface BookmarkDao {
bookName: String,
bookAuthor: String,
key: String
): LiveData<List<Bookmark>>
): Flow<List<Bookmark>>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(vararg bookmark: Bookmark)

@ -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<TocViewModel>(R.layout.fragment_bookmark),
@ -28,13 +29,13 @@ class BookmarkFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_bookmark
override val viewModel by activityViewModels<TocViewModel>()
private val binding by viewBinding(FragmentBookmarkBinding::bind)
private lateinit var adapter: BookmarkAdapter
private var bookmarkLiveData: LiveData<List<Bookmark>>? = 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<TocViewModel>(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)
}
}
}

@ -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<TocViewModel>(R.layout.fragment_chapt
lateinit var adapter: ChapterListAdapter
private var durChapterIndex = 0
private lateinit var mLayoutManager: UpLinearLayoutManager
private var tocLiveData: LiveData<List<BookChapter>>? = 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<TocViewModel>(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<TocViewModel>(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<TocViewModel>(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
}
}
}
}

@ -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?)
}
}
Loading…
Cancel
Save