From 5ca22f1419f3cf43575e26bf752e969ca87546e9 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Mon, 24 Jul 2023 16:57:24 +0300 Subject: [PATCH] Fix crash with empty EnumSet --- .../org/koitharu/kotatsu/core/util/ext/Collections.kt | 7 +++++++ .../search/ui/suggestion/SearchSuggestionViewModel.kt | 4 ++-- .../kotatsu/settings/sources/SourcesListViewModel.kt | 3 ++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Collections.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Collections.kt index 9cb967878..1e66f3b14 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Collections.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Collections.kt @@ -3,6 +3,7 @@ package org.koitharu.kotatsu.core.util.ext import androidx.collection.ArrayMap import androidx.collection.ArraySet import java.util.Collections +import java.util.EnumSet @Deprecated("TODO: remove") fun MutableList.move(sourceIndex: Int, targetIndex: Int) { @@ -61,3 +62,9 @@ fun List.takeMostFrequent(limit: Int): List { } } } + +inline fun > Collection.toEnumSet(): EnumSet = if (isEmpty()) { + EnumSet.noneOf(E::class.java) +} else { + EnumSet.copyOf(this) +} diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/search/ui/suggestion/SearchSuggestionViewModel.kt b/app/src/main/kotlin/org/koitharu/kotatsu/search/ui/suggestion/SearchSuggestionViewModel.kt index 2be9f36c4..46c523bff 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/search/ui/suggestion/SearchSuggestionViewModel.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/search/ui/suggestion/SearchSuggestionViewModel.kt @@ -19,12 +19,12 @@ import org.koitharu.kotatsu.core.prefs.AppSettings import org.koitharu.kotatsu.core.prefs.observeAsStateFlow import org.koitharu.kotatsu.core.ui.BaseViewModel import org.koitharu.kotatsu.core.ui.widgets.ChipsView +import org.koitharu.kotatsu.core.util.ext.toEnumSet import org.koitharu.kotatsu.explore.data.MangaSourcesRepository import org.koitharu.kotatsu.parsers.model.MangaSource import org.koitharu.kotatsu.parsers.model.MangaTag import org.koitharu.kotatsu.search.domain.MangaSearchRepository import org.koitharu.kotatsu.search.ui.suggestion.model.SearchSuggestionItem -import java.util.EnumSet import javax.inject.Inject private const val DEBOUNCE_TIMEOUT = 500L @@ -91,7 +91,7 @@ class SearchSuggestionViewModel @Inject constructor( suggestionJob?.cancel() suggestionJob = combine( query.debounce(DEBOUNCE_TIMEOUT), - sourcesRepository.observeEnabledSources().map { EnumSet.copyOf(it) }, + sourcesRepository.observeEnabledSources().map { it.toEnumSet() }, ::Pair, ).mapLatest { (searchQuery, enabledSources) -> buildSearchSuggestion(searchQuery, enabledSources) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/SourcesListViewModel.kt b/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/SourcesListViewModel.kt index 02ee26737..73a1e3471 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/SourcesListViewModel.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/settings/sources/SourcesListViewModel.kt @@ -18,6 +18,7 @@ import org.koitharu.kotatsu.core.util.AlphanumComparator import org.koitharu.kotatsu.core.util.ext.MutableEventFlow import org.koitharu.kotatsu.core.util.ext.call import org.koitharu.kotatsu.core.util.ext.map +import org.koitharu.kotatsu.core.util.ext.toEnumSet import org.koitharu.kotatsu.explore.data.MangaSourcesRepository import org.koitharu.kotatsu.parsers.model.MangaSource import org.koitharu.kotatsu.parsers.util.toTitleCase @@ -122,7 +123,7 @@ class SourcesListViewModel @Inject constructor( private suspend fun buildList() = withContext(Dispatchers.Default) { val allSources = repository.allMangaSources val enabledSources = repository.getEnabledSources() - val enabledSet = EnumSet.copyOf(enabledSources) + val enabledSet = enabledSources.toEnumSet() val query = searchQuery if (!query.isNullOrEmpty()) { items.value = allSources.mapNotNull {