|
|
|
@ -16,7 +16,7 @@ import splitties.init.appCtx |
|
|
|
|
import java.util.concurrent.Executors |
|
|
|
|
import kotlin.math.min |
|
|
|
|
|
|
|
|
|
class SearchModel(private val scope: CoroutineScope) { |
|
|
|
|
class SearchModel(private val scope: CoroutineScope, private val callBack: CallBack) { |
|
|
|
|
val threadCount = AppConfig.threadCount |
|
|
|
|
private var searchPool: ExecutorCoroutineDispatcher? = null |
|
|
|
|
private var mSearchId = 0L |
|
|
|
@ -25,19 +25,10 @@ class SearchModel(private val scope: CoroutineScope) { |
|
|
|
|
private var tasks = CompositeCoroutine() |
|
|
|
|
private var bookSourceList = arrayListOf<BookSource>() |
|
|
|
|
private var searchBooks = arrayListOf<SearchBook>() |
|
|
|
|
private var callBack: CallBack? = null |
|
|
|
|
|
|
|
|
|
@Volatile |
|
|
|
|
private var searchIndex = -1 |
|
|
|
|
|
|
|
|
|
fun registerCallback(callBack: CallBack) { |
|
|
|
|
this.callBack = callBack |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun unRegisterCallback() { |
|
|
|
|
this.callBack = null |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun initSearchPool() { |
|
|
|
|
searchPool?.close() |
|
|
|
|
searchPool = Executors |
|
|
|
@ -45,10 +36,10 @@ class SearchModel(private val scope: CoroutineScope) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun search(searchId: Long, key: String) { |
|
|
|
|
callBack?.onSearchStart() |
|
|
|
|
callBack.onSearchStart() |
|
|
|
|
if (searchId != mSearchId) { |
|
|
|
|
if (key.isEmpty()) { |
|
|
|
|
callBack?.onSearchCancel() |
|
|
|
|
callBack.onSearchCancel() |
|
|
|
|
return |
|
|
|
|
} else { |
|
|
|
|
this.searchKey = key |
|
|
|
@ -62,7 +53,7 @@ class SearchModel(private val scope: CoroutineScope) { |
|
|
|
|
val searchGroup = AppConfig.searchGroup |
|
|
|
|
bookSourceList.clear() |
|
|
|
|
searchBooks.clear() |
|
|
|
|
callBack?.onSearchSuccess(searchBooks) |
|
|
|
|
callBack.onSearchSuccess(searchBooks) |
|
|
|
|
if (searchGroup.isBlank()) { |
|
|
|
|
bookSourceList.addAll(appDb.bookSourceDao.allEnabled) |
|
|
|
|
} else { |
|
|
|
@ -114,7 +105,7 @@ class SearchModel(private val scope: CoroutineScope) { |
|
|
|
|
appDb.searchBookDao.insert(*items.toTypedArray()) |
|
|
|
|
val precision = appCtx.getPrefBoolean(PreferKey.precisionSearch) |
|
|
|
|
mergeItems(scope, items, precision) |
|
|
|
|
callBack?.onSearchSuccess(searchBooks) |
|
|
|
|
callBack.onSearchSuccess(searchBooks) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -128,7 +119,7 @@ class SearchModel(private val scope: CoroutineScope) { |
|
|
|
|
if (searchIndex >= bookSourceList.lastIndex |
|
|
|
|
+ min(bookSourceList.size, threadCount) |
|
|
|
|
) { |
|
|
|
|
callBack?.onSearchFinish(searchBooks.isEmpty()) |
|
|
|
|
callBack.onSearchFinish(searchBooks.isEmpty()) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -201,7 +192,7 @@ class SearchModel(private val scope: CoroutineScope) { |
|
|
|
|
|
|
|
|
|
fun cancelSearch() { |
|
|
|
|
close() |
|
|
|
|
callBack?.onSearchCancel() |
|
|
|
|
callBack.onSearchCancel() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fun close() { |
|
|
|
|