From 43c65bf95be1fd0e399f39dc55f9a7a1392c0df5 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Wed, 24 Jul 2024 11:40:55 +0300 Subject: [PATCH] Fix global search --- app/build.gradle | 4 +- .../core/parser/EmptyMangaRepository.kt | 3 - .../kotatsu/core/parser/MangaRepository.kt | 2 + .../search/ui/multi/MultiSearchViewModel.kt | 56 ++++++++++--------- 4 files changed, 34 insertions(+), 31 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index a06c935ed..168075340 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -16,8 +16,8 @@ android { applicationId 'org.koitharu.kotatsu' minSdk = 21 targetSdk = 35 - versionCode = 653 - versionName = '7.4-a2' + versionCode = 654 + versionName = '7.4-a3' generatedDensities = [] testInstrumentationRunner 'org.koitharu.kotatsu.HiltTestRunner' ksp { diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/EmptyMangaRepository.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/EmptyMangaRepository.kt index f9ff0d1fb..833b87edd 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/EmptyMangaRepository.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/EmptyMangaRepository.kt @@ -13,9 +13,6 @@ import org.koitharu.kotatsu.parsers.model.SortOrder import java.util.EnumSet import java.util.Locale -/** - * This parser is just for parser development, it should not be used in releases - */ class EmptyMangaRepository(override val source: MangaSource) : MangaRepository { override val sortOrders: Set diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/MangaRepository.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/MangaRepository.kt index 03c6c22df..909762ceb 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/MangaRepository.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/parser/MangaRepository.kt @@ -4,6 +4,7 @@ import androidx.annotation.AnyThread import androidx.collection.ArrayMap import org.koitharu.kotatsu.core.cache.MemoryContentCache import org.koitharu.kotatsu.core.model.LocalMangaSource +import org.koitharu.kotatsu.core.model.MangaSourceInfo import org.koitharu.kotatsu.core.model.UnknownMangaSource import org.koitharu.kotatsu.core.network.MirrorSwitchInterceptor import org.koitharu.kotatsu.local.data.LocalMangaRepository @@ -69,6 +70,7 @@ interface MangaRepository { @AnyThread fun create(source: MangaSource): MangaRepository { when (source) { + is MangaSourceInfo -> return create(source.mangaSource) LocalMangaSource -> return localMangaRepository UnknownMangaSource -> return EmptyMangaRepository(source) } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/search/ui/multi/MultiSearchViewModel.kt b/app/src/main/kotlin/org/koitharu/kotatsu/search/ui/multi/MultiSearchViewModel.kt index e772af0be..686271449 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/search/ui/multi/MultiSearchViewModel.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/search/ui/multi/MultiSearchViewModel.kt @@ -14,8 +14,10 @@ import kotlinx.coroutines.flow.channelFlow import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.filterNotNull import kotlinx.coroutines.flow.flatMapLatest +import kotlinx.coroutines.flow.onEmpty import kotlinx.coroutines.flow.runningFold import kotlinx.coroutines.flow.stateIn +import kotlinx.coroutines.joinAll import kotlinx.coroutines.launch import kotlinx.coroutines.plus import kotlinx.coroutines.sync.Semaphore @@ -112,37 +114,39 @@ class MultiSearchViewModel @Inject constructor( return@channelFlow } val semaphore = Semaphore(MAX_PARALLELISM) - for (source in sources) { + sources.mapNotNull { source -> val repository = mangaRepositoryFactory.create(source) if (!repository.isSearchSupported) { - continue - } - launch { - val item = runCatchingCancellable { - semaphore.withPermit { - mangaListMapper.toListModelList( - manga = repository.getList(offset = 0, filter = MangaListFilter.Search(q)), - mode = ListMode.GRID, - ) - } - }.fold( - onSuccess = { list -> - if (list.isEmpty()) { - null - } else { - MultiSearchListModel(source, list.size > MIN_HAS_MORE_ITEMS, list, null) + null + } else { + launch { + val item = runCatchingCancellable { + semaphore.withPermit { + mangaListMapper.toListModelList( + manga = repository.getList(offset = 0, filter = MangaListFilter.Search(q)), + mode = ListMode.GRID, + ) } - }, - onFailure = { error -> - error.printStackTraceDebug() - MultiSearchListModel(source, true, emptyList(), error) - }, - ) - if (item != null) { - send(item) + }.fold( + onSuccess = { list -> + if (list.isEmpty()) { + null + } else { + MultiSearchListModel(source, list.size > MIN_HAS_MORE_ITEMS, list, null) + } + }, + onFailure = { error -> + error.printStackTraceDebug() + MultiSearchListModel(source, true, emptyList(), error) + }, + ) + if (item != null) { + send(item) + } } } - } + }.joinAll() }.runningFold?>(null) { list, item -> list.orEmpty() + item } .filterNotNull() + .onEmpty { emit(emptyList()) } }