diff --git a/app/src/main/java/io/legado/app/data/dao/BookDao.kt b/app/src/main/java/io/legado/app/data/dao/BookDao.kt index bae53c8f2..a174e0829 100644 --- a/app/src/main/java/io/legado/app/data/dao/BookDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/BookDao.kt @@ -24,6 +24,9 @@ interface BookDao { @Query("SELECT bookUrl FROM books WHERE `group` = :group") fun observeUrlsByGroup(group: Int): LiveData> + @Query("SELECT bookUrl FROM books WHERE name like '%' | :key | '%' or author like '%' | :key | '%'") + fun liveDataSearch(key: String): LiveData> + @Query("SELECT * FROM books WHERE `name` in (:names)") fun findByName(vararg names: String): List diff --git a/app/src/main/java/io/legado/app/data/dao/SearchKeywordDao.kt b/app/src/main/java/io/legado/app/data/dao/SearchKeywordDao.kt index b2370ceb6..fcd7130ef 100644 --- a/app/src/main/java/io/legado/app/data/dao/SearchKeywordDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/SearchKeywordDao.kt @@ -1,6 +1,6 @@ package io.legado.app.data.dao -import androidx.paging.DataSource +import androidx.lifecycle.LiveData import androidx.room.* import io.legado.app.data.entities.SearchKeyword @@ -9,16 +9,16 @@ import io.legado.app.data.entities.SearchKeyword interface SearchKeywordDao { @Query("SELECT * FROM search_keywords ORDER BY usage DESC") - fun observeByUsage(): DataSource.Factory + fun liveDataByUsage(): LiveData> @Query("SELECT * FROM search_keywords ORDER BY lastUseTime DESC") - fun observeByTime(): DataSource.Factory + fun liveDataByTime(): LiveData> - @Insert(onConflict = OnConflictStrategy.REPLACE) - fun insert(vararg keywords: SearchKeyword) + @Query("SELECT * FROM search_keywords where word like '%' | :key | '%' ORDER BY usage DESC") + fun liveDataSearch(key: String): LiveData> @Insert(onConflict = OnConflictStrategy.REPLACE) - fun insert(keyword: SearchKeyword): Long + fun insert(vararg keywords: SearchKeyword) @Update fun update(vararg keywords: SearchKeyword) diff --git a/app/src/main/java/io/legado/app/ui/book/search/BookAdapter.kt b/app/src/main/java/io/legado/app/ui/book/search/BookAdapter.kt index 7b018db40..9c8f2735c 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/BookAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/search/BookAdapter.kt @@ -4,12 +4,16 @@ import android.content.Context import io.legado.app.R import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.SimpleRecyclerAdapter +import io.legado.app.data.entities.Book +import kotlinx.android.synthetic.main.item_text.view.* class BookAdapter(context: Context) : - SimpleRecyclerAdapter(context, R.layout.item_text) { - - override fun convert(holder: ItemViewHolder, item: String, payloads: MutableList) { + SimpleRecyclerAdapter(context, R.layout.item_text) { + override fun convert(holder: ItemViewHolder, item: Book, payloads: MutableList) { + with(holder.itemView) { + text_view.text = item.name + } } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/search/HistoryKeyAdapter.kt b/app/src/main/java/io/legado/app/ui/book/search/HistoryKeyAdapter.kt index 7ceb93993..752c1f705 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/HistoryKeyAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/search/HistoryKeyAdapter.kt @@ -4,13 +4,17 @@ import android.content.Context import io.legado.app.R import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.SimpleRecyclerAdapter +import io.legado.app.data.entities.SearchKeyword +import kotlinx.android.synthetic.main.item_text.view.* class HistoryKeyAdapter(context: Context) : - SimpleRecyclerAdapter(context, R.layout.item_text) { - - override fun convert(holder: ItemViewHolder, item: String, payloads: MutableList) { + SimpleRecyclerAdapter(context, R.layout.item_text) { + override fun convert(holder: ItemViewHolder, item: SearchKeyword, payloads: MutableList) { + with(holder.itemView) { + text_view.text = item.word + } } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt b/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt index 5d3299611..eae5a9ec2 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt @@ -11,11 +11,14 @@ import androidx.recyclerview.widget.RecyclerView import io.legado.app.App import io.legado.app.R import io.legado.app.base.VMBaseActivity +import io.legado.app.data.entities.Book +import io.legado.app.data.entities.SearchKeyword import io.legado.app.data.entities.SearchShow import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.primaryTextColor import io.legado.app.ui.book.info.BookInfoActivity import io.legado.app.utils.getViewModel +import io.legado.app.utils.gone import io.legado.app.utils.invisible import io.legado.app.utils.visible import kotlinx.android.synthetic.main.activity_book_search.* @@ -33,6 +36,8 @@ class SearchActivity : VMBaseActivity(R.layout.activity_book_se private lateinit var bookAdapter: BookAdapter private lateinit var historyKeyAdapter: HistoryKeyAdapter private var searchBookData: LiveData>? = null + private var historyData: LiveData>? = null + private var bookData: LiveData>? = null override fun onActivityCreated(savedInstanceState: Bundle?) { initRecyclerView() @@ -67,10 +72,11 @@ class SearchActivity : VMBaseActivity(R.layout.activity_book_se override fun onQueryTextChange(newText: String?): Boolean { if (newText.isNullOrBlank()) viewModel.stop() + upHistory(newText) return false } }) - search_view.setOnQueryTextFocusChangeListener { v, hasFocus -> + search_view.setOnQueryTextFocusChangeListener { _, hasFocus -> if (hasFocus) { ll_history.visible() } else { @@ -107,6 +113,27 @@ class SearchActivity : VMBaseActivity(R.layout.activity_book_se searchBookData?.observe(this, Observer { adapter.submitList(it) }) } + private fun upHistory(key: String?) { + bookData?.removeObservers(this) + if (key.isNullOrBlank()) { + tv_book_show.gone() + rv_bookshelf_search.gone() + } else { + tv_book_show.visible() + rv_bookshelf_search.visible() + bookData = App.db.bookDao().liveDataSearch(key) + bookData?.observe(this, Observer { bookAdapter.setItems(it) }) + } + historyData?.removeObservers(this) + historyData = + if (key.isNullOrBlank()) { + App.db.searchKeywordDao().liveDataByUsage() + } else { + App.db.searchKeywordDao().liveDataSearch(key) + } + historyData?.observe(this, Observer { historyKeyAdapter.setItems(it) }) + } + override fun showBookInfo(name: String, author: String) { viewModel.getSearchBook(name, author) { searchBook -> searchBook?.let { diff --git a/app/src/main/res/layout/activity_book_search.xml b/app/src/main/res/layout/activity_book_search.xml index f231b65cb..246341e4c 100644 --- a/app/src/main/res/layout/activity_book_search.xml +++ b/app/src/main/res/layout/activity_book_search.xml @@ -45,8 +45,10 @@ app:layout_constraintTop_toBottomOf="@id/title_bar">