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 704f37287..be7ad75c5 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 @@ -17,6 +17,9 @@ interface SearchKeywordDao { @Query("SELECT * FROM search_keywords where word like '%'||:key||'%' ORDER BY usage DESC") fun liveDataSearch(key: String): LiveData> + @Query("select * from search_keywords where word = :key") + fun get(key: String): SearchKeyword? + @Insert(onConflict = OnConflictStrategy.REPLACE) fun insert(vararg keywords: SearchKeyword) diff --git a/app/src/main/java/io/legado/app/data/entities/SearchKeyword.kt b/app/src/main/java/io/legado/app/data/entities/SearchKeyword.kt index 1c1c5015b..89d1c3baf 100644 --- a/app/src/main/java/io/legado/app/data/entities/SearchKeyword.kt +++ b/app/src/main/java/io/legado/app/data/entities/SearchKeyword.kt @@ -13,5 +13,5 @@ data class SearchKeyword( @PrimaryKey var word: String = "", // 搜索关键词 var usage: Int = 1, // 使用次数 - var lastUseTime: Long = 0 // 最后一次使用时间 + var lastUseTime: Long = System.currentTimeMillis() // 最后一次使用时间 ) : Parcelable 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 eae5a9ec2..ed51e45df 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 @@ -58,6 +58,7 @@ class SearchActivity : VMBaseActivity(R.layout.activity_book_se override fun onQueryTextSubmit(query: String?): Boolean { search_view.clearFocus() query?.let { + viewModel.saveSearchKey(query) viewModel.search(it, { refresh_progress_bar.isAutoLoading = true initData() diff --git a/app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt b/app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt index f8550229f..85b3694db 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt @@ -4,6 +4,7 @@ import android.app.Application import io.legado.app.App import io.legado.app.base.BaseViewModel 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.model.WebBook import kotlinx.coroutines.Dispatchers @@ -61,4 +62,13 @@ class SearchViewModel(application: Application) : BaseViewModel(application) { 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)) + } + } }