Yokan / rebuild
Course 1 8624561

SourcePresets.kt

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

implementation75 lines
1package eu.friedcutlet.yokan.ui.screens.browse23import eu.friedcutlet.yokan.database.DbSavedSearch4import eu.friedcutlet.yokan.database.DbSettings5import eu.friedcutlet.yokan.database.DbSource6import eu.friedcutlet.yokan.database.presetIdentity7import eu.friedcutlet.yokan.services.extensions.PresetIssue8import eu.friedcutlet.yokan.services.extensions.PresetIssueReason9import eu.friedcutlet.yokan.services.extensions.PresetValidation10import eu.friedcutlet.yokan.services.extensions.capturePresetFilters11import eu.friedcutlet.yokan.services.extensions.validateSourcePreset12import kotlin.random.Random1314internal fun DbSettings.withSourcePreset(preset: DbSavedSearch): DbSettings = copy(15    savedSearches = savedSearches.orEmpty().filterNot { it.sourceId == preset.sourceId && it.id == preset.id } + preset,16)1718internal fun DbSettings.withoutSourcePreset(sourceId: Long, id: String): DbSettings = copy(19    savedSearches = savedSearches.orEmpty().filterNot { it.sourceId == sourceId && it.id == id },20)2122internal fun DbSettings.sourcePreset(sourceId: Long, id: String): DbSavedSearch? =23    savedSearches.orEmpty().firstOrNull { it.sourceId == sourceId && it.id == id }2425internal fun newPresetId(): String = "preset:" + Random.nextBytes(16).toHexString()2627/** Result of committing the open [PresetEditor]: the editor to show next and the preset to store, if any. */28internal data class PresetCommit(val editor: PresetEditor?, val save: DbSavedSearch? = null)2930/**31 * Commits [state]'s preset editor for [source]. Returns null when nothing changes (no editor, or the32 * filter schema is not ready for a save/update); otherwise the next editor (kept open with an error or33 * conflict, or closed) and the preset to store.34 */35internal fun commitPreset(36    state: SourceHomeUiState,37    db: DbSettings,38    source: DbSource,39    replace: Boolean,40    newId: () -> String = ::newPresetId,41): PresetCommit? {42    val editor = state.presetEditor ?: return null43    val name = editor.name.trim()44    if (name.isEmpty()) return PresetCommit(editor.copy(invalidName = true))45    val target = editor.targetId?.let { db.sourcePreset(source.id, it) }46    if (editor.targetId != null && target == null) return PresetCommit(null)47    val conflict = db.savedSearches.orEmpty().firstOrNull {48        it.sourceId == source.id && it.name == name && it.id != editor.targetId49    }50    if (conflict != null && (!replace || editor.conflictId != conflict.id || editor.mode == PresetEditMode.Rename)) {51        return PresetCommit(editor.copy(conflictId = conflict.id))52    }53    if (editor.mode != PresetEditMode.Rename && !state.filterSchemaReady) return null54    val preset = if (editor.mode == PresetEditMode.Rename && target != null) target.copy(name = name) else DbSavedSearch(55        sourceId = source.id, name = name, query = state.searchQuery.trim(),56        id = target?.id ?: conflict?.id ?: newId(),57        sourceIdentity = source.presetIdentity(),58        filters = capturePresetFilters(state.filters, state.selectedFilterValues),59        filterSchemaRecorded = true,60    )61    return PresetCommit(null, preset)62}6364/** Runs [preset] as a search when it validates against the loaded filters; otherwise opens its review. */65internal fun SourceHomeUiState.withPresetApplied(preset: DbSavedSearch, source: DbSource): SourceHomeUiState {66    val validation = if (filterSchemaReady) validateSourcePreset(preset, source, filters)67        else PresetValidation(emptyMap(), listOf(PresetIssue(preset.name, PresetIssueReason.SchemaNotReady)))68    if (validation.issues.isNotEmpty()) return copy(presetReview = PresetReview(preset, validation))69    return copy(isSearchMode = true, selectedTab = SourceTab.Search, searchQuery = preset.query,70        submittedQuery = preset.query, selectedFilterValues = validation.selections,71        appliedFilterValues = validation.selections, filtersApplied = validation.selections.isNotEmpty(),72        page = 1, items = emptyList(), hasNextPage = false, appendExhausted = false,73        loading = false, loadingMore = false, loadedKey = null,74        requestRevision = requestRevision + 1, presetReview = null)75}76

Search chapters, reference and source filenames.