From 403a89e5c4527c81dbd3c90eafe4b8a33a43a29f Mon Sep 17 00:00:00 2001 From: Administrator Date: Sun, 12 Jan 2020 17:36:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=B9=A6=E7=B1=8D=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=E9=80=BB=E8=BE=91=E5=B9=B6=E6=8F=90=E9=AB=98=E6=A8=A1?= =?UTF-8?q?=E7=B3=8A=E6=90=9C=E7=B4=A2=E5=87=86=E7=A1=AE=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/data/dao/BookChapterDao.kt | 4 ++-- .../app/ui/chapterlist/ChapterListFragment.kt | 16 +++++++--------- 2 files changed, 9 insertions(+), 11 deletions(-) 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 a67d12f03..9dda87c0e 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 @@ -13,8 +13,8 @@ interface BookChapterDao { @Query("select * from chapters where bookUrl = :bookUrl") fun observeByBook(bookUrl: String): LiveData> - @Query("SELECT * FROM chapters where title like '%'||:key||'%'") - fun liveDataSearch(key: String): LiveData> + @Query("SELECT * FROM chapters where bookUrl = :bookUrl and title like '%'||:key||'%'") + fun liveDataSearch(bookUrl: String, key: String): LiveData> @Query("select * from chapters where bookUrl = :bookUrl") fun getChapterList(bookUrl: String): List diff --git a/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListFragment.kt b/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListFragment.kt index 6853a1f07..bc002f56e 100644 --- a/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListFragment.kt +++ b/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListFragment.kt @@ -5,7 +5,6 @@ import android.content.Intent import android.os.Bundle import android.view.View import android.widget.LinearLayout -import androidx.lifecycle.LiveData import androidx.lifecycle.Observer import androidx.recyclerview.widget.DividerItemDecoration import io.legado.app.App @@ -26,7 +25,6 @@ class ChapterListFragment : VMBaseFragment(R.layout.fragme get() = getViewModelOfActivity(ChapterListViewModel::class.java) lateinit var adapter: ChapterListAdapter - private var chapterData: LiveData>? = null private var durChapterIndex = 0 private lateinit var mLayoutManager: UpLinearLayoutManager @@ -56,7 +54,7 @@ class ChapterListFragment : VMBaseFragment(R.layout.fragme adapter.setItems(it) viewModel.book?.let { book -> durChapterIndex = book.durChapterIndex - tv_current_chapter_info.text = book.durChapterTitle + tv_current_chapter_info.text = it[durChapterIndex()].title mLayoutManager.scrollToPositionWithOffset(durChapterIndex, 0) } }) @@ -80,15 +78,15 @@ class ChapterListFragment : VMBaseFragment(R.layout.fragme } override fun startSearch(newText: String?) { - chapterData?.removeObservers(this) if (newText.isNullOrBlank()) { initData() } else { - chapterData = App.db.bookChapterDao().liveDataSearch(newText) - chapterData?.observe(viewLifecycleOwner, Observer { - adapter.setItems(it) - }) - mLayoutManager.scrollToPositionWithOffset(0, 0) + viewModel.bookUrl?.let { bookUrl -> + App.db.bookChapterDao().liveDataSearch(bookUrl, newText).observe(viewLifecycleOwner, Observer { + adapter.setItems(it) + mLayoutManager.scrollToPositionWithOffset(0, 0) + }) + } } }