From 090f7a585821e9cd02f9992ba05b96905c83dc5f Mon Sep 17 00:00:00 2001 From: Koitharu Date: Wed, 10 May 2023 20:01:15 +0300 Subject: [PATCH] Update random manga selecting --- .../explore/domain/ExploreRepository.kt | 62 +++++++++++++------ .../kotatsu/explore/ui/ExploreViewModel.kt | 2 - 2 files changed, 42 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/org/koitharu/kotatsu/explore/domain/ExploreRepository.kt b/app/src/main/java/org/koitharu/kotatsu/explore/domain/ExploreRepository.kt index 4bded9335..5fbce6f56 100644 --- a/app/src/main/java/org/koitharu/kotatsu/explore/domain/ExploreRepository.kt +++ b/app/src/main/java/org/koitharu/kotatsu/explore/domain/ExploreRepository.kt @@ -4,8 +4,12 @@ import org.koitharu.kotatsu.core.parser.MangaRepository import org.koitharu.kotatsu.core.prefs.AppSettings import org.koitharu.kotatsu.history.domain.HistoryRepository import org.koitharu.kotatsu.parsers.model.Manga -import org.koitharu.kotatsu.parsers.model.SortOrder +import org.koitharu.kotatsu.parsers.model.MangaSource import org.koitharu.kotatsu.suggestions.domain.TagsBlacklist +import org.koitharu.kotatsu.utils.ext.almostEquals +import org.koitharu.kotatsu.utils.ext.asArrayList +import org.koitharu.kotatsu.utils.ext.printStackTraceDebug +import org.koitharu.kotatsu.utils.ext.runCatchingCancellable import javax.inject.Inject class ExploreRepository @Inject constructor( @@ -16,28 +20,46 @@ class ExploreRepository @Inject constructor( suspend fun findRandomManga(tagsLimit: Int): Manga { val blacklistTagRegex = TagsBlacklist(settings.suggestionsTagsBlacklist, 0.4f) - val allTags = historyRepository.getPopularTags(tagsLimit).filterNot { - it in blacklistTagRegex + val tags = historyRepository.getPopularTags(tagsLimit).mapNotNull { + if (it in blacklistTagRegex) null else it.title } - val tag = allTags.randomOrNull() - val source = checkNotNull(tag?.source ?: settings.getMangaSources(includeHidden = false).randomOrNull()) { - "No sources found" - } - val repo = mangaRepositoryFactory.create(source) - val list = repo.getList( - offset = 0, - sortOrder = if (SortOrder.UPDATED in repo.sortOrders) SortOrder.UPDATED else null, - tags = setOfNotNull(tag), - ).shuffled() - for (item in list) { - if (settings.isSuggestionsExcludeNsfw && item.isNsfw) { - continue - } - if (item in blacklistTagRegex) { + val sources = settings.getMangaSources(includeHidden = false) + check(sources.isNotEmpty()) { "No sources available" } + for (i in 0..4) { + val list = getList(sources.random(), tags, blacklistTagRegex) + val manga = list.randomOrNull() ?: continue + val details = runCatchingCancellable { + mangaRepositoryFactory.create(manga.source).getDetails(manga) + }.getOrNull() ?: continue + if ((settings.isSuggestionsExcludeNsfw && details.isNsfw) || details in blacklistTagRegex) { continue } - return item + return details } - return list.random() + throw NoSuchElementException() } + + private suspend fun getList( + source: MangaSource, + tags: List, + blacklist: TagsBlacklist, + ): List = runCatchingCancellable { + val repository = mangaRepositoryFactory.create(source) + val order = repository.sortOrders.random() + val availableTags = repository.getTags() + val tag = tags.firstNotNullOfOrNull { title -> + availableTags.find { x -> x.title.almostEquals(title, 0.4f) } + } + val list = repository.getList(0, setOfNotNull(tag), order).asArrayList() + if (settings.isSuggestionsExcludeNsfw) { + list.removeAll { it.isNsfw } + } + if (blacklist.isNotEmpty()) { + list.removeAll { manga -> manga in blacklist } + } + list.shuffle() + list + }.onFailure { + it.printStackTraceDebug() + }.getOrDefault(emptyList()) } diff --git a/app/src/main/java/org/koitharu/kotatsu/explore/ui/ExploreViewModel.kt b/app/src/main/java/org/koitharu/kotatsu/explore/ui/ExploreViewModel.kt index 42e3847aa..d81472ba3 100644 --- a/app/src/main/java/org/koitharu/kotatsu/explore/ui/ExploreViewModel.kt +++ b/app/src/main/java/org/koitharu/kotatsu/explore/ui/ExploreViewModel.kt @@ -6,7 +6,6 @@ import androidx.lifecycle.viewModelScope import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.combine -import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.flatMapLatest import kotlinx.coroutines.flow.flowOf @@ -96,7 +95,6 @@ class ExploreViewModel @Inject constructor( } .onStart { emit("") } .map { settings.getMangaSources(includeHidden = false) } - .distinctUntilChanged() .combine(gridMode) { content, grid -> buildList(content, grid) } private fun buildList(sources: List, isGrid: Boolean): List {