From 71bb578cdf6531badf5eeda41c0cfc4a6085fe46 Mon Sep 17 00:00:00 2001 From: Administrator Date: Tue, 7 Jan 2020 16:23:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E4=B9=A6=E7=B1=8D=E7=9B=AE=E5=BD=95?= =?UTF-8?q?=E8=83=BD=E5=A4=9F=E6=A8=A1=E7=B3=8A=E6=90=9C=E7=B4=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/data/dao/BookChapterDao.kt | 3 +++ .../app/ui/chapterlist/ChapterListActivity.kt | 6 ++++++ .../app/ui/chapterlist/ChapterListFragment.kt | 19 ++++++++++++++++++- .../ui/chapterlist/ChapterListViewModel.kt | 9 +++++++++ 4 files changed, 36 insertions(+), 1 deletion(-) 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 9a4db0cb1..a67d12f03 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,6 +13,9 @@ 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") fun getChapterList(bookUrl: String): List diff --git a/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListActivity.kt b/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListActivity.kt index a7876f8e4..1aab97a4b 100644 --- a/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListActivity.kt +++ b/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListActivity.kt @@ -18,6 +18,7 @@ import io.legado.app.utils.visible import kotlinx.android.synthetic.main.activity_chapter_list.* import kotlinx.android.synthetic.main.view_tab_layout.* + class ChapterListActivity : VMBaseActivity(R.layout.activity_chapter_list) { override val viewModel: ChapterListViewModel get() = getViewModel(ChapterListViewModel::class.java) @@ -52,6 +53,11 @@ class ChapterListActivity : VMBaseActivity(R.layout.activi } override fun onQueryTextChange(newText: String): Boolean { + if (tab_layout.selectedTabPosition == 1) { + + } else { + viewModel.startChapterListSearch(newText) + } return false } }) 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 c8860f021..509e514ba 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,6 +5,7 @@ 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 androidx.recyclerview.widget.LinearLayoutManager @@ -19,11 +20,13 @@ import kotlinx.android.synthetic.main.fragment_chapter_list.* import org.jetbrains.anko.sdk27.listeners.onClick class ChapterListFragment : VMBaseFragment(R.layout.fragment_chapter_list), - ChapterListAdapter.Callback { + ChapterListAdapter.Callback, + ChapterListViewModel.CallBack { override val viewModel: ChapterListViewModel get() = getViewModelOfActivity(ChapterListViewModel::class.java) lateinit var adapter: ChapterListAdapter + private var chapterData: LiveData>? = null private var durChapterIndex = 0 override fun onViewCreated(view: View, savedInstanceState: Bundle?) { @@ -71,6 +74,20 @@ class ChapterListFragment : VMBaseFragment(R.layout.fragme recycler_view.scrollToPosition(it.durChapterIndex) } } + viewModel.callBack = this + } + + 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) + }) + recycler_view.scrollToPosition(0) + } } override fun durChapterIndex(): Int { diff --git a/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListViewModel.kt b/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListViewModel.kt index d11aa73dc..abbc81b01 100644 --- a/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/chapterlist/ChapterListViewModel.kt @@ -9,6 +9,7 @@ import io.legado.app.data.entities.Book class ChapterListViewModel(application: Application) : BaseViewModel(application) { var bookUrl: String? = null var book: Book? = null + var callBack: CallBack? = null fun loadBook(success: () -> Unit) { execute { @@ -19,4 +20,12 @@ class ChapterListViewModel(application: Application) : BaseViewModel(application success() } } + + fun startChapterListSearch(newText: String?) { + callBack?.startSearch(newText) + } + + interface CallBack { + fun startSearch(newText: String?) + } } \ No newline at end of file