pull/36/head
kunfei 5 years ago
parent 39c8a1d06b
commit 0b1c508fc9
  1. 3
      app/src/main/java/io/legado/app/data/dao/SearchKeywordDao.kt
  2. 2
      app/src/main/java/io/legado/app/data/entities/SearchKeyword.kt
  3. 1
      app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt
  4. 10
      app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt

@ -17,6 +17,9 @@ interface SearchKeywordDao {
@Query("SELECT * FROM search_keywords where word like '%'||:key||'%' ORDER BY usage DESC") @Query("SELECT * FROM search_keywords where word like '%'||:key||'%' ORDER BY usage DESC")
fun liveDataSearch(key: String): LiveData<List<SearchKeyword>> fun liveDataSearch(key: String): LiveData<List<SearchKeyword>>
@Query("select * from search_keywords where word = :key")
fun get(key: String): SearchKeyword?
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(vararg keywords: SearchKeyword) fun insert(vararg keywords: SearchKeyword)

@ -13,5 +13,5 @@ data class SearchKeyword(
@PrimaryKey @PrimaryKey
var word: String = "", // 搜索关键词 var word: String = "", // 搜索关键词
var usage: Int = 1, // 使用次数 var usage: Int = 1, // 使用次数
var lastUseTime: Long = 0 // 最后一次使用时间 var lastUseTime: Long = System.currentTimeMillis() // 最后一次使用时间
) : Parcelable ) : Parcelable

@ -58,6 +58,7 @@ class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_book_se
override fun onQueryTextSubmit(query: String?): Boolean { override fun onQueryTextSubmit(query: String?): Boolean {
search_view.clearFocus() search_view.clearFocus()
query?.let { query?.let {
viewModel.saveSearchKey(query)
viewModel.search(it, { viewModel.search(it, {
refresh_progress_bar.isAutoLoading = true refresh_progress_bar.isAutoLoading = true
initData() initData()

@ -4,6 +4,7 @@ import android.app.Application
import io.legado.app.App import io.legado.app.App
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.data.entities.SearchBook import io.legado.app.data.entities.SearchBook
import io.legado.app.data.entities.SearchKeyword
import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.coroutine.Coroutine
import io.legado.app.model.WebBook import io.legado.app.model.WebBook
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@ -61,4 +62,13 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
success?.invoke(searchBook) success?.invoke(searchBook)
} }
} }
fun saveSearchKey(key: String) {
execute {
App.db.searchKeywordDao().get(key)?.let {
it.usage = it.usage + 1
App.db.searchKeywordDao().update(it)
} ?: App.db.searchKeywordDao().insert(SearchKeyword(key, 1))
}
}
} }

Loading…
Cancel
Save