From 30c0fd600fcb61b978721c8f46347682f0d4449e Mon Sep 17 00:00:00 2001 From: Koitharu Date: Tue, 31 May 2022 16:43:23 +0300 Subject: [PATCH] Fix global search results order --- .../core/exceptions/CompositeException.kt | 7 ++++ .../search/ui/multi/MultiSearchViewModel.kt | 37 ++++++++++++++----- 2 files changed, 35 insertions(+), 9 deletions(-) create mode 100644 app/src/main/java/org/koitharu/kotatsu/core/exceptions/CompositeException.kt diff --git a/app/src/main/java/org/koitharu/kotatsu/core/exceptions/CompositeException.kt b/app/src/main/java/org/koitharu/kotatsu/core/exceptions/CompositeException.kt new file mode 100644 index 000000000..353ff1ca6 --- /dev/null +++ b/app/src/main/java/org/koitharu/kotatsu/core/exceptions/CompositeException.kt @@ -0,0 +1,7 @@ +package org.koitharu.kotatsu.core.exceptions + +import org.koitharu.kotatsu.parsers.util.mapNotNullToSet + +class CompositeException(val errors: Collection) : Exception( + message = errors.mapNotNullToSet { it.message }.joinToString() +) \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/search/ui/multi/MultiSearchViewModel.kt b/app/src/main/java/org/koitharu/kotatsu/search/ui/multi/MultiSearchViewModel.kt index 40b6a8619..adea07898 100644 --- a/app/src/main/java/org/koitharu/kotatsu/search/ui/multi/MultiSearchViewModel.kt +++ b/app/src/main/java/org/koitharu/kotatsu/search/ui/multi/MultiSearchViewModel.kt @@ -9,6 +9,7 @@ import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.update import org.koitharu.kotatsu.R 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.prefs.AppSettings import org.koitharu.kotatsu.core.prefs.ListMode @@ -78,8 +79,7 @@ class MultiSearchViewModel( listData.value = emptyList() loadingData.value = true query.postValue(q) - val errors = searchImpl(q) - listError.value = errors.firstOrNull() + searchImpl(q) } catch (e: Throwable) { listError.value = e } finally { @@ -88,25 +88,44 @@ class MultiSearchViewModel( } } - private suspend fun searchImpl(q: String): List { + private suspend fun searchImpl(q: String) { val sources = settings.getMangaSources(includeHidden = false) val dispatcher = Dispatchers.Default.limitedParallelism(MAX_PARALLELISM) - return coroutineScope { + val deferredList = coroutineScope { sources.map { source -> async(dispatcher) { runCatching { val list = MangaRepository(source).getList(offset = 0, query = q) - // .sortedBy { x -> x.title.levenshteinDistance(q) } .toUi(ListMode.GRID) if (list.isNotEmpty()) { - val item = MultiSearchListModel(source, list) - listData.update { x -> x + item } + MultiSearchListModel(source, list) + } else { + null } }.onFailure { it.printStackTraceDebug() - }.exceptionOrNull() + } } } - }.awaitAll().filterNotNull() + } + val errors = ArrayList() + 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) + } } } \ No newline at end of file