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 package io.legado.app.data.dao
import androidx.lifecycle.LiveData
import androidx.room.Dao import androidx.room.Dao
import androidx.room.Insert import androidx.room.Insert
import androidx.room.OnConflictStrategy import androidx.room.OnConflictStrategy
import androidx.room.Query import androidx.room.Query
import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookChapter
import kotlinx.coroutines.flow.Flow
@Dao @Dao
interface BookChapterDao { interface BookChapterDao {
@Query("select * from chapters where bookUrl = :bookUrl order by `index`") @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`") @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`") @Query("select * from chapters where bookUrl = :bookUrl order by `index`")
fun getChapterList(bookUrl: String): List<BookChapter> fun getChapterList(bookUrl: String): List<BookChapter>

@ -1,8 +1,8 @@
package io.legado.app.data.dao package io.legado.app.data.dao
import androidx.lifecycle.LiveData
import androidx.room.* import androidx.room.*
import io.legado.app.data.entities.Bookmark import io.legado.app.data.entities.Bookmark
import kotlinx.coroutines.flow.Flow
@Dao @Dao
@ -15,7 +15,7 @@ interface BookmarkDao {
fun observeByBook( fun observeByBook(
bookName: String, bookName: String,
bookAuthor: String bookAuthor: String
): LiveData<List<Bookmark>> ): Flow<List<Bookmark>>
@Query( @Query(
""" """
@ -27,7 +27,7 @@ interface BookmarkDao {
bookName: String, bookName: String,
bookAuthor: String, bookAuthor: String,
key: String key: String
): LiveData<List<Bookmark>> ): Flow<List<Bookmark>>
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(vararg bookmark: Bookmark) fun insert(vararg bookmark: Bookmark)

@ -5,12 +5,10 @@ import android.content.Intent
import android.os.Bundle 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.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
import io.legado.app.data.appDb import io.legado.app.data.appDb
import io.legado.app.data.entities.Book
import io.legado.app.data.entities.Bookmark import io.legado.app.data.entities.Bookmark
import io.legado.app.databinding.DialogBookmarkBinding import io.legado.app.databinding.DialogBookmarkBinding
import io.legado.app.databinding.FragmentBookmarkBinding 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.ui.widget.recycler.VerticalDivider
import io.legado.app.utils.requestInputMethod import io.legado.app.utils.requestInputMethod
import io.legado.app.utils.viewbindingdelegate.viewBinding 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), 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>() override val viewModel by activityViewModels<TocViewModel>()
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<List<Bookmark>>? = null private var bookmarkFlowJob: Job? = null
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
viewModel.bookMarkCallBack = this viewModel.bookMarkCallBack = this
initRecyclerView() initRecyclerView()
viewModel.bookData.observe(this) { viewModel.bookData.observe(this) {
initData(it) upBookmark(null)
} }
} }
@ -46,20 +47,15 @@ class BookmarkFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_bookmark
binding.recyclerView.adapter = adapter binding.recyclerView.adapter = adapter
} }
private fun initData(book: Book) { override fun upBookmark(searchKey: String?) {
bookmarkLiveData?.removeObservers(viewLifecycleOwner) val book = viewModel.bookData.value ?: return
bookmarkLiveData = appDb.bookmarkDao.observeByBook(book.name, book.author) bookmarkFlowJob?.cancel()
bookmarkLiveData?.observe(viewLifecycleOwner, { adapter.setItems(it) }) bookmarkFlowJob = launch {
} when {
searchKey.isNullOrBlank() -> appDb.bookmarkDao.observeByBook(book.name, book.author)
override fun startBookmarkSearch(newText: String?) { else -> appDb.bookmarkDao.liveDataSearch(book.name, book.author, searchKey)
viewModel.bookData.value?.let { book -> }.collect {
if (newText.isNullOrBlank()) { adapter.setItems(it)
initData(book)
} else {
bookmarkLiveData?.removeObservers(viewLifecycleOwner)
bookmarkLiveData = appDb.bookmarkDao.liveDataSearch(book.name, book.author, newText)
bookmarkLiveData?.observe(viewLifecycleOwner, { adapter.setItems(it) })
} }
} }
} }

@ -6,7 +6,6 @@ import android.content.Intent
import android.os.Bundle 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 io.legado.app.R import io.legado.app.R
import io.legado.app.base.VMBaseFragment import io.legado.app.base.VMBaseFragment
import io.legado.app.constant.EventBus import io.legado.app.constant.EventBus
@ -24,6 +23,8 @@ import io.legado.app.utils.observeEvent
import io.legado.app.utils.viewbindingdelegate.viewBinding import io.legado.app.utils.viewbindingdelegate.viewBinding
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import kotlin.math.min import kotlin.math.min
@ -36,7 +37,7 @@ class ChapterListFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_chapt
lateinit var adapter: ChapterListAdapter lateinit var adapter: ChapterListAdapter
private var durChapterIndex = 0 private var durChapterIndex = 0
private lateinit var mLayoutManager: UpLinearLayoutManager private lateinit var mLayoutManager: UpLinearLayoutManager
private var tocLiveData: LiveData<List<BookChapter>>? = null private var tocFlowJob: Job? = null
private var scrollToDurChapter = false private var scrollToDurChapter = false
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) = binding.run { override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) = binding.run {
@ -77,7 +78,7 @@ class ChapterListFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_chapt
@SuppressLint("SetTextI18n") @SuppressLint("SetTextI18n")
private fun initBook(book: Book) { private fun initBook(book: Book) {
launch { launch {
initToc() upChapterList(null)
durChapterIndex = book.durChapterIndex durChapterIndex = book.durChapterIndex
binding.tvCurrentChapterInfo.text = binding.tvCurrentChapterInfo.text =
"${book.durChapterTitle}(${book.durChapterIndex + 1}/${book.totalChapterNum})" "${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) { private fun initCacheFileNames(book: Book) {
launch(IO) { launch(IO) {
adapter.cacheFileNames.addAll(BookHelp.getChapterFiles(book)) adapter.cacheFileNames.addAll(BookHelp.getChapterFiles(book))
@ -117,15 +106,19 @@ class ChapterListFragment : VMBaseFragment<TocViewModel>(R.layout.fragment_chapt
} }
} }
override fun startChapterListSearch(newText: String?) { override fun upChapterList(searchKey: String?) {
if (newText.isNullOrBlank()) { tocFlowJob?.cancel()
initToc() tocFlowJob = launch {
} else { when {
tocLiveData?.removeObservers(this) searchKey.isNullOrBlank() -> appDb.bookChapterDao.observeByBook(viewModel.bookUrl)
tocLiveData = appDb.bookChapterDao.liveDataSearch(viewModel.bookUrl, newText) else -> appDb.bookChapterDao.liveDataSearch(viewModel.bookUrl, searchKey)
tocLiveData?.observe(viewLifecycleOwner, { }.collect {
adapter.setItems(it) 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?) { fun startChapterListSearch(newText: String?) {
chapterCallBack?.startChapterListSearch(newText) chapterCallBack?.upChapterList(newText)
} }
fun startBookmarkSearch(newText: String?) { fun startBookmarkSearch(newText: String?) {
bookMarkCallBack?.startBookmarkSearch(newText) bookMarkCallBack?.upBookmark(newText)
} }
interface ChapterListCallBack { interface ChapterListCallBack {
fun startChapterListSearch(newText: String?) fun upChapterList(searchKey: String?)
} }
interface BookmarkCallBack { interface BookmarkCallBack {
fun startBookmarkSearch(newText: String?) fun upBookmark(searchKey: String?)
} }
} }
Loading…
Cancel
Save