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