pull/32/head
kunfei 5 years ago
parent e8219eeb10
commit 743f6da9b5
  1. 6
      app/src/main/java/io/legado/app/data/dao/SearchBookDao.kt
  2. 2
      app/src/main/java/io/legado/app/model/webbook/BookList.kt
  3. 7
      app/src/main/java/io/legado/app/ui/search/SearchActivity.kt
  4. 42
      app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt

@ -2,6 +2,8 @@ package io.legado.app.data.dao
import androidx.paging.DataSource
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import io.legado.app.data.entities.SearchBook
@ -11,8 +13,10 @@ interface SearchBookDao {
@Query("SELECT * FROM searchBooks")
fun observeAll(): DataSource.Factory<Int, SearchBook>
@Query("SELECT * FROM searchBooks where time >= :time")
@Query("SELECT name, author, '' bookUrl, '' origin, '' originName, min(time) time, max(intro) intro, max(kind) kind, max(coverUrl) coverUrl, max(latestChapterTitle) latestChapterTitle FROM searchBooks where time >= :time group by name and author")
fun observeNew(time: Long): DataSource.Factory<Int, SearchBook>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(vararg searchBook: SearchBook)
}

@ -131,6 +131,7 @@ object BookList {
SourceDebug.printLog(bookSource.bookSourceUrl, 1, "获取最新章节")
searchBook.latestChapterTitle = analyzeRule.getString(lastChapter ?: "")
SourceDebug.printLog(bookSource.bookSourceUrl, 1, searchBook.latestChapterTitle ?: "")
searchBook.time = System.currentTimeMillis()
return searchBook
}
}
@ -182,6 +183,7 @@ object BookList {
SourceDebug.printLog(bookSource.bookSourceUrl, 1, "获取最新章节", printLog)
searchBook.latestChapterTitle = analyzeRule.getString(ruleLastChapter)
SourceDebug.printLog(bookSource.bookSourceUrl, 1, searchBook.latestChapterTitle ?: "", printLog)
searchBook.time = System.currentTimeMillis()
return searchBook
}
return null

@ -32,8 +32,9 @@ class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_search)
search_view.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
query?.let {
viewModel.search(it, {
viewModel.search(it, { startTime ->
content_view.showProgressView()
initData(startTime)
}, {
})
@ -58,4 +59,8 @@ class SearchActivity : VMBaseActivity<SearchViewModel>(R.layout.activity_search)
rv_search_list.adapter = adapter
}
private fun initData(startTime: Long) {
}
}

@ -1,36 +1,30 @@
package io.legado.app.ui.search
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.withContext
class SearchViewModel(application: Application) : BaseViewModel(application) {
val tasks: CompositeCoroutine = CompositeCoroutine()
val searchBookList = arrayListOf<SearchBook>()
val searchBooksData: LiveData<List<SearchBook>> = MutableLiveData()
private val tasks: CompositeCoroutine = CompositeCoroutine()
var searchPage = 0
private val channel = Channel<Int>()//协程之间通信
fun search(key: String, start: (() -> Unit)? = null, finally: (() -> Unit)? = null) {
fun search(key: String, start: ((startTime: Long) -> Unit)? = null, finally: (() -> Unit)? = null) {
if (key.isEmpty()) return
start?.invoke()
tasks.clear()
start?.invoke(System.currentTimeMillis())
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) }
searchBookS?.let {
for (searchBook in searchBookS) {
}
App.db.searchBookDao().insert(*it.toTypedArray())
}
}
tasks.add(search)
}
@ -38,18 +32,8 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
}
}
suspend fun test(scope: CoroutineScope): MutableList<String> {
val list = mutableListOf<String>()
repeat(10) {
withContext(scope.coroutineContext) {
Log.e("TAG3", Thread.currentThread().name)
val response: String = HttpHelper.getApiService<CommonHttpApi>(
"http://www.baidu.com"
).get("http://www.baidu.com").await()
list.add(response)
}
}
return list
override fun onCleared() {
super.onCleared()
tasks.clear()
}
}

Loading…
Cancel
Save