diff --git a/app/src/main/java/io/legado/app/data/dao/SearchBookDao.kt b/app/src/main/java/io/legado/app/data/dao/SearchBookDao.kt index 75e6ae895..2032c46e8 100644 --- a/app/src/main/java/io/legado/app/data/dao/SearchBookDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/SearchBookDao.kt @@ -17,8 +17,16 @@ interface SearchBookDao { @Query("SELECT * FROM searchBooks where time >= :time") fun observeNew(time: Long): DataSource.Factory - @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 + @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 @Query("select * from searchBooks where bookUrl = :bookUrl") fun getSearchBook(bookUrl: String): SearchBook? diff --git a/app/src/main/java/io/legado/app/ui/search/SearchActivity.kt b/app/src/main/java/io/legado/app/ui/search/SearchActivity.kt index c37b81605..60478c0b2 100644 --- a/app/src/main/java/io/legado/app/ui/search/SearchActivity.kt +++ b/app/src/main/java/io/legado/app/ui/search/SearchActivity.kt @@ -31,7 +31,7 @@ class SearchActivity : VMBaseActivity(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(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(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) }) } diff --git a/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt b/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt index b05dca440..e8276db87 100644 --- a/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt @@ -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