diff --git a/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt b/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt index 832f877ea..ddc2fdc1b 100644 --- a/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt @@ -25,6 +25,9 @@ interface BookSourceDao { @get:Query("select * from book_sources order by customOrder asc") val all: List + @get:Query("select * from book_sources where enabled = 1 order by customOrder asc") + val allEnabled: List + @Query("select * from book_sources where bookSourceUrl = :key") fun findByKey(key: String): BookSource? diff --git a/app/src/main/java/io/legado/app/ui/search/SearchActivity.kt b/app/src/main/java/io/legado/app/ui/search/SearchActivity.kt index cc9b7a76b..3d6dfb1dc 100644 --- a/app/src/main/java/io/legado/app/ui/search/SearchActivity.kt +++ b/app/src/main/java/io/legado/app/ui/search/SearchActivity.kt @@ -2,6 +2,7 @@ package io.legado.app.ui.search import android.os.Bundle import android.view.View +import androidx.appcompat.widget.SearchView import io.legado.app.R import io.legado.app.base.VMBaseActivity import io.legado.app.lib.theme.ATH @@ -17,7 +18,6 @@ class SearchActivity : VMBaseActivity(R.layout.activity_search) override fun onActivityCreated(savedInstanceState: Bundle?) { initSearchView() initRecyclerView() - viewModel.search() } private fun initSearchView() { @@ -26,6 +26,23 @@ class SearchActivity : VMBaseActivity(R.layout.activity_search) search_view.isSubmitButtonEnabled = true search_view.queryHint = getString(R.string.search_book_key) search_view.clearFocus() + search_view.setOnQueryTextListener(object : SearchView.OnQueryTextListener { + override fun onQueryTextSubmit(query: String?): Boolean { + query?.let { + viewModel.search(it, { + content_view.showProgressView() + }, { + + }) + } + return true + } + + override fun onQueryTextChange(newText: String?): Boolean { + return false + } + + }) intent.getStringExtra("key")?.let { search_view.setQuery(it, true) } diff --git a/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt b/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt index d3dddb787..3ca042ad0 100644 --- a/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt @@ -4,85 +4,38 @@ import android.app.Application import android.util.Log import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData +import io.legado.app.App import io.legado.app.base.BaseViewModel import io.legado.app.data.api.CommonHttpApi import io.legado.app.data.entities.SearchBook +import io.legado.app.help.coroutine.CompositeCoroutine import io.legado.app.help.http.HttpHelper +import io.legado.app.model.WebBook import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.channels.Channel -import kotlinx.coroutines.delay -import kotlinx.coroutines.launch import kotlinx.coroutines.withContext class SearchViewModel(application: Application) : BaseViewModel(application) { - - val searchBooks: LiveData> = MutableLiveData() - + val tasks: CompositeCoroutine = CompositeCoroutine() + val searchBookList = arrayListOf() + val searchBooksData: LiveData> = MutableLiveData() + var searchPage = 0 private val channel = Channel()//协程之间通信 - fun search(start: (() -> Unit)? = null, finally: (() -> Unit)? = null) { - launch { - repeat(1000) { - channel.send(it) + fun search(key: String, start: (() -> Unit)? = null, finally: (() -> Unit)? = null) { + if (key.isEmpty()) return + start?.invoke() + execute { + val bookSourceList = App.db.bookSourceDao().allEnabled + for (item in bookSourceList) { + val search = WebBook(item).searchBook(key, searchPage) + .onSuccess { searchBookS -> + searchBookS?.let { searchBookList.addAll(it) } + } + tasks.add(search) } - } - - - val c = execute { - val response: String = HttpHelper.getApiService( - "http://www.baidu.com" - ).get("http://www.baidu.com").await() - - delay(2000L) - - response } - .onStart { - Log.e("TAG!", "start") - start?.let { it() } - } - .onSuccess { - Log.e("TAG!", "success: $it") - } - .onError { - Log.e("TAG!", "error: $it") - } - .onFinally { - Log.e("TAG!", "finally") - if (finally != null) { - finally() - } - } - - val c2 = plus(c) -// .timeout { 100L } -// .onErrorReturn { "error return2" } - .onStart { - //会拦截掉c的onStart - Log.e("TAG!", "start2") - start?.let { it() } - } -// .onSuccess { -// Log.e("TAG!", "success2: $it") -// } - .onError { - Log.e("TAG!", "error2: $it") - } - .onFinally { - Log.e("TAG!", "finally2") - if (finally != null) { - finally() - } - - Log.e("TAG!", "rec2: " + channel.receive()) - } - -// execute { -// test(this) -// }.onSuccess { -// println("size: ${it?.size} $it") -// } } suspend fun test(scope: CoroutineScope): MutableList {