Merge pull request #68 from KKL369/master

使书籍目录能够模糊搜索
pull/69/head
kunfei 5 years ago committed by GitHub
commit 33a1a5ccef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  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") @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||'%'")
fun liveDataSearch(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>

@ -18,6 +18,7 @@ import io.legado.app.utils.visible
import kotlinx.android.synthetic.main.activity_chapter_list.* import kotlinx.android.synthetic.main.activity_chapter_list.*
import kotlinx.android.synthetic.main.view_tab_layout.* import kotlinx.android.synthetic.main.view_tab_layout.*
class ChapterListActivity : VMBaseActivity<ChapterListViewModel>(R.layout.activity_chapter_list) { class ChapterListActivity : VMBaseActivity<ChapterListViewModel>(R.layout.activity_chapter_list) {
override val viewModel: ChapterListViewModel override val viewModel: ChapterListViewModel
get() = getViewModel(ChapterListViewModel::class.java) get() = getViewModel(ChapterListViewModel::class.java)
@ -52,6 +53,11 @@ class ChapterListActivity : VMBaseActivity<ChapterListViewModel>(R.layout.activi
} }
override fun onQueryTextChange(newText: String): Boolean { override fun onQueryTextChange(newText: String): Boolean {
if (tab_layout.selectedTabPosition == 1) {
} else {
viewModel.startChapterListSearch(newText)
}
return false return false
} }
}) })

@ -5,6 +5,7 @@ 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 androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
@ -19,11 +20,13 @@ import kotlinx.android.synthetic.main.fragment_chapter_list.*
import org.jetbrains.anko.sdk27.listeners.onClick import org.jetbrains.anko.sdk27.listeners.onClick
class ChapterListFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragment_chapter_list), class ChapterListFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragment_chapter_list),
ChapterListAdapter.Callback { ChapterListAdapter.Callback,
ChapterListViewModel.CallBack {
override val viewModel: ChapterListViewModel override val viewModel: ChapterListViewModel
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
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
@ -71,6 +74,20 @@ class ChapterListFragment : VMBaseFragment<ChapterListViewModel>(R.layout.fragme
recycler_view.scrollToPosition(it.durChapterIndex) 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 { override fun durChapterIndex(): Int {

@ -9,6 +9,7 @@ import io.legado.app.data.entities.Book
class ChapterListViewModel(application: Application) : BaseViewModel(application) { class ChapterListViewModel(application: Application) : BaseViewModel(application) {
var bookUrl: String? = null var bookUrl: String? = null
var book: Book? = null var book: Book? = null
var callBack: CallBack? = null
fun loadBook(success: () -> Unit) { fun loadBook(success: () -> Unit) {
execute { execute {
@ -19,4 +20,12 @@ class ChapterListViewModel(application: Application) : BaseViewModel(application
success() success()
} }
} }
fun startChapterListSearch(newText: String?) {
callBack?.startSearch(newText)
}
interface CallBack {
fun startSearch(newText: String?)
}
} }
Loading…
Cancel
Save