ref: pinned collections to appear at the top in HomeCatalogSettingsRepository

This commit is contained in:
tapframe 2026-04-08 14:04:47 +05:30
parent acbd830edb
commit 8e8e65d831

View file

@ -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(