Fix crash with empty EnumSet

This commit is contained in:
Koitharu
2023-07-24 16:57:24 +03:00
parent 345a878d83
commit 5ca22f1419
3 changed files with 11 additions and 3 deletions

View File

@@ -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 <T> MutableList<T>.move(sourceIndex: Int, targetIndex: Int) {
@@ -61,3 +62,9 @@ fun <T> List<T>.takeMostFrequent(limit: Int): List<T> {
}
}
}
inline fun <reified E : Enum<E>> Collection<E>.toEnumSet(): EnumSet<E> = if (isEmpty()) {
EnumSet.noneOf(E::class.java)
} else {
EnumSet.copyOf(this)
}

View File

@@ -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)

View File

@@ -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 {