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:
parent
0db0238d6b
commit
5bdd464200
3 changed files with 32 additions and 7 deletions
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Reference in a new issue