Fix global search

master
Koitharu 2 years ago
parent cb4ee2dcca
commit 43c65bf95b
Signed by: Koitharu
GPG Key ID: 676DEE768C17A9D7

@ -16,8 +16,8 @@ android {
applicationId 'org.koitharu.kotatsu' applicationId 'org.koitharu.kotatsu'
minSdk = 21 minSdk = 21
targetSdk = 35 targetSdk = 35
versionCode = 653 versionCode = 654
versionName = '7.4-a2' versionName = '7.4-a3'
generatedDensities = [] generatedDensities = []
testInstrumentationRunner 'org.koitharu.kotatsu.HiltTestRunner' testInstrumentationRunner 'org.koitharu.kotatsu.HiltTestRunner'
ksp { ksp {

@ -13,9 +13,6 @@ import org.koitharu.kotatsu.parsers.model.SortOrder
import java.util.EnumSet import java.util.EnumSet
import java.util.Locale 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 { class EmptyMangaRepository(override val source: MangaSource) : MangaRepository {
override val sortOrders: Set<SortOrder> override val sortOrders: Set<SortOrder>

@ -4,6 +4,7 @@ import androidx.annotation.AnyThread
import androidx.collection.ArrayMap import androidx.collection.ArrayMap
import org.koitharu.kotatsu.core.cache.MemoryContentCache import org.koitharu.kotatsu.core.cache.MemoryContentCache
import org.koitharu.kotatsu.core.model.LocalMangaSource 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.model.UnknownMangaSource
import org.koitharu.kotatsu.core.network.MirrorSwitchInterceptor import org.koitharu.kotatsu.core.network.MirrorSwitchInterceptor
import org.koitharu.kotatsu.local.data.LocalMangaRepository import org.koitharu.kotatsu.local.data.LocalMangaRepository
@ -69,6 +70,7 @@ interface MangaRepository {
@AnyThread @AnyThread
fun create(source: MangaSource): MangaRepository { fun create(source: MangaSource): MangaRepository {
when (source) { when (source) {
is MangaSourceInfo -> return create(source.mangaSource)
LocalMangaSource -> return localMangaRepository LocalMangaSource -> return localMangaRepository
UnknownMangaSource -> return EmptyMangaRepository(source) UnknownMangaSource -> return EmptyMangaRepository(source)
} }

@ -14,8 +14,10 @@ import kotlinx.coroutines.flow.channelFlow
import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.filterNotNull import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.flatMapLatest import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.onEmpty
import kotlinx.coroutines.flow.runningFold import kotlinx.coroutines.flow.runningFold
import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.joinAll
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.plus import kotlinx.coroutines.plus
import kotlinx.coroutines.sync.Semaphore import kotlinx.coroutines.sync.Semaphore
@ -112,37 +114,39 @@ class MultiSearchViewModel @Inject constructor(
return@channelFlow return@channelFlow
} }
val semaphore = Semaphore(MAX_PARALLELISM) val semaphore = Semaphore(MAX_PARALLELISM)
for (source in sources) { sources.mapNotNull { source ->
val repository = mangaRepositoryFactory.create(source) val repository = mangaRepositoryFactory.create(source)
if (!repository.isSearchSupported) { if (!repository.isSearchSupported) {
continue null
} } else {
launch { launch {
val item = runCatchingCancellable { val item = runCatchingCancellable {
semaphore.withPermit { semaphore.withPermit {
mangaListMapper.toListModelList( mangaListMapper.toListModelList(
manga = repository.getList(offset = 0, filter = MangaListFilter.Search(q)), manga = repository.getList(offset = 0, filter = MangaListFilter.Search(q)),
mode = ListMode.GRID, mode = ListMode.GRID,
) )
}
}.fold(
onSuccess = { list ->
if (list.isEmpty()) {
null
} else {
MultiSearchListModel(source, list.size > MIN_HAS_MORE_ITEMS, list, null)
} }
}, }.fold(
onFailure = { error -> onSuccess = { list ->
error.printStackTraceDebug() if (list.isEmpty()) {
MultiSearchListModel(source, true, emptyList(), error) null
}, } else {
) MultiSearchListModel(source, list.size > MIN_HAS_MORE_ITEMS, list, null)
if (item != null) { }
send(item) },
onFailure = { error ->
error.printStackTraceDebug()
MultiSearchListModel(source, true, emptyList(), error)
},
)
if (item != null) {
send(item)
}
} }
} }
} }.joinAll()
}.runningFold<MultiSearchListModel, List<MultiSearchListModel>?>(null) { list, item -> list.orEmpty() + item } }.runningFold<MultiSearchListModel, List<MultiSearchListModel>?>(null) { list, item -> list.orEmpty() + item }
.filterNotNull() .filterNotNull()
.onEmpty { emit(emptyList()) }
} }

Loading…
Cancel
Save