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
|
@Composable
|
||||||
fun LibraryContent(
|
fun LibraryContent(
|
||||||
categories: List<Category>,
|
categories: List<Category>,
|
||||||
|
// KMK -->
|
||||||
|
activeCategoryIndex: Int,
|
||||||
|
// KMK <--
|
||||||
searchQuery: String?,
|
searchQuery: String?,
|
||||||
selection: Set<Long>,
|
selection: Set<Long>,
|
||||||
contentPadding: PaddingValues,
|
contentPadding: PaddingValues,
|
||||||
|
|
@ -60,9 +63,17 @@ fun LibraryContent(
|
||||||
|
|
||||||
if (showPageTabs && categories.isNotEmpty() && (categories.size > 1 || !categories.first().isSystemCategory)) {
|
if (showPageTabs && categories.isNotEmpty() && (categories.size > 1 || !categories.first().isSystemCategory)) {
|
||||||
LaunchedEffect(categories) {
|
LaunchedEffect(categories) {
|
||||||
if (categories.size <= pagerState.currentPage) {
|
// KMK -->
|
||||||
pagerState.scrollToPage(categories.size - 1)
|
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(
|
LibraryTabs(
|
||||||
categories = categories,
|
categories = categories,
|
||||||
|
|
|
||||||
|
|
@ -1386,7 +1386,12 @@ class LibraryScreenModel(
|
||||||
|
|
||||||
fun updateActiveCategoryIndex(index: Int) {
|
fun updateActiveCategoryIndex(index: Int) {
|
||||||
val newIndex = mutableState.updateAndGet { state ->
|
val newIndex = mutableState.updateAndGet { state ->
|
||||||
state.copy(activeCategoryIndex = index)
|
state.copy(
|
||||||
|
activeCategoryIndex = index,
|
||||||
|
// KMK -->
|
||||||
|
activeCategoryId = state.displayedCategories.getOrNull(index)?.id,
|
||||||
|
// KMK <--
|
||||||
|
)
|
||||||
}
|
}
|
||||||
.coercedActiveCategoryIndex
|
.coercedActiveCategoryIndex
|
||||||
|
|
||||||
|
|
@ -1653,6 +1658,9 @@ class LibraryScreenModel(
|
||||||
val dialog: Dialog? = null,
|
val dialog: Dialog? = null,
|
||||||
val libraryData: LibraryData = LibraryData(),
|
val libraryData: LibraryData = LibraryData(),
|
||||||
private val activeCategoryIndex: Int = 0,
|
private val activeCategoryIndex: Int = 0,
|
||||||
|
// KMK -->
|
||||||
|
private val activeCategoryId: Long? = null,
|
||||||
|
// KMK <--
|
||||||
private val groupedFavorites: Map<Category, List</* LibraryItem */ Long>> = emptyMap(),
|
private val groupedFavorites: Map<Category, List</* LibraryItem */ Long>> = emptyMap(),
|
||||||
// SY -->
|
// SY -->
|
||||||
val showSyncExh: Boolean = false,
|
val showSyncExh: Boolean = false,
|
||||||
|
|
@ -1671,10 +1679,13 @@ class LibraryScreenModel(
|
||||||
*/
|
*/
|
||||||
val displayedCategories: List<Category> = groupedFavorites.keys.toList()
|
val displayedCategories: List<Category> = groupedFavorites.keys.toList()
|
||||||
|
|
||||||
val coercedActiveCategoryIndex = activeCategoryIndex.coerceIn(
|
val coercedActiveCategoryIndex = /* KMK --> */ displayedCategories.indexOfFirst { it.id == activeCategoryId }
|
||||||
minimumValue = 0,
|
.takeIf { it != -1 } ?: activeCategoryIndex
|
||||||
maximumValue = displayedCategories.lastIndex.coerceAtLeast(0),
|
// KMK <--
|
||||||
)
|
.coerceIn(
|
||||||
|
minimumValue = 0,
|
||||||
|
maximumValue = displayedCategories.lastIndex.coerceAtLeast(0),
|
||||||
|
)
|
||||||
|
|
||||||
val activeCategory: Category? = displayedCategories.getOrNull(coercedActiveCategoryIndex)
|
val activeCategory: Category? = displayedCategories.getOrNull(coercedActiveCategoryIndex)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -302,6 +302,9 @@ data object LibraryTab : Tab {
|
||||||
else -> {
|
else -> {
|
||||||
LibraryContent(
|
LibraryContent(
|
||||||
categories = state.displayedCategories,
|
categories = state.displayedCategories,
|
||||||
|
// KMK -->
|
||||||
|
activeCategoryIndex = state.coercedActiveCategoryIndex,
|
||||||
|
// KMK <--
|
||||||
searchQuery = state.searchQuery,
|
searchQuery = state.searchQuery,
|
||||||
selection = state.selection,
|
selection = state.selection,
|
||||||
contentPadding = contentPadding,
|
contentPadding = contentPadding,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue