Decrease debounce time for library search

Async library search code cleanup
Rename LoadingTools -> LoaderManager
This commit is contained in:
NerdNumber9 2019-04-12 12:16:52 -04:00
parent 6f2ff6a77e
commit 9b3579acf6
4 changed files with 18 additions and 5 deletions

View File

@ -7,6 +7,7 @@ import eu.kanade.tachiyomi.data.database.models.Manga
import exh.isLewdSource import exh.isLewdSource
import exh.metadata.sql.tables.SearchMetadataTable import exh.metadata.sql.tables.SearchMetadataTable
import exh.search.SearchEngine import exh.search.SearchEngine
import exh.util.cancellable
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlinx.coroutines.flow.asFlow import kotlinx.coroutines.flow.asFlow
import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.filter
@ -77,24 +78,24 @@ class LibraryCategoryAdapter(val view: LibraryCategoryView) :
.args(*sqlQuery.second.toTypedArray()) .args(*sqlQuery.second.toTypedArray())
.build()) .build())
if(!isActive) return@launch // Fail early when cancelled ensureActive() // Fail early when cancelled
val convertedResult = LongArray(queryResult.count) val convertedResult = LongArray(queryResult.count)
if(convertedResult.isNotEmpty()) { if(convertedResult.isNotEmpty()) {
val mangaIdCol = queryResult.getColumnIndex(SearchMetadataTable.COL_MANGA_ID) val mangaIdCol = queryResult.getColumnIndex(SearchMetadataTable.COL_MANGA_ID)
queryResult.moveToFirst() queryResult.moveToFirst()
while (!queryResult.isAfterLast) { while (!queryResult.isAfterLast) {
if(!isActive) return@launch // Fail early when cancelled ensureActive() // Fail early when cancelled
convertedResult[queryResult.position] = queryResult.getLong(mangaIdCol) convertedResult[queryResult.position] = queryResult.getLong(mangaIdCol)
queryResult.moveToNext() queryResult.moveToNext()
} }
} }
if(!isActive) return@launch // Fail early when cancelled ensureActive() // Fail early when cancelled
// Flow the mangas to allow cancellation of this filter operation // Flow the mangas to allow cancellation of this filter operation
mangas.asFlow().filter { item -> mangas.asFlow().cancellable().filter { item ->
if(isLewdSource(item.manga.source)) { if(isLewdSource(item.manga.source)) {
convertedResult.binarySearch(item.manga.id ?: -1) >= 0 convertedResult.binarySearch(item.manga.id ?: -1) >= 0
} else { } else {

View File

@ -131,7 +131,7 @@ class LibraryCategoryView @JvmOverloads constructor(context: Context, attrs: Att
subscriptions += controller.searchRelay subscriptions += controller.searchRelay
.doOnNext { adapter.searchText = it } .doOnNext { adapter.searchText = it }
.skip(1) .skip(1)
.debounce(500, TimeUnit.MILLISECONDS) .debounce(250, TimeUnit.MILLISECONDS)
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe { .subscribe {
// EXH --> // EXH -->

View File

@ -0,0 +1,12 @@
package exh.util
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.onEach
import kotlin.coroutines.coroutineContext
@FlowPreview
fun <T> Flow<T>.cancellable() = onEach {
coroutineContext.ensureActive()
}