diff --git a/composeApp/src/commonMain/kotlin/com/nuvio/app/features/home/HomeCatalogSettingsRepository.kt b/composeApp/src/commonMain/kotlin/com/nuvio/app/features/home/HomeCatalogSettingsRepository.kt index bb36428a..0767805a 100644 --- a/composeApp/src/commonMain/kotlin/com/nuvio/app/features/home/HomeCatalogSettingsRepository.kt +++ b/composeApp/src/commonMain/kotlin/com/nuvio/app/features/home/HomeCatalogSettingsRepository.kt @@ -114,6 +114,7 @@ object HomeCatalogSettingsRepository { return } normalizePreferences() + enforcePinnedCollectionsAtTop() publish() persist() } @@ -122,6 +123,7 @@ object HomeCatalogSettingsRepository { ensureLoaded() collectionDefinitions = buildCollectionDefinitions(collections) normalizePreferences() + enforcePinnedCollectionsAtTop() publish() persist() } @@ -382,6 +384,30 @@ object HomeCatalogSettingsRepository { return (catalogKeys + collectionKeys) .sortedBy { key -> preferences[key]?.order ?: Int.MAX_VALUE } } + + private fun enforcePinnedCollectionsAtTop() { + val orderedKeys = allOrderedKeys() + if (orderedKeys.isEmpty()) return + + val pinnedCollectionKeys = collectionDefinitions + .asSequence() + .filter { it.isPinnedToTop } + .map { it.key } + .toSet() + if (pinnedCollectionKeys.isEmpty()) return + + val pinnedKeys = orderedKeys.filter { it in pinnedCollectionKeys } + if (pinnedKeys.isEmpty()) return + + val nonPinnedKeys = orderedKeys.filterNot { it in pinnedCollectionKeys } + val reorderedKeys = pinnedKeys + nonPinnedKeys + if (reorderedKeys == orderedKeys) return + + reorderedKeys.forEachIndexed { index, itemKey -> + val current = preferences[itemKey] ?: return@forEachIndexed + preferences[itemKey] = current.copy(order = index) + } + } } internal data class CollectionCatalogDefinition(