DbSourcePreset.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/database/DbSourcePreset.kt
1package eu.friedcutlet.yokan.database23import kotlinx.serialization.SerialName4import kotlinx.serialization.Serializable56/** Source identity excludes installed version and local row ID, which can change during restore. */7@Serializable8data class DbPresetSource(9 val name: String,10 val baseUrl: String,11 val lang: String,12 val itemType: DbItemType,13 val runtime: DbSourceCodeLanguage,14 val typeSource: String,15 val repository: String,16)1718@Serializable19data class DbPresetOption(val label: String, val value: String)2021@Serializable22data class DbPresetFilter(23 val id: String,24 val name: String,25 val type: String,26 val kind: String,27 val stableId: Boolean,28 val options: List<DbPresetOption>,29 val defaultValue: String?,30 val defaultAscending: Boolean = true,31 val selection: DbPresetSelection? = null,32)3334@Serializable35sealed interface DbPresetSelection {36 @Serializable @SerialName("toggle") data class Toggle(val checked: Boolean) : DbPresetSelection37 @Serializable @SerialName("single") data class Single(val value: String) : DbPresetSelection38 @Serializable @SerialName("multi") data class Multi(val values: Set<String>) : DbPresetSelection39 @Serializable @SerialName("triState") data class TriState(val state: Int) : DbPresetSelection40 @Serializable @SerialName("text") data class Text(val value: String) : DbPresetSelection41 @Serializable @SerialName("sort") data class Sort(val value: String, val ascending: Boolean) : DbPresetSelection42}4344fun DbSource.presetIdentity(): DbPresetSource = DbPresetSource(45 name = name.trim(), baseUrl = baseUrl.trim().trimEnd('/'), lang = lang,46 itemType = itemType, runtime = sourceCodeLanguage, typeSource = typeSource,47 repository = repo?.jsonUrl.orEmpty().trim().trimEnd('/'),48)4950/** Bind legacy query-only rows while their original source still exists. */51fun DbSettings.adoptLegacySourcePresets(sources: List<DbSource>): DbSettings {52 if (savedSearches.orEmpty().none { it.sourceIdentity == null && !it.missingSourceIdentity }) return this53 val byId = sources.associateBy { it.id }54 return copy(savedSearches = savedSearches.orEmpty().map { preset ->55 if (preset.sourceIdentity != null || preset.missingSourceIdentity) preset else {56 val source = byId[preset.sourceId]57 preset.copy(sourceIdentity = source?.presetIdentity(), missingSourceIdentity = source == null)58 }59 })60}61