feat(library): Improve category tab selection when searching/filtering library or adding/removing categories (#1233)

* feat(library): Improve category tab selection when searching/filtering library or adding/removing categories

* Improve active category index handling in LibraryScreenModel
This commit is contained in:
Cuong-Tran 2025-10-13 13:10:31 +07:00 committed by GitHub
parent 0db0238d6b
commit 5bdd464200
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 32 additions and 7 deletions

View file

@ -28,6 +28,9 @@ import kotlin.time.Duration.Companion.seconds
@Composable
fun LibraryContent(
categories: List<Category>,
// KMK -->
activeCategoryIndex: Int,
// KMK <--
searchQuery: String?,
selection: Set<Long>,
contentPadding: PaddingValues,
@ -60,9 +63,17 @@ fun LibraryContent(
if (showPageTabs && categories.isNotEmpty() && (categories.size > 1 || !categories.first().isSystemCategory)) {
LaunchedEffect(categories) {
if (categories.size <= pagerState.currentPage) {
pagerState.scrollToPage(categories.size - 1)
// KMK -->
val targetPage = when {
categories.isEmpty() -> 0
activeCategoryIndex != pagerState.currentPage -> activeCategoryIndex.coerceAtMost(categories.size - 1)
pagerState.currentPage >= categories.size -> categories.size - 1
else -> pagerState.currentPage
}
if (targetPage != pagerState.currentPage) {
pagerState.scrollToPage(targetPage)
}
// KMK <--
}
LibraryTabs(
categories = categories,

View file

@ -1386,7 +1386,12 @@ class LibraryScreenModel(
fun updateActiveCategoryIndex(index: Int) {
val newIndex = mutableState.updateAndGet { state ->
state.copy(activeCategoryIndex = index)
state.copy(
activeCategoryIndex = index,
// KMK -->
activeCategoryId = state.displayedCategories.getOrNull(index)?.id,
// KMK <--
)
}
.coercedActiveCategoryIndex
@ -1653,6 +1658,9 @@ class LibraryScreenModel(
val dialog: Dialog? = null,
val libraryData: LibraryData = LibraryData(),
private val activeCategoryIndex: Int = 0,
// KMK -->
private val activeCategoryId: Long? = null,
// KMK <--
private val groupedFavorites: Map<Category, List</* LibraryItem */ Long>> = emptyMap(),
// SY -->
val showSyncExh: Boolean = false,
@ -1671,10 +1679,13 @@ class LibraryScreenModel(
*/
val displayedCategories: List<Category> = groupedFavorites.keys.toList()
val coercedActiveCategoryIndex = activeCategoryIndex.coerceIn(
minimumValue = 0,
maximumValue = displayedCategories.lastIndex.coerceAtLeast(0),
)
val coercedActiveCategoryIndex = /* KMK --> */ displayedCategories.indexOfFirst { it.id == activeCategoryId }
.takeIf { it != -1 } ?: activeCategoryIndex
// KMK <--
.coerceIn(
minimumValue = 0,
maximumValue = displayedCategories.lastIndex.coerceAtLeast(0),
)
val activeCategory: Category? = displayedCategories.getOrNull(coercedActiveCategoryIndex)

View file

@ -302,6 +302,9 @@ data object LibraryTab : Tab {
else -> {
LibraryContent(
categories = state.displayedCategories,
// KMK -->
activeCategoryIndex = state.coercedActiveCategoryIndex,
// KMK <--
searchQuery = state.searchQuery,
selection = state.selection,
contentPadding = contentPadding,