pull/32/head
kunfei 5 years ago
parent 43d73a8b42
commit c41ab2d8b9
  1. 3
      app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt
  2. 19
      app/src/main/java/io/legado/app/ui/search/SearchActivity.kt
  3. 81
      app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt

@ -25,6 +25,9 @@ interface BookSourceDao {
@get:Query("select * from book_sources order by customOrder asc") @get:Query("select * from book_sources order by customOrder asc")
val all: List<BookSource> val all: List<BookSource>
@get:Query("select * from book_sources where enabled = 1 order by customOrder asc")
val allEnabled: List<BookSource>
@Query("select * from book_sources where bookSourceUrl = :key") @Query("select * from book_sources where bookSourceUrl = :key")
fun findByKey(key: String): BookSource? fun findByKey(key: String): BookSource?

@ -2,6 +2,7 @@ package io.legado.app.ui.search
import android.os.Bundle import android.os.Bundle
import android.view.View import android.view.View
import androidx.appcompat.widget.SearchView
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.VMBaseActivity import io.legado.app.base.VMBaseActivity
import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.ATH
@ -17,7 +18,6 @@ class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_search)
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
initSearchView() initSearchView()
initRecyclerView() initRecyclerView()
viewModel.search()
} }
private fun initSearchView() { private fun initSearchView() {
@ -26,6 +26,23 @@ class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_search)
search_view.isSubmitButtonEnabled = true search_view.isSubmitButtonEnabled = true
search_view.queryHint = getString(R.string.search_book_key) search_view.queryHint = getString(R.string.search_book_key)
search_view.clearFocus() 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 { intent.getStringExtra("key")?.let {
search_view.setQuery(it, true) search_view.setQuery(it, true)
} }

@ -4,85 +4,38 @@ import android.app.Application
import android.util.Log import android.util.Log
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData import androidx.lifecycle.MutableLiveData
import io.legado.app.App
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.data.api.CommonHttpApi import io.legado.app.data.api.CommonHttpApi
import io.legado.app.data.entities.SearchBook 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.help.http.HttpHelper
import io.legado.app.model.WebBook
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
class SearchViewModel(application: Application) : BaseViewModel(application) { class SearchViewModel(application: Application) : BaseViewModel(application) {
val tasks: CompositeCoroutine = CompositeCoroutine()
val searchBooks: LiveData<List<SearchBook>> = MutableLiveData() val searchBookList = arrayListOf<SearchBook>()
val searchBooksData: LiveData<List<SearchBook>> = MutableLiveData()
var searchPage = 0
private val channel = Channel<Int>()//协程之间通信 private val channel = Channel<Int>()//协程之间通信
fun search(start: (() -> Unit)? = null, finally: (() -> Unit)? = null) { fun search(key: String, start: (() -> Unit)? = null, finally: (() -> Unit)? = null) {
launch { if (key.isEmpty()) return
repeat(1000) { start?.invoke()
channel.send(it) 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<CommonHttpApi>(
"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<String> { suspend fun test(scope: CoroutineScope): MutableList<String> {

Loading…
Cancel
Save