|
|
@ -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> { |
|
|
|