Fix global search results order

pull/178/head
Koitharu 4 years ago
parent ccb31de1ba
commit 30c0fd600f
No known key found for this signature in database
GPG Key ID: 8E861F8CE6E7CE27

@ -0,0 +1,7 @@
package org.koitharu.kotatsu.core.exceptions
import org.koitharu.kotatsu.parsers.util.mapNotNullToSet
class CompositeException(val errors: Collection<Throwable>) : Exception(
message = errors.mapNotNullToSet { it.message }.joinToString()
)

@ -9,6 +9,7 @@ import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.update import kotlinx.coroutines.flow.update
import org.koitharu.kotatsu.R import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.base.ui.BaseViewModel import org.koitharu.kotatsu.base.ui.BaseViewModel
import org.koitharu.kotatsu.core.exceptions.CompositeException
import org.koitharu.kotatsu.core.parser.MangaRepository import org.koitharu.kotatsu.core.parser.MangaRepository
import org.koitharu.kotatsu.core.prefs.AppSettings import org.koitharu.kotatsu.core.prefs.AppSettings
import org.koitharu.kotatsu.core.prefs.ListMode import org.koitharu.kotatsu.core.prefs.ListMode
@ -78,8 +79,7 @@ class MultiSearchViewModel(
listData.value = emptyList() listData.value = emptyList()
loadingData.value = true loadingData.value = true
query.postValue(q) query.postValue(q)
val errors = searchImpl(q) searchImpl(q)
listError.value = errors.firstOrNull()
} catch (e: Throwable) { } catch (e: Throwable) {
listError.value = e listError.value = e
} finally { } finally {
@ -88,25 +88,44 @@ class MultiSearchViewModel(
} }
} }
private suspend fun searchImpl(q: String): List<Throwable> { private suspend fun searchImpl(q: String) {
val sources = settings.getMangaSources(includeHidden = false) val sources = settings.getMangaSources(includeHidden = false)
val dispatcher = Dispatchers.Default.limitedParallelism(MAX_PARALLELISM) val dispatcher = Dispatchers.Default.limitedParallelism(MAX_PARALLELISM)
return coroutineScope { val deferredList = coroutineScope {
sources.map { source -> sources.map { source ->
async(dispatcher) { async(dispatcher) {
runCatching { runCatching {
val list = MangaRepository(source).getList(offset = 0, query = q) val list = MangaRepository(source).getList(offset = 0, query = q)
// .sortedBy { x -> x.title.levenshteinDistance(q) }
.toUi(ListMode.GRID) .toUi(ListMode.GRID)
if (list.isNotEmpty()) { if (list.isNotEmpty()) {
val item = MultiSearchListModel(source, list) MultiSearchListModel(source, list)
listData.update { x -> x + item } } else {
null
} }
}.onFailure { }.onFailure {
it.printStackTraceDebug() it.printStackTraceDebug()
}.exceptionOrNull() }
} }
} }
}.awaitAll().filterNotNull() }
val errors = ArrayList<Throwable>()
for (deferred in deferredList) {
deferred.await()
.onSuccess { item ->
if (item != null) {
listData.update { x -> x + item }
}
}.onFailure {
errors.add(it)
}
}
if (listData.value.isNotEmpty()) {
return
}
when (errors.size) {
0 -> Unit
1 -> throw errors[0]
else -> throw CompositeException(errors)
}
} }
} }
Loading…
Cancel
Save