Yokan / rebuild
Course 1 8624561

GenreSearch.kt

composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/browse/GenreSearch.kt

implementation34 lines
1package eu.friedcutlet.yokan.ui.screens.browse23import eu.friedcutlet.yokan.services.extensions.FilterSelection4import eu.friedcutlet.yokan.services.extensions.SourceFilter5import eu.friedcutlet.yokan.services.extensions.SourceFilterKind67data class GenreSearchRequest(val query: String, val selections: Map<String, FilterSelection>)89/** Use the extension's outbound filter values, never the displayed genre label as a value. */10fun genreSearchRequest(genre: String, filters: List<SourceFilter>): GenreSearchRequest {11    val tag = genre.trim()12    val candidates = filters.mapNotNull { filter ->13        // Some extensions expose the same labels in include and exclude groups.14        if (filter.id.contains("exclude", ignoreCase = true) ||15            excludedGroup.containsMatchIn(filter.name)) return@mapNotNull null16        val name = filter.name.substringAfterLast(':').substringAfterLast('/').trim()17        val entry = filter.entries.firstOrNull { it.label.trim().equals(tag, ignoreCase = true) }18        val selection = when (filter.kind) {19            SourceFilterKind.MultiSelect -> entry?.let { FilterSelection.Multi(setOf(it.value)) }20            SourceFilterKind.SingleSelect -> entry?.let { FilterSelection.Single(it.value) }21            SourceFilterKind.Toggle -> if (name.equals(tag, ignoreCase = true)) FilterSelection.Toggle(true) else null22            SourceFilterKind.TriState -> if (name.equals(tag, ignoreCase = true)) FilterSelection.TriState(1) else null23            SourceFilterKind.Text, SourceFilterKind.Sort -> null24        }25        selection?.let { filter to it }26    }27    val candidate = candidates.firstOrNull { (filter, _) ->28        filter.name.contains("genre", ignoreCase = true) || filter.name.contains("tag", ignoreCase = true)29    } ?: candidates.firstOrNull()30    val selections = candidate?.let { (filter, selection) -> mapOf(filter.id to selection) }.orEmpty()31    return GenreSearchRequest(query = if (selections.isEmpty()) tag else "", selections = selections)32}3334private val excludedGroup = Regex("\\b(exclude|excluded|excluding|without)\\b", RegexOption.IGNORE_CASE)35

Search chapters, reference and source filenames.