@ -1,36 +1,30 @@
package io.legado.app.ui.search
package io.legado.app.ui.search
import android.app.Application
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.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.entities.SearchBook
import io.legado.app.help.coroutine.CompositeCoroutine
import io.legado.app.help.coroutine.CompositeCoroutine
import io.legado.app.help.http.HttpHelper
import io.legado.app.model.WebBook
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 ) {
class SearchViewModel ( application : Application ) : BaseViewModel ( application ) {
val tasks : CompositeCoroutine = CompositeCoroutine ( )
private val tasks : CompositeCoroutine = CompositeCoroutine ( )
val searchBookList = arrayListOf < SearchBook > ( )
val searchBooksData : LiveData < List < SearchBook > > = MutableLiveData ( )
var searchPage = 0
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
if ( key . isEmpty ( ) ) return
start ?. invoke ( )
tasks . clear ( )
start ?. invoke ( System . currentTimeMillis ( ) )
execute {
execute {
val bookSourceList = App . db . bookSourceDao ( ) . allEnabled
val bookSourceList = App . db . bookSourceDao ( ) . allEnabled
for ( item in bookSourceList ) {
for ( item in bookSourceList ) {
val search = WebBook ( item ) . searchBook ( key , searchPage )
val search = WebBook ( item ) . searchBook ( key , searchPage )
. onSuccess { searchBookS ->
. onSuccess { searchBookS ->
searchBookS ?. let { searchBookList . addAll ( it ) }
searchBookS ?. let {
for ( searchBook in searchBookS ) {
}
App . db . searchBookDao ( ) . insert ( * it . toTypedArray ( ) )
}
}
}
tasks . add ( search )
tasks . add ( search )
}
}
@ -38,18 +32,8 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
}
}
}
}
suspend fun test ( scope : CoroutineScope ) : MutableList < String > {
override fun onCleared ( ) {
val list = mutableListOf < String > ( )
super . onCleared ( )
repeat ( 10 ) {
tasks . clear ( )
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
}
}
}
}