parent
87a5409eac
commit
5b4e4d4e26
@ -1,25 +0,0 @@ |
||||
package io.legado.app.model |
||||
|
||||
import io.legado.app.data.entities.BookSource |
||||
import io.legado.app.help.AppConfig |
||||
import kotlinx.coroutines.CoroutineScope |
||||
import kotlinx.coroutines.asCoroutineDispatcher |
||||
import java.util.concurrent.Executors |
||||
|
||||
class SearchBook(scope: CoroutineScope, callBack: CallBack) { |
||||
private var searchPool = |
||||
Executors.newFixedThreadPool(AppConfig.threadCount).asCoroutineDispatcher() |
||||
private var searchId = System.currentTimeMillis() |
||||
private var searchPage = 1 |
||||
private var searchKey: String = "" |
||||
private val searchEngines = arrayListOf<SearchEngine>() |
||||
|
||||
|
||||
private data class SearchEngine(val bookSource: BookSource, var hasMore: Boolean = true) |
||||
|
||||
interface CallBack { |
||||
fun onSearchSuccess() |
||||
fun onSearchFinish() |
||||
} |
||||
|
||||
} |
@ -0,0 +1,78 @@ |
||||
package io.legado.app.model |
||||
|
||||
import io.legado.app.App |
||||
import io.legado.app.data.entities.BookSource |
||||
import io.legado.app.data.entities.SearchBook |
||||
import io.legado.app.help.AppConfig |
||||
import io.legado.app.help.coroutine.Coroutine |
||||
import io.legado.app.utils.getPrefString |
||||
import kotlinx.coroutines.CoroutineScope |
||||
import kotlinx.coroutines.Dispatchers.IO |
||||
import kotlinx.coroutines.asCoroutineDispatcher |
||||
import java.util.concurrent.Executors |
||||
|
||||
class SearchBookModel(private val scope: CoroutineScope, private val callBack: CallBack) { |
||||
private var searchPool = |
||||
Executors.newFixedThreadPool(AppConfig.threadCount).asCoroutineDispatcher() |
||||
private var mSearchId = System.currentTimeMillis() |
||||
private var searchPage = 1 |
||||
private var searchKey: String = "" |
||||
private var task: Coroutine<*>? = null |
||||
|
||||
fun search(searchId: Long, key: String) { |
||||
if (searchId != mSearchId) { |
||||
task?.cancel() |
||||
mSearchId = searchId |
||||
searchPage = 1 |
||||
if (key.isEmpty()) { |
||||
return |
||||
} else { |
||||
this.searchKey = key |
||||
} |
||||
} else { |
||||
searchPage++ |
||||
} |
||||
task = Coroutine.async(scope, searchPool) { |
||||
val searchGroup = App.INSTANCE.getPrefString("searchGroup") ?: "" |
||||
val bookSourceList = if (searchGroup.isBlank()) { |
||||
App.db.bookSourceDao().allEnabled |
||||
} else { |
||||
App.db.bookSourceDao().getEnabledByGroup(searchGroup) |
||||
} |
||||
for (item in bookSourceList) { |
||||
//task取消时自动取消 by (scope = this@execute) |
||||
WebBook(item).searchBook( |
||||
searchKey, |
||||
searchPage, |
||||
scope = this, |
||||
context = searchPool |
||||
) |
||||
.timeout(30000L) |
||||
.onSuccess(IO) { |
||||
it?.let { |
||||
if (searchId == mSearchId) { |
||||
callBack.onSearchSuccess(it) |
||||
} |
||||
} |
||||
} |
||||
} |
||||
}.onStart { |
||||
|
||||
}.onCancel { |
||||
|
||||
} |
||||
|
||||
task?.invokeOnCompletion { |
||||
|
||||
} |
||||
} |
||||
|
||||
private data class SearchEngine(val bookSource: BookSource, var hasMore: Boolean = true) |
||||
|
||||
interface CallBack { |
||||
fun onSearchSuccess(searchBooks: ArrayList<SearchBook>) |
||||
fun onSearchFinish() |
||||
fun onSearchCancel() |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue