优化书籍目录逻辑并提高模糊搜索准确性

pull/72/head
Administrator 5 years ago
parent 7da314fb7f
commit 403a89e5c4
  1. 4
      app/src/main/java/io/legado/app/data/dao/BookChapterDao.kt
  2. 12
      app/src/main/java/io/legado/app/ui/chapterlist/ChapterListFragment.kt

@ -13,8 +13,8 @@ interface BookChapterDao {
@Query("select * from chapters where bookUrl = :bookUrl") @Query("select * from chapters where bookUrl = :bookUrl")
fun observeByBook(bookUrl: String): LiveData<List<BookChapter>> fun observeByBook(bookUrl: String): LiveData<List<BookChapter>>
@Query("SELECT * FROM chapters where title like '%'||:key||'%'") @Query("SELECT * FROM chapters where bookUrl = :bookUrl and title like '%'||:key||'%'")
fun liveDataSearch(key: String): LiveData<List<BookChapter>> fun liveDataSearch(bookUrl: String, key: String): LiveData<List<BookChapter>>
@Query("select * from chapters where bookUrl = :bookUrl") @Query("select * from chapters where bookUrl = :bookUrl")
fun getChapterList(bookUrl: String): List<BookChapter> fun getChapterList(bookUrl: String): List<BookChapter>

@ -5,7 +5,6 @@ import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.view.View import android.view.View
import android.widget.LinearLayout import android.widget.LinearLayout
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.DividerItemDecoration
import io.legado.app.App import io.legado.app.App
@ -26,7 +25,6 @@ class ChapterListFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragme
get() = getViewModelOfActivity(ChapterListViewModel::class.java) get() = getViewModelOfActivity(ChapterListViewModel::class.java)
lateinit var adapter: ChapterListAdapter lateinit var adapter: ChapterListAdapter
private var chapterData: LiveData<List<BookChapter>>? = null
private var durChapterIndex = 0 private var durChapterIndex = 0
private lateinit var mLayoutManager: UpLinearLayoutManager private lateinit var mLayoutManager: UpLinearLayoutManager
@ -56,7 +54,7 @@ class ChapterListFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragme
adapter.setItems(it) adapter.setItems(it)
viewModel.book?.let { book -> viewModel.book?.let { book ->
durChapterIndex = book.durChapterIndex durChapterIndex = book.durChapterIndex
tv_current_chapter_info.text = book.durChapterTitle tv_current_chapter_info.text = it[durChapterIndex()].title
mLayoutManager.scrollToPositionWithOffset(durChapterIndex, 0) mLayoutManager.scrollToPositionWithOffset(durChapterIndex, 0)
} }
}) })
@ -80,15 +78,15 @@ class ChapterListFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragme
} }
override fun startSearch(newText: String?) { override fun startSearch(newText: String?) {
chapterData?.removeObservers(this)
if (newText.isNullOrBlank()) { if (newText.isNullOrBlank()) {
initData() initData()
} else { } else {
chapterData = App.db.bookChapterDao().liveDataSearch(newText) viewModel.bookUrl?.let { bookUrl ->
chapterData?.observe(viewLifecycleOwner, Observer { App.db.bookChapterDao().liveDataSearch(bookUrl, newText).observe(viewLifecycleOwner, Observer {
adapter.setItems(it) adapter.setItems(it)
})
mLayoutManager.scrollToPositionWithOffset(0, 0) mLayoutManager.scrollToPositionWithOffset(0, 0)
})
}
} }
} }

Loading…
Cancel
Save