pull/32/head
kunfei 5 years ago
parent 7ced66282e
commit b2c00449be
  1. 12
      app/src/main/java/io/legado/app/data/dao/SearchBookDao.kt
  2. 15
      app/src/main/java/io/legado/app/ui/search/SearchActivity.kt
  3. 9
      app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt

@ -17,8 +17,16 @@ interface SearchBookDao {
@Query("SELECT * FROM searchBooks where time >= :time")
fun observeNew(time: Long): DataSource.Factory<Int, SearchBook>
@Query("SELECT name, author, min(time) time, max(kind) kind, max(coverUrl) coverUrl, max(intro) intro, max(wordCount) wordCount, max(latestChapterTitle) latestChapterTitle, count(origin) originCount FROM searchBooks where time >= :time group by name, author")
fun observeShow(time: Long): DataSource.Factory<Int, SearchShow>
@Query(
"""
SELECT name, author, min(time) time, max(kind) kind, max(coverUrl) coverUrl,max(intro) intro, max(wordCount) wordCount,
max(latestChapterTitle) latestChapterTitle, count(origin) originCount
FROM searchBooks where time >= :time
group by name, author
order by case when name = :key then 1 when author = :key then 2 when name like '%'+:key+'%' then 3 when author like '%'+:key+'%' then 4 else 5 end, time
"""
)
fun observeShow(key: String, time: Long): DataSource.Factory<Int, SearchShow>
@Query("select * from searchBooks where bookUrl = :bookUrl")
fun getSearchBook(bookUrl: String): SearchBook?

@ -31,7 +31,7 @@ class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_search)
override fun onActivityCreated(savedInstanceState: Bundle?) {
initRecyclerView()
initSearchView()
initData(0L)
initData()
intent.getStringExtra("key")?.let {
search_view.setQuery(it, true)
}
@ -47,9 +47,9 @@ class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_search)
override fun onQueryTextSubmit(query: String?): Boolean {
search_view.clearFocus()
query?.let {
viewModel.search(it, { startTime ->
viewModel.search(it, {
content_view.showContentView()
initData(startTime)
initData()
}, {
})
@ -74,9 +74,14 @@ class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_search)
rv_search_list.adapter = adapter
}
private fun initData(startTime: Long) {
private fun initData() {
searchBookData?.removeObservers(this)
searchBookData = LivePagedListBuilder(App.db.searchBookDao().observeShow(startTime), 30).build()
searchBookData = LivePagedListBuilder(
App.db.searchBookDao().observeShow(
viewModel.searchKey,
viewModel.startTime
), 30
).build()
searchBookData?.observe(this, Observer { adapter.submitList(it) })
}

@ -11,17 +11,20 @@ import kotlinx.coroutines.delay
class SearchViewModel(application: Application) : BaseViewModel(application) {
private var task: Coroutine<*>? = null
var searchKey: String = ""
var startTime: Long = 0
var searchPage = 0
fun search(
key: String,
start: ((startTime: Long) -> Unit)? = null,
start: (() -> Unit)? = null,
finally: (() -> Unit)? = null
) {
if (key.isEmpty()) return
task?.cancel()
start?.invoke(System.currentTimeMillis())
searchKey = key
startTime = System.currentTimeMillis()
start?.invoke()
task = execute {
//onCleared时自动取消
val bookSourceList = App.db.bookSourceDao().allEnabled

Loading…
Cancel
Save