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 48b60e9b0..213157e90 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 @@ -1,12 +1,16 @@ package io.legado.app.ui.book.search import android.content.Context +import io.legado.app.App 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 io.legado.app.ui.widget.anima.explosion_field.ExplosionField import kotlinx.android.synthetic.main.item_text.view.* +import kotlinx.coroutines.Dispatchers.IO +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.launch import org.jetbrains.anko.sdk27.listeners.onClick import org.jetbrains.anko.sdk27.listeners.onLongClick @@ -24,7 +28,9 @@ class HistoryKeyAdapter(context: Context, val callBack: CallBack) : it?.let { ExplosionField(context).explode(it, true) } - + GlobalScope.launch(IO) { + App.db.searchKeywordDao().delete(item) + } true } } 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 bdf2525f0..098fe7983 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 @@ -44,6 +44,7 @@ class SearchActivity : VMBaseActivity(R.layout.activity_book_se override fun onActivityCreated(savedInstanceState: Bundle?) { initRecyclerView() initSearchView() + initOtherView() initData() intent.getStringExtra("key")?.let { search_view.setQuery(it, true) @@ -105,6 +106,12 @@ class SearchActivity : VMBaseActivity(R.layout.activity_book_se fb_stop.onClick { viewModel.stop() } } + private fun initOtherView() { + tv_clear_history.onClick { + viewModel.clearHistory() + } + } + private fun initData() { searchBookData?.removeObservers(this) searchBookData = LivePagedListBuilder( 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 85b3694db..011a13d8a 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 @@ -71,4 +71,10 @@ class SearchViewModel(application: Application) : BaseViewModel(application) { } ?: App.db.searchKeywordDao().insert(SearchKeyword(key, 1)) } } + + fun clearHistory() { + execute { + App.db.searchKeywordDao().deleteAll() + } + } }