diff --git a/app/src/main/java/io/legado/app/model/SearchBook.kt b/app/src/main/java/io/legado/app/model/SearchBook.kt deleted file mode 100644 index f70d931a2..000000000 --- a/app/src/main/java/io/legado/app/model/SearchBook.kt +++ /dev/null @@ -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() - - - private data class SearchEngine(val bookSource: BookSource, var hasMore: Boolean = true) - - interface CallBack { - fun onSearchSuccess() - fun onSearchFinish() - } - -} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/model/SearchBookModel.kt b/app/src/main/java/io/legado/app/model/SearchBookModel.kt new file mode 100644 index 000000000..8b415bc9f --- /dev/null +++ b/app/src/main/java/io/legado/app/model/SearchBookModel.kt @@ -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) + fun onSearchFinish() + fun onSearchCancel() + } + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/model/WebBook.kt b/app/src/main/java/io/legado/app/model/WebBook.kt index 6fee7add6..cb1742132 100644 --- a/app/src/main/java/io/legado/app/model/WebBook.kt +++ b/app/src/main/java/io/legado/app/model/WebBook.kt @@ -27,7 +27,7 @@ class WebBook(val bookSource: BookSource) { page: Int? = 1, scope: CoroutineScope = Coroutine.DEFAULT, context: CoroutineContext = Dispatchers.IO - ): Coroutine> { + ): Coroutine> { return Coroutine.async(scope, context) { searchBookSuspend(scope, key, page) }