pull/36/head
kunfei 5 years ago
parent abc9c2f196
commit 5c41348cb5
  1. 3
      app/src/main/java/io/legado/app/data/dao/BookDao.kt
  2. 12
      app/src/main/java/io/legado/app/data/dao/SearchKeywordDao.kt
  3. 10
      app/src/main/java/io/legado/app/ui/book/search/BookAdapter.kt
  4. 10
      app/src/main/java/io/legado/app/ui/book/search/HistoryKeyAdapter.kt
  5. 29
      app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt
  6. 3
      app/src/main/res/layout/activity_book_search.xml

@ -24,6 +24,9 @@ interface BookDao {
@Query("SELECT bookUrl FROM books WHERE `group` = :group")
fun observeUrlsByGroup(group: Int): LiveData<List<String>>
@Query("SELECT bookUrl FROM books WHERE name like '%' | :key | '%' or author like '%' | :key | '%'")
fun liveDataSearch(key: String): LiveData<List<Book>>
@Query("SELECT * FROM books WHERE `name` in (:names)")
fun findByName(vararg names: String): List<Book>

@ -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<Int, SearchKeyword>
fun liveDataByUsage(): LiveData<List<SearchKeyword>>
@Query("SELECT * FROM search_keywords ORDER BY lastUseTime DESC")
fun observeByTime(): DataSource.Factory<Int, SearchKeyword>
fun liveDataByTime(): LiveData<List<SearchKeyword>>
@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<List<SearchKeyword>>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(keyword: SearchKeyword): Long
fun insert(vararg keywords: SearchKeyword)
@Update
fun update(vararg keywords: SearchKeyword)

@ -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<String>(context, R.layout.item_text) {
override fun convert(holder: ItemViewHolder, item: String, payloads: MutableList<Any>) {
SimpleRecyclerAdapter<Book>(context, R.layout.item_text) {
override fun convert(holder: ItemViewHolder, item: Book, payloads: MutableList<Any>) {
with(holder.itemView) {
text_view.text = item.name
}
}
}

@ -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<String>(context, R.layout.item_text) {
override fun convert(holder: ItemViewHolder, item: String, payloads: MutableList<Any>) {
SimpleRecyclerAdapter<SearchKeyword>(context, R.layout.item_text) {
override fun convert(holder: ItemViewHolder, item: SearchKeyword, payloads: MutableList<Any>) {
with(holder.itemView) {
text_view.text = item.word
}
}
}

@ -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<SearchViewModel>(R.layout.activity_book_se
private lateinit var bookAdapter: BookAdapter
private lateinit var historyKeyAdapter: HistoryKeyAdapter
private var searchBookData: LiveData<PagedList<SearchShow>>? = null
private var historyData: LiveData<List<SearchKeyword>>? = null
private var bookData: LiveData<List<Book>>? = null
override fun onActivityCreated(savedInstanceState: Bundle?) {
initRecyclerView()
@ -67,10 +72,11 @@ class SearchActivity : VMBaseActivity<SearchViewModel>(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<SearchViewModel>(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 {

@ -45,8 +45,10 @@
app:layout_constraintTop_toBottomOf="@id/title_bar">
<TextView
android:id="@+id/tv_book_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="6dp"
android:text="@string/bookshelf" />
<androidx.recyclerview.widget.RecyclerView
@ -58,6 +60,7 @@
android:id="@+id/tv_history"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="6dp"
android:text="@string/searchHistory" />
<androidx.recyclerview.widget.RecyclerView

Loading…
Cancel
Save