feat: 优化代码

pull/169/head
kunfei 5 years ago
parent 87a5409eac
commit 5b4e4d4e26
  1. 25
      app/src/main/java/io/legado/app/model/SearchBook.kt
  2. 78
      app/src/main/java/io/legado/app/model/SearchBookModel.kt
  3. 2
      app/src/main/java/io/legado/app/model/WebBook.kt

@ -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()
}
}

@ -27,7 +27,7 @@ class WebBook(val bookSource: BookSource) {
page: Int? = 1,
scope: CoroutineScope = Coroutine.DEFAULT,
context: CoroutineContext = Dispatchers.IO
): Coroutine<List<SearchBook>> {
): Coroutine<ArrayList<SearchBook>> {
return Coroutine.async(scope, context) {
searchBookSuspend(scope, key, page)
}

Loading…
Cancel
Save