From d031f22012055317d1c20fcb74ece4d355224d73 Mon Sep 17 00:00:00 2001 From: tapframe <85391825+tapframe@users.noreply.github.com> Date: Wed, 29 Apr 2026 23:48:11 +0530 Subject: [PATCH] fix: implement catalog resolution logic for folders --- .../collection/CollectionCatalogResolver.kt | 43 +++++++++++++++++++ .../collection/CollectionEditorScreen.kt | 10 +---- .../collection/FolderDetailRepository.kt | 11 +++-- 3 files changed, 50 insertions(+), 14 deletions(-) create mode 100644 composeApp/src/commonMain/kotlin/com/nuvio/app/features/collection/CollectionCatalogResolver.kt diff --git a/composeApp/src/commonMain/kotlin/com/nuvio/app/features/collection/CollectionCatalogResolver.kt b/composeApp/src/commonMain/kotlin/com/nuvio/app/features/collection/CollectionCatalogResolver.kt new file mode 100644 index 00000000..cad93b34 --- /dev/null +++ b/composeApp/src/commonMain/kotlin/com/nuvio/app/features/collection/CollectionCatalogResolver.kt @@ -0,0 +1,43 @@ +package com.nuvio.app.features.collection + +import com.nuvio.app.features.addons.AddonCatalog +import com.nuvio.app.features.addons.ManagedAddon + +internal data class ResolvedCollectionCatalog( + val addon: ManagedAddon, + val catalog: AddonCatalog, +) + +internal fun List.findCollectionCatalog( + source: CollectionCatalogSource, +): ResolvedCollectionCatalog? { + val declaredAddon = firstOrNull { it.manifest?.id == source.addonId } + val declaredCatalog = declaredAddon?.manifest?.catalogs?.findSourceCatalog(source) + if (declaredAddon != null && declaredCatalog != null) { + return ResolvedCollectionCatalog(addon = declaredAddon, catalog = declaredCatalog) + } + + return firstNotNullOfOrNull { addon -> + val catalog = addon.manifest?.catalogs?.find { + it.id == source.catalogId && it.type == source.type + } ?: return@firstNotNullOfOrNull null + ResolvedCollectionCatalog(addon = addon, catalog = catalog) + } +} + +internal fun List.findAvailableCatalog( + source: CollectionCatalogSource, +): AvailableCatalog? { + val declaredCatalogs = filter { it.addonId == source.addonId } + return declaredCatalogs.findSourceCatalog(source) + ?: firstOrNull { it.catalogId == source.catalogId && it.type == source.type } +} + +private fun List.findSourceCatalog(source: CollectionCatalogSource): AddonCatalog? = + find { it.id == source.catalogId && it.type == source.type } + ?: find { it.id == source.catalogId.substringBefore(",") && it.type == source.type } + +private fun List.findSourceCatalog(source: CollectionCatalogSource): AvailableCatalog? = + find { it.catalogId == source.catalogId && it.type == source.type } + ?: find { it.catalogId == source.catalogId.substringBefore(",") && it.type == source.type } + diff --git a/composeApp/src/commonMain/kotlin/com/nuvio/app/features/collection/CollectionEditorScreen.kt b/composeApp/src/commonMain/kotlin/com/nuvio/app/features/collection/CollectionEditorScreen.kt index 1a7bdf04..a47e36ab 100644 --- a/composeApp/src/commonMain/kotlin/com/nuvio/app/features/collection/CollectionEditorScreen.kt +++ b/composeApp/src/commonMain/kotlin/com/nuvio/app/features/collection/CollectionEditorScreen.kt @@ -111,9 +111,7 @@ fun CollectionEditorScreen( val genrePickerSource = genrePickerIndex?.let { editingFolder.resolvedSources.getOrNull(it) } val genrePickerCatalogSource = genrePickerSource?.addonCatalogSource() val genrePickerCatalog = genrePickerCatalogSource?.let { source -> - state.availableCatalogs.find { - it.addonId == source.addonId && it.type == source.type && it.catalogId == source.catalogId - } + state.availableCatalogs.findAvailableCatalog(source) } FolderEditorPage( @@ -757,11 +755,7 @@ private fun FolderEditorPage( } else if (addonSource != null) { FolderCatalogSourceCard( source = addonSource, - matchingCatalog = state.availableCatalogs.find { - it.addonId == addonSource.addonId && - it.type == addonSource.type && - it.catalogId == addonSource.catalogId - }, + matchingCatalog = state.availableCatalogs.findAvailableCatalog(addonSource), onRemove = { CollectionEditorRepository.removeCatalogSource(index) }, onOpenGenrePicker = { CollectionEditorRepository.showGenrePicker(index) }, ) diff --git a/composeApp/src/commonMain/kotlin/com/nuvio/app/features/collection/FolderDetailRepository.kt b/composeApp/src/commonMain/kotlin/com/nuvio/app/features/collection/FolderDetailRepository.kt index decff7a5..36698b25 100644 --- a/composeApp/src/commonMain/kotlin/com/nuvio/app/features/collection/FolderDetailRepository.kt +++ b/composeApp/src/commonMain/kotlin/com/nuvio/app/features/collection/FolderDetailRepository.kt @@ -150,10 +150,9 @@ object FolderDetailRepository { ) } else { val catalogSource = source.addonCatalogSource() ?: return@forEach - val addon = addons.find { it.manifest?.id == catalogSource.addonId } - val catalog = addon?.manifest?.catalogs?.find { - it.id == catalogSource.catalogId && it.type == catalogSource.type - } + val resolvedCatalog = addons.findCollectionCatalog(catalogSource) + val addon = resolvedCatalog?.addon + val catalog = resolvedCatalog?.catalog val label = catalog?.name ?: catalogSource.catalogId val typeLabel = localizedMediaTypeLabel(catalogSource.type) val genreSuffix = if (catalogSource.genre != null) " ยท ${catalogSource.genre}" else "" @@ -188,8 +187,8 @@ object FolderDetailRepository { sources.forEachIndexed { sourceIndex, source -> val tabIndex = if (showAll) sourceIndex + 1 else sourceIndex val catalogSource = source.addonCatalogSource() - val addon = catalogSource?.let { value -> addons.find { it.manifest?.id == value.addonId } } - if (!source.isTmdb && addon == null) { + val resolvedCatalog = catalogSource?.let { addons.findCollectionCatalog(it) } + if (!source.isTmdb && resolvedCatalog == null) { updateTab(tabIndex) { it.copy( isLoading = false,