使书籍目录能够模糊搜索

pull/68/head
Administrator 5 years ago
parent c08db668ac
commit 71bb578cdf
  1. 3
      app/src/main/java/io/legado/app/data/dao/BookChapterDao.kt
  2. 6
      app/src/main/java/io/legado/app/ui/chapterlist/ChapterListActivity.kt
  3. 19
      app/src/main/java/io/legado/app/ui/chapterlist/ChapterListFragment.kt
  4. 9
      app/src/main/java/io/legado/app/ui/chapterlist/ChapterListViewModel.kt

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

@ -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<ChapterListViewModel>(R.layout.activity_chapter_list) {
override val viewModel: ChapterListViewModel
get() = getViewModel(ChapterListViewModel::class.java)
@ -52,6 +53,11 @@ class ChapterListActivity : VMBaseActivity<ChapterListViewModel>(R.layout.activi
}
override fun onQueryTextChange(newText: String): Boolean {
if (tab_layout.selectedTabPosition == 1) {
} else {
viewModel.startChapterListSearch(newText)
}
return false
}
})

@ -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<ChapterListViewModel>(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<List<BookChapter>>? = null
private var durChapterIndex = 0
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
@ -71,6 +74,20 @@ class ChapterListFragment : VMBaseFragment<ChapterListViewModel>(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 {

@ -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?)
}
}
Loading…
Cancel
Save